summaryrefslogtreecommitdiffstats
path: root/tools/laas-fog/source/network.py
blob: 234ba22e97ca12b5bb482bea13186854dae8af66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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("<network>\n</network>")
        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