Esempio n. 1
0
def generate_address(debug, network, vlan):
    """
    If required generate an address
    """

    # Extract the already allocated addresses
    addrs = []
    for i in vlan:
	if "" != i[2]:
	    addrs += [i[2]]

    if 1 == debug:
	print "generate_address: ", addrs

    #
    # Make sure the addresses are unique
    #
    from xtutils import unique
    unique("address", addrs)
    
    def incr(a):
        """
        Increment the x.x.x.x style address
        """

        import socket
        import struct

        n = struct.unpack("i", socket.inet_aton(a))[0]
        n = socket.ntohl(n)
        n += 1
        n = socket.htonl(n)
        a = socket.inet_ntoa(struct.pack("i", n))

        return a

    address = network
    for i in range(len(vlan)):
	if "" == vlan[i][2]:
	    while 1:
		address = incr(address)
		for a in addrs:
		    if a == address:
			break
		else:
		    break
	    vlan[i][2] = address
            addrs += [address]

    return vlan
Esempio n. 2
0
def generate_address(debug, network, vlan):
    """
    If required generate an address
    """

    # Extract the already allocated addresses
    addrs = []
    for i in vlan:
        if "" != i[2]:
            addrs += [i[2]]

    if 1 == debug:
        print "generate_address: ", addrs

    #
    # Make sure the addresses are unique
    #
    from xtutils import unique
    unique("address", addrs)

    def incr(a):
        """
        Increment the x.x.x.x style address
        """

        import socket
        import struct

        n = struct.unpack("i", socket.inet_aton(a))[0]
        n = socket.ntohl(n)
        n += 1
        n = socket.htonl(n)
        a = socket.inet_ntoa(struct.pack("i", n))

        return a

    address = network
    for i in range(len(vlan)):
        if "" == vlan[i][2]:
            while 1:
                address = incr(address)
                for a in addrs:
                    if a == address:
                        break
                else:
                    break
            vlan[i][2] = address
            addrs += [address]

    return vlan
Esempio n. 3
0
    def start(self):
        # Before we start read in the <testbed_physical> file

	self.x = Xorp(self.physical, self.debug).start()

	parser = make_parser()
	parser.setFeature(feature_namespaces, 0)
	parser.setContentHandler(self)
	parser.parse(self.config)

        # Now do the uniqueness test
        unique("vlan", self.vlans)
        unique("network", self.network)
        unique("ports", self.ports)
        unique("ipv4 address", self.addresses)
Esempio n. 4
0
    def start(self):
        # Before we start read in the <testbed_physical> file

        self.x = Xorp(self.physical, self.debug).start()

        parser = make_parser()
        parser.setFeature(feature_namespaces, 0)
        parser.setContentHandler(self)
        parser.parse(self.config)

        # Now do the uniqueness test
        unique("vlan", self.vlans)
        unique("network", self.network)
        unique("ports", self.ports)
        unique("ipv4 address", self.addresses)
Esempio n. 5
0
def ifcheck(physical, debug):
    "Check <testbed_physical> against the ifconfig output"
    # First read in the <testbed_physical> file

    ports = ()
    macs = ()
    cable = ()
    for i in Xtphysical(physical, debug).start():
        if 1 == debug:
            print i
        # If there is valid mac address then check it
        if -1 != string.find(i["mac"], ":"):
            from xtifset import verify
            status, msg = verify(i["host"], i["vif"], i["mac"], i["port"])
            if 1 != status:
                print msg
            ports = ports + (i["port"],)
            macs = macs + (i["mac"],)
            cable = cable + (i["cable"],)
    unique("ports", ports)
    unique("mac", macs)
    unique("cable", cable)
Esempio n. 6
0
def ifcheck(physical, debug):
    "Check <testbed_physical> against the ifconfig output"
    # First read in the <testbed_physical> file

    ports = ()
    macs = ()
    cable = ()
    for i in Xtphysical(physical, debug).start():
        if 1 == debug:
            print i
        # If there is valid mac address then check it
        if -1 != string.find(i["mac"], ":"):
            from xtifset import verify
            status, msg = verify(i["host"], i["vif"], i["mac"], i["port"])
            if 1 != status:
                print msg
            ports = ports + (i["port"], )
            macs = macs + (i["mac"], )
            cable = cable + (i["cable"], )
    unique("ports", ports)
    unique("mac", macs)
    unique("cable", cable)
