From eb2b5db1f5af00edb5637f389e8c2c78c65d0d08 Mon Sep 17 00:00:00 2001 From: Parker Berberian Date: Mon, 21 Aug 2017 09:07:01 -0400 Subject: Adds Libvirt Handler JIRA: N/A Adds a handler which can control the libvirt hypervisor on the remote host, in order to define the vm's and networks that are needed for an OPNFV deployment. Also adds the domain and network objects, which are simple abstractions of virtual machines and networks. Change-Id: Ia836e7b080b8bca220d5fdf6eb72b6c580cab4d1 Signed-off-by: Parker Berberian --- laas-fog/source/network.py | 103 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 laas-fog/source/network.py (limited to 'laas-fog/source/network.py') diff --git a/laas-fog/source/network.py b/laas-fog/source/network.py new file mode 100644 index 0000000..234ba22 --- /dev/null +++ b/laas-fog/source/network.py @@ -0,0 +1,103 @@ +""" +############################################################################# +#Copyright 2017 Parker Berberian and others # +# # +#Licensed under the Apache License, Version 2.0 (the "License"); # +#you may not use this file except in compliance with the License. # +#You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +#Unless required by applicable law or agreed to in writing, software # +#distributed under the License is distributed on an "AS IS" BASIS, # +#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +#See the License for the specific language governing permissions and # +#limitations under the License. # +############################################################################# +""" + +import sys +import xml.dom +import xml.dom.minidom +import yaml + + +class Network: + """ + This class has a similar role as the Domain class. + This class will parse a config file and + write the xml definitions of those networks for libvirt. + """ + + def __init__(self, propertiesDict): + """ + init. propertiesDict should be + one of the dictionaries returned by parseConfigFile + """ + self.name = propertiesDict['name'] + self.brName = propertiesDict['brName'] + self.brAddr = propertiesDict['brAddr'] + self.netmask = propertiesDict['netmask'] + self.forward = propertiesDict['forward'] + self.dhcp = propertiesDict['dhcp'] + self.cidr = propertiesDict['cidr'] + + def toXML(self): + """ + Takes the config of this network and writes a valid xml definition + for libvirt. + returns a string + """ + definition = xml.dom.minidom.parseString("\n") + nameElem = definition.createElement('name') + nameElem.appendChild(definition.createTextNode(self.name)) + definition.documentElement.appendChild(nameElem) + + if self.forward['used']: + forwardElem = definition.createElement('forward') + forwardElem.setAttribute('mode', self.forward['type']) + definition.documentElement.appendChild(forwardElem) + + bridgeElem = definition.createElement('bridge') + bridgeElem.setAttribute('name', self.brName) + bridgeElem.setAttribute('stp', 'on') + bridgeElem.setAttribute('delay', '5') + definition.documentElement.appendChild(bridgeElem) + + ipElem = definition.createElement('ip') + ipElem.setAttribute('address', self.brAddr) + ipElem.setAttribute('netmask', self.netmask) + if self.dhcp['used']: + dhcpElem = definition.createElement('dhcp') + rangeElem = definition.createElement('range') + rangeElem.setAttribute('start', self.dhcp['rangeStart']) + rangeElem.setAttribute('end', self.dhcp['rangeEnd']) + dhcpElem.appendChild(rangeElem) + ipElem.appendChild(dhcpElem) + + definition.documentElement.appendChild(ipElem) + + self.xml = definition.toprettyxml() + return self.xml + + def writeXML(self, filePath): + """ + writes xml definition to given file + """ + f = open(filePath, 'w') + f.write(self.toXML()) + f.close() + + @staticmethod + def parseConfigFile(path): + """ + parses given config file + """ + configFile = open(path, 'r') + try: + config = yaml.safe_load(configFile) + except Exception: + print "Bad network configuration file. exiting" + sys.exit(1) + + return config -- cgit 1.2.3-korg