Пример #1
0
    def test_link_delete_net_to_node(self, coreserver):
        node_one = 1
        coreserver.session.add_node(_id=node_one)
        switch = 2
        coreserver.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
        ip_prefix = Ipv4Prefix("10.0.0.0/24")
        interface_one = ip_prefix.addr(node_one)
        message = coreapi.CoreLinkMessage.create(
            MessageFlags.ADD.value,
            [
                (LinkTlvs.N1_NUMBER, node_one),
                (LinkTlvs.N2_NUMBER, switch),
                (LinkTlvs.INTERFACE1_NUMBER, 0),
                (LinkTlvs.INTERFACE1_IP4, interface_one),
                (LinkTlvs.INTERFACE1_IP4_MASK, 24),
            ],
        )
        coreserver.request_handler.handle_message(message)
        switch_node = coreserver.session.get_node(switch)
        all_links = switch_node.all_link_data(0)
        assert len(all_links) == 1

        message = coreapi.CoreLinkMessage.create(
            MessageFlags.DELETE.value,
            [
                (LinkTlvs.N1_NUMBER, switch),
                (LinkTlvs.N2_NUMBER, node_one),
                (LinkTlvs.INTERFACE2_NUMBER, 0),
            ],
        )
        coreserver.request_handler.handle_message(message)

        switch_node = coreserver.session.get_node(switch)
        all_links = switch_node.all_link_data(0)
        assert len(all_links) == 0
Пример #2
0
    def subnetentry(x):
        """
        Generate a subnet declaration block given an IPv4 prefix string
        for inclusion in the dhcpd3 config file.
        """
        if x.find(":") >= 0:
            return ""
        else:
            addr = x.split("/")[0]
            net = Ipv4Prefix(x)
            # divide the address space in half
            rangelow = net.addr(net.num_addr() / 2)
            rangehigh = net.max_addr()
            return """
subnet %s netmask %s {
  pool {
    range %s %s;
    default-lease-time 600;
    option routers %s;
  }
}
""" % (
                net.prefix_str(),
                net.netmask_str(),
                rangelow,
                rangehigh,
                addr,
            )
Пример #3
0
    def test_link_add_node_to_node(self, coreserver):
        node_one = 1
        coreserver.session.add_node(_id=node_one)
        node_two = 2
        coreserver.session.add_node(_id=node_two)
        ip_prefix = Ipv4Prefix("10.0.0.0/24")
        interface_one = ip_prefix.addr(node_one)
        interface_two = ip_prefix.addr(node_two)
        message = coreapi.CoreLinkMessage.create(
            MessageFlags.ADD.value,
            [
                (LinkTlvs.N1_NUMBER, node_one),
                (LinkTlvs.N2_NUMBER, node_two),
                (LinkTlvs.INTERFACE1_NUMBER, 0),
                (LinkTlvs.INTERFACE1_IP4, interface_one),
                (LinkTlvs.INTERFACE1_IP4_MASK, 24),
                (LinkTlvs.INTERFACE2_NUMBER, 0),
                (LinkTlvs.INTERFACE2_IP4, interface_two),
                (LinkTlvs.INTERFACE2_IP4_MASK, 24),
            ],
        )

        coreserver.request_handler.handle_message(message)

        all_links = []
        for node_id in coreserver.session.nodes:
            node = coreserver.session.nodes[node_id]
            all_links += node.all_link_data(0)
        assert len(all_links) == 1
Пример #4
0
    def test_link_update(self, coreserver):
        node_one = 1
        coreserver.session.add_node(_id=node_one)
        switch = 2
        coreserver.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
        ip_prefix = Ipv4Prefix("10.0.0.0/24")
        interface_one = ip_prefix.addr(node_one)
        message = coreapi.CoreLinkMessage.create(MessageFlags.ADD.value, [
            (LinkTlvs.N1_NUMBER, node_one),
            (LinkTlvs.N2_NUMBER, switch),
            (LinkTlvs.INTERFACE1_NUMBER, 0),
            (LinkTlvs.INTERFACE1_IP4, interface_one),
            (LinkTlvs.INTERFACE1_IP4_MASK, 24),
        ])
        coreserver.request_handler.handle_message(message)
        switch_node = coreserver.session.get_node(switch)
        all_links = switch_node.all_link_data(0)
        assert len(all_links) == 1
        link = all_links[0]
        assert link.bandwidth is None

        bandwidth = 50000
        message = coreapi.CoreLinkMessage.create(0, [
            (LinkTlvs.N1_NUMBER, node_one),
            (LinkTlvs.N2_NUMBER, switch),
            (LinkTlvs.INTERFACE1_NUMBER, 0),
            (LinkTlvs.BANDWIDTH, bandwidth),
        ])
        coreserver.request_handler.handle_message(message)

        switch_node = coreserver.session.get_node(switch)
        all_links = switch_node.all_link_data(0)
        assert len(all_links) == 1
        link = all_links[0]
        assert link.bandwidth == bandwidth
