예제 #1
0
    def test_init(self):

        rte_min = BgpIpv4UnicastRoute('0.0.0.0/0')
        rte_max = BgpIpv4UnicastRoute('255.255.255.255/32')

        rte = BgpIpv4UnicastRoute('1.2.3.4')
        self.assertIs(rte.af, AddressFamily.ipv4_unicast)
        self.assertEqual(rte.type, rte.Type.ip)
        self.assertEqual(rte.ip, IPv4Address('1.2.3.4'))
        self.assertEqual(rte.ip_network, IPv4Network('1.2.3.4/32'))
        self.assertEqual(rte.prefix_length, 32)
        self.assertEqual(str(rte), '1.2.3.4/32')
        rte2 = BgpIpv4UnicastRoute(rte)
        self.assertEqual(rte, rte2)
        self.assertTrue(rte_min < rte < rte_max)
        self.assertFalse(rte != rte2)

        rte = BgpIpv4UnicastRoute('1.2.3.0/24')
        self.assertIs(rte.af, AddressFamily.ipv4_unicast)
        self.assertEqual(rte.type, rte.Type.ip)
        self.assertEqual(rte.ip, IPv4Address('1.2.3.0'))
        self.assertEqual(rte.prefix_length, 24)
        self.assertEqual(rte.ip_network, IPv4Network('1.2.3.0/24'))
        self.assertEqual(str(rte), '1.2.3.0/24')
        rte2 = BgpIpv4UnicastRoute(rte)
        self.assertEqual(rte, rte2)
        self.assertTrue(rte_min < rte < rte_max)
        self.assertFalse(rte != rte2)
예제 #2
0
    def __init__(self, value=None, **kwargs):
        if value is None:
            if 'type' not in kwargs:
                raise TypeError('type argument mandatory.')
            for attr in (
                    'type',
                    'prefix_length',
                    'ip',
            ):
                v = kwargs.pop(attr, None)
                if v is not None:
                    setattr(self, attr, v)
            if kwargs:
                raise TypeError('Unexpected keyword arguments: {}'\
                                .format(', '.join(kwargs.keys())))
            return

        if kwargs:
            raise TypeError('Provide either value or kwargs, not both.')

        if isinstance(value, BgpIpv4UnicastRoute):
            # Copy constructor
            for attr in (
                    'type',
                    'prefix_length',
                    'ip',
            ):
                v = getattr(value, attr)
                if v is not None:
                    setattr(self, attr, v)
            return

        if isinstance(value, (str, IPv4Address, IPv4Network)):
            # '1.2.3.4', '1.2.3.0/24'
            self.ip_network = IPv4Network(value)
            self.type = BgpIpv4UnicastRouteType.ip
            return

        raise TypeError(value)
예제 #3
0
def get_duplicated_interface_ip(ops):
    '''Get interface A which has ipv4 address, and find another interface B'''
    # initial return values
    duplicated_ip = None
    duplicated_iprefix_length = None
    ip_intf = None
    duplicated_intf = None
    network_addr = None

    intfs = list(ops.info.keys())
    # find any 'up' interface
    for intf in ops.info:
        for ipv4, item in ops.info[intf].get('ipv4', {}).items():
            # choose one different interfaces
            diff_intfs = intfs.copy()
            diff_intfs.remove(intf)
            if not diff_intfs:
                # no diff_intf found, find the next item
                continue

            # store the ip and interface lists for next section
            duplicated_ip = item['ip']
            duplicated_iprefix_length = item['prefix_length']

            if duplicated_iprefix_length == '32':
                # looks for loopback interfaces
                duplicated_intfs = [i for i in diff_intfs if 'Loopback' in i]
                if not duplicated_intfs:
                    continue
            else:
                # choose one non-loopback interface
                duplicated_intfs = [
                    i for i in diff_intfs if 'Loopback' not in i
                ]
                if not duplicated_intfs:
                    continue

            # get interface from same vrf
            for interface in duplicated_intfs:
                try:
                    assert ops.info[intf].get(
                        'vrf') == ops.info[interface].get('vrf')
                except Exception:
                    continue
                else:
                    duplicated_intf = interface
                    break

            # store interface to accurate the error message
            ip_intf = intf
            # store network address to accurate the error message
            network_addr = IPv4Network(ipv4, False).network_address
            # print out the message
            log.info(
                banner("The ip address {ip} is collected from {o_intf}".format(
                    ip=ipv4, o_intf=intf),
                       align='left'))
        if duplicated_intf:
            break

    # return the values
    return duplicated_ip, duplicated_iprefix_length, ip_intf, duplicated_intf, network_addr
예제 #4
0
 def ip_network(self, value):
     ip_network = IPv4Network(value)
     self.ip = ip_network.network_address
     self.prefix_length = ip_network.prefixlen
예제 #5
0
 def ip_network(self):
     return IPv4Network('{}/{}'.format(self.ip, self.prefix_length))