Merge "Adds Libvirt Handler"
[pharos.git] / tools / laas-fog / source / network.py
1 """
2 #############################################################################
3 #Copyright 2017 Parker Berberian and others                                 #
4 #                                                                           #
5 #Licensed under the Apache License, Version 2.0 (the "License");            #
6 #you may not use this file except in compliance with the License.           #
7 #You may obtain a copy of the License at                                    #
8 #                                                                           #
9 #    http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                           #
11 #Unless required by applicable law or agreed to in writing, software        #
12 #distributed under the License is distributed on an "AS IS" BASIS,          #
13 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #See the License for the specific language governing permissions and        #
15 #limitations under the License.                                             #
16 #############################################################################
17 """
18
19 import sys
20 import xml.dom
21 import xml.dom.minidom
22 import yaml
23
24
25 class Network:
26     """
27     This class has a similar role as the Domain class.
28     This class will parse a config file and
29     write the xml definitions of those networks for libvirt.
30     """
31
32     def __init__(self, propertiesDict):
33         """
34         init. propertiesDict should be
35         one of the dictionaries returned by parseConfigFile
36         """
37         self.name = propertiesDict['name']
38         self.brName = propertiesDict['brName']
39         self.brAddr = propertiesDict['brAddr']
40         self.netmask = propertiesDict['netmask']
41         self.forward = propertiesDict['forward']
42         self.dhcp = propertiesDict['dhcp']
43         self.cidr = propertiesDict['cidr']
44
45     def toXML(self):
46         """
47         Takes the config of this network and writes a valid xml definition
48         for libvirt.
49         returns a string
50         """
51         definition = xml.dom.minidom.parseString("<network>\n</network>")
52         nameElem = definition.createElement('name')
53         nameElem.appendChild(definition.createTextNode(self.name))
54         definition.documentElement.appendChild(nameElem)
55
56         if self.forward['used']:
57             forwardElem = definition.createElement('forward')
58             forwardElem.setAttribute('mode', self.forward['type'])
59             definition.documentElement.appendChild(forwardElem)
60
61         bridgeElem = definition.createElement('bridge')
62         bridgeElem.setAttribute('name', self.brName)
63         bridgeElem.setAttribute('stp', 'on')
64         bridgeElem.setAttribute('delay', '5')
65         definition.documentElement.appendChild(bridgeElem)
66
67         ipElem = definition.createElement('ip')
68         ipElem.setAttribute('address', self.brAddr)
69         ipElem.setAttribute('netmask', self.netmask)
70         if self.dhcp['used']:
71             dhcpElem = definition.createElement('dhcp')
72             rangeElem = definition.createElement('range')
73             rangeElem.setAttribute('start', self.dhcp['rangeStart'])
74             rangeElem.setAttribute('end', self.dhcp['rangeEnd'])
75             dhcpElem.appendChild(rangeElem)
76             ipElem.appendChild(dhcpElem)
77
78         definition.documentElement.appendChild(ipElem)
79
80         self.xml = definition.toprettyxml()
81         return self.xml
82
83     def writeXML(self, filePath):
84         """
85         writes xml definition to given file
86         """
87         f = open(filePath, 'w')
88         f.write(self.toXML())
89         f.close()
90
91     @staticmethod
92     def parseConfigFile(path):
93         """
94         parses given config file
95         """
96         configFile = open(path, 'r')
97         try:
98             config = yaml.safe_load(configFile)
99         except Exception:
100             print "Bad network configuration file. exiting"
101             sys.exit(1)
102
103         return config