Esempio n. 7
0
def process(debug, physical, fancy):
    """
    Generate a <testbed_config> entry from the fancy config
    """
    
    vlans, kernels, noconfigs, router = parse(debug, physical, fancy)
    if 1 == debug:
        for i in vlans:
            print i
        for i in kernels:
            print i
        for i in noconfigs:
            print i
        print router
        
    # Verify that each vlan has a name. If a vlan doesn't have a name
    # generate one for it.
    for i in range(len(vlans)):
        if 1 == debug:
            print i, vlans[i]
        if "" == vlans[i][0][0]:
            vlans[i][0][0] = generate_vlan_name(debug, \
                                                [vn[0][0] for vn in vlans])

    #
    # Verify that the vlan names are unique
    #
    from xtutils import unique
    unique("vlan", [vn[0][0] for vn in vlans])

    #
    # For netmasks of the form x.x.x.x/n convert the prefix to a mask
    #
    for i in range(len(vlans)):
        if 1 == debug:
            print i, vlans[i]
        if "" != vlans[i][0][1]:
            # If there is a "/" generate a mask
            if -1 != string.find(vlans[i][0][1], "/"):
                vlans[i][0][1], vlans[i][0][2] = \
                                generate_mask(debug, vlans[i][0][1])

    # Each vlan requires a network number and netmask
    for i in range(len(vlans)):
        if 1 == debug:
            print i, vlans[i]
        if "" == vlans[i][0][1]:
            vlans[i][0][1], vlans[i][0][2] = \
                            generate_network(debug,[vn[0][1] for vn in vlans])

    # By this stage every entry should have a network number. It
    # may not however have a netmask so derive it from the network number
    for i in range(len(vlans)):
        if 1 == debug:
            print i, vlans[i]
        if "" == vlans[i][0][2]:
            vlans[i][0][2] = net2mask(debug, vlans[i][0][1])

    #
    # Verify that the network numbers are unique
    #
    unique("network", [vn[0][1] for vn in vlans])

    #
    # It is not necessary to specify an interface so try and allocate
    # unused interfaces.
    # 1) Generate the full list of interfaces that are available
    # 2) Knock out the used interfaces from the list
    # 3) Make the unused ones available

    vifs = []
    from xtxml import Xtphysical
    for i in Xtphysical(physical, debug).start():
        if i["use"] != "no":
            vifs = vifs + [[i["host"], i["vif"]]]

    for i in vlans:
        for vlan in i[1:]:
            if 1 == debug:
                print vlan
            if "" != vlan[1]:
                for i in range(len(vifs)):
                    if vlan[0] == vifs[i][0] and vlan[1] == vifs[i][1]:
                        vifs[i][0] = vifs[i][1] = ""
                        break
                else:
                    error = \
"%s.%s Is either not available or multiple use has been attempted" % \
(vlan[0], vlan[1])
                    raise MyError, error

    for i in range(len(vlans)):
        for j in range(1, len(vlans[i][1:]) + 1):
            if 1 == debug:
                print i, j, vlans[i][j]
            if "" == vlans[i][j][1]:
                vifs, vlans[i][j][1] = generatevif(debug, vifs, vlans[i][j][0])

    #
    # We may need to generate addresses
    #
    for i in range(len(vlans)):
	vlans[i] = [vlans[i][0]] + \
		   [generate_address(debug, vlans[i][0][1], vlans[i][1:])]


    #
    # Generate the XML that can be included in <testbed_config>
    #
    for i in vlans:
        if 1 == debug:
            print i
        v = i[0]
        print '<vlan name="%s" ipv4="%s" mask="%s">' % (v[0], v[1], v[2])
        for h in i[1:][0]:
            for p in Xtphysical(physical, debug).start():
                if h[0] == p["host"] and h[1] == p["vif"]:
                    mac = p["mac"]
                    port = p["port"]

            print '\t<host name="%s">' % h[0]
            print '\t\t<ipv4 name="%s" mac="%s" port="%s">' % \
                  (h[1], mac, port)
            print '\t\t\t%s' % h[2]
            print '\t\t</ipv4>'
            print '\t</host>'
        print '</vlan>'

    #
    # Generate the kernel location XML
    #
    for i in kernels:
        if 1 == debug:
            print i
        print '<kernel>'
        print '\t<src>', i[0], '</src>'
        print '\t<dst>', i[1], '</dst>'
        print '</kernel>'

    #
    # Generate the list of hosts that should not be configured.
    #
    for i in noconfigs:
        if 1 == debug:
            print i
        print '<noconfig>'
        print i
        print '</noconfig>'

    #
    # The router host if present
    #
    if "" != router:
        print '<router>'
        print router
        print '</router>'