Пример #5
0
 def addrstr(x):
     if x.find(":") >= 0:
         net = Ipv6Prefix(x)
     else:
         net = Ipv4Prefix(x)
     if net.max_addr() == net.min_addr():
         return ""
     else:
         if os.uname()[0] == "Linux":
             rtcmd = "ip route add default via"
         else:
             raise Exception("unknown platform")
         return "%s %s" % (rtcmd, net.min_addr())
Пример #6
0
 def addrstr(x):
     addr = x.split("/")[0]
     if ipaddress.is_ipv6_address(addr):
         net = Ipv6Prefix(x)
     else:
         net = Ipv4Prefix(x)
     if net.max_addr() == net.min_addr():
         return ""
     else:
         if os.uname()[0] == "Linux":
             rtcmd = "ip route add default via"
         else:
             raise Exception("unknown platform")
         return "%s %s" % (rtcmd, net.min_addr())
Пример #7
0
 def routestr(x):
     if x.find(":") >= 0:
         net = Ipv6Prefix(x)
         dst = "3ffe:4::/64"
     else:
         net = Ipv4Prefix(x)
         dst = "10.9.8.0/24"
     if net.max_addr() == net.min_addr():
         return ""
     else:
         if os.uname()[0] == "Linux":
             rtcmd = "#/sbin/ip route add %s via" % dst
         else:
             raise Exception("unknown platform")
         return "%s %s" % (rtcmd, net.min_addr())
Пример #8
0
 def routestr(x):
     addr = x.split("/")[0]
     if ipaddress.is_ipv6_address(addr):
         net = Ipv6Prefix(x)
         dst = "3ffe:4::/64"
     else:
         net = Ipv4Prefix(x)
         dst = "10.9.8.0/24"
     if net.max_addr() == net.min_addr():
         return ""
     else:
         if os.uname()[0] == "Linux":
             rtcmd = "#/sbin/ip route add %s via" % dst
         else:
             raise Exception("unknown platform")
         return "%s %s" % (rtcmd, net.min_addr())
Пример #9
0
 def firstipv4prefix(node, prefixlen=24):
     """
     Similar to QuaggaService.routerid(). Helper to return the first IPv4
     prefix of a node, using the supplied prefix length. This ignores the
     interface's prefix length, so e.g. '/32' can turn into '/24'.
     """
     for ifc in node.netifs():
         if hasattr(ifc, "control") and ifc.control is True:
             continue
         for a in ifc.addrlist:
             a = a.split("/")[0]
             if ipaddress.is_ipv4_address(a):
                 pre = Ipv4Prefix("%s/%s" % (a, prefixlen))
                 return str(pre)
     # raise ValueError,  "no IPv4 address found"
     return "0.0.0.0/%s" % prefixlen
Пример #10
0
    def __init__(self, ip4_prefix=None, ip6_prefix=None):
        """
        Creates an InterfaceHelper object.

        :param str ip4_prefix: ip4 prefix to use for generation
        :param str ip6_prefix: ip6 prefix to use for generation
        :raises ValueError: when both ip4 and ip6 prefixes have not been provided
        """
        if not ip4_prefix and not ip6_prefix:
            raise ValueError("ip4 or ip6 must be provided")

        self.ip4 = None
        if ip4_prefix:
            self.ip4 = Ipv4Prefix(ip4_prefix)
        self.ip6 = None
        if ip6_prefix:
            self.ip6 = Ipv6Prefix(ip6_prefix)