Esempio n. 8
0
def process(debug, physical, fancy):
    """
    Generate a <testbed_config> entry from the fancy config
    """

    vlans, kernels, noconfigs, router = parse(debug, physical, fancy)
    if 1 == debug:
        for i in vlans:
            print i
        for i in kernels:
            print i
        for i in noconfigs:
            print i
        print router

    # Verify that each vlan has a name. If a vlan doesn't have a name
    # generate one for it.
    for i in range(len(vlans)):
        if 1 == debug:
            print i, vlans[i]
        if "" == vlans[i][0][0]:
            vlans[i][0][0] = generate_vlan_name(debug, \
                                                [vn[0][0] for vn in vlans])

    #
    # Verify that the vlan names are unique
    #
    from xtutils import unique
    unique("vlan", [vn[0][0] for vn in vlans])

    #
    # For netmasks of the form x.x.x.x/n convert the prefix to a mask
    #
    for i in range(len(vlans)):
        if 1 == debug:
            print i, vlans[i]
        if "" != vlans[i][0][1]:
            # If there is a "/" generate a mask
            if -1 != string.find(vlans[i][0][1], "/"):
                vlans[i][0][1], vlans[i][0][2] = \
                                generate_mask(debug, vlans[i][0][1])

    # Each vlan requires a network number and netmask
    for i in range(len(vlans)):
        if 1 == debug:
            print i, vlans[i]
        if "" == vlans[i][0][1]:
            vlans[i][0][1], vlans[i][0][2] = \
                            generate_network(debug,[vn[0][1] for vn in vlans])

    # By this stage every entry should have a network number. It
    # may not however have a netmask so derive it from the network number
    for i in range(len(vlans)):
        if 1 == debug:
            print i, vlans[i]
        if "" == vlans[i][0][2]:
            vlans[i][0][2] = net2mask(debug, vlans[i][0][1])

    #
    # Verify that the network numbers are unique
    #
    unique("network", [vn[0][1] for vn in vlans])

    #
    # It is not necessary to specify an interface so try and allocate
    # unused interfaces.
    # 1) Generate the full list of interfaces that are available
    # 2) Knock out the used interfaces from the list
    # 3) Make the unused ones available

    vifs = []
    from xtxml import Xtphysical
    for i in Xtphysical(physical, debug).start():
        if i["use"] != "no":
            vifs = vifs + [[i["host"], i["vif"]]]

    for i in vlans:
        for vlan in i[1:]:
            if 1 == debug:
                print vlan
            if "" != vlan[1]:
                for i in range(len(vifs)):
                    if vlan[0] == vifs[i][0] and vlan[1] == vifs[i][1]:
                        vifs[i][0] = vifs[i][1] = ""
                        break
                else:
                    error = \
"%s.%s Is either not available or multiple use has been attempted" % \
(vlan[0], vlan[1])
                    raise MyError, error

    for i in range(len(vlans)):
        for j in range(1, len(vlans[i][1:]) + 1):
            if 1 == debug:
                print i, j, vlans[i][j]
            if "" == vlans[i][j][1]:
                vifs, vlans[i][j][1] = generatevif(debug, vifs, vlans[i][j][0])

    #
    # We may need to generate addresses
    #
    for i in range(len(vlans)):
        vlans[i] = [vlans[i][0]] + \
            [generate_address(debug, vlans[i][0][1], vlans[i][1:])]

    #
    # Generate the XML that can be included in <testbed_config>
    #
    for i in vlans:
        if 1 == debug:
            print i
        v = i[0]
        print '<vlan name="%s" ipv4="%s" mask="%s">' % (v[0], v[1], v[2])
        for h in i[1:][0]:
            for p in Xtphysical(physical, debug).start():
                if h[0] == p["host"] and h[1] == p["vif"]:
                    mac = p["mac"]
                    port = p["port"]

            print '\t<host name="%s">' % h[0]
            print '\t\t<ipv4 name="%s" mac="%s" port="%s">' % \
                  (h[1], mac, port)
            print '\t\t\t%s' % h[2]
            print '\t\t</ipv4>'
            print '\t</host>'
        print '</vlan>'

    #
    # Generate the kernel location XML
    #
    for i in kernels:
        if 1 == debug:
            print i
        print '<kernel>'
        print '\t<src>', i[0], '</src>'
        print '\t<dst>', i[1], '</dst>'
        print '</kernel>'

    #
    # Generate the list of hosts that should not be configured.
    #
    for i in noconfigs:
        if 1 == debug:
            print i
        print '<noconfig>'
        print i
        print '</noconfig>'

    #
    # The router host if present
    #
    if "" != router:
        print '<router>'
        print router
        print '</router>'