Ejemplo n.º 1
0
 def test_withdraw_construct(self):
     nlri_hex = b'\x98\x80\x00\x00\x00\x00\x00\x64\x00\x00\x00\x0c\x20\x10\x00\x00\x00\x12\x00' \
                b'\x04\x98\x80\x00\x00\x00\x00\x00\x64\x00\x00\x00\x0c\x20\x10\x00\x01\x00\x12\x00\x00'
     nlri_list = [
         {'rd': '100:12', 'prefix': '2010:0:12:4::/64'},
         {'rd': '100:12', 'prefix': '2010:1:12::/64'}
     ]
     self.assertEqual(nlri_hex, IPv6MPLSVPN.construct(value=nlri_list, iswithdraw=True))
Ejemplo n.º 2
0
 def test_update_construct(self):
     nlri_hex = b'\x98\x00\x03\x61\x00\x00\x00\x64\x00\x00\x00\x0c\x20\x10\x00\x00\x00\x12\x00\x04' \
                b'\x98\x00\x03\x71\x00\x00\x00\x64\x00\x00\x00\x0c\x20\x10\x00\x01\x00\x12\x00\x00'
     nlri_list = [
         {'label': [54], 'rd': '100:12', 'prefix': '2010:0:12:4::/64'},
         {'label': [55], 'rd': '100:12', 'prefix': '2010:1:12::/64'}
     ]
     self.assertEqual(nlri_hex, IPv6MPLSVPN.construct(value=nlri_list))
Ejemplo n.º 3
0
 def test_withdraw_construct(self):
     nlri_hex = b'\x98\x80\x00\x00\x00\x00\x00\x64\x00\x00\x00\x0c\x20\x10\x00\x00\x00\x12\x00' \
                b'\x04\x98\x80\x00\x00\x00\x00\x00\x64\x00\x00\x00\x0c\x20\x10\x00\x01\x00\x12\x00\x00'
     nlri_list = [{
         'rd': '100:12',
         'prefix': '2010:0:12:4::/64'
     }, {
         'rd': '100:12',
         'prefix': '2010:1:12::/64'
     }]
     self.assertEqual(
         nlri_hex, IPv6MPLSVPN.construct(value=nlri_list, iswithdraw=True))
Ejemplo n.º 4
0
 def test_update_construct(self):
     nlri_hex = b'\x98\x00\x03\x61\x00\x00\x00\x64\x00\x00\x00\x0c\x20\x10\x00\x00\x00\x12\x00\x04' \
                b'\x98\x00\x03\x71\x00\x00\x00\x64\x00\x00\x00\x0c\x20\x10\x00\x01\x00\x12\x00\x00'
     nlri_list = [{
         'label': [54],
         'rd': '100:12',
         'prefix': '2010:0:12:4::/64'
     }, {
         'label': [55],
         'rd': '100:12',
         'prefix': '2010:1:12::/64'
     }]
     self.assertEqual(nlri_hex, IPv6MPLSVPN.construct(value=nlri_list))
Ejemplo n.º 5
0
    def construct(cls, value):
        """Construct a attribute

        :param value: python dictionary
        {'afi_safi': (1,128),
         'nexthop': {},
         'nlri': []
        """
        afi, safi = value['afi_safi']
        if afi == afn.AFNUM_INET:
            if safi == safn.SAFNUM_LAB_VPNUNICAST:  # MPLS VPN
                nexthop_hex = cls.construct_mpls_vpn_nexthop(value['nexthop'])
                nlri_hex = IPv4MPLSVPN.construct(value['nlri'])
                attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) +\
                    struct.pack('!B', len(nexthop_hex)) + nexthop_hex + b'\x00' + nlri_hex
                return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                    + struct.pack('!B', len(attr_value)) + attr_value
            elif safi == safn.SAFNUM_FSPEC_RULE:  # BGP Flow spec
                try:
                    try:
                        nexthop = netaddr.IPAddress(value['nexthop']).packed
                    except netaddr.core.AddrFormatError:
                        nexthop = ''
                    nlri = IPv4FlowSpec.construct(value=value['nlri'])
                    if nlri:
                        attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + \
                            struct.pack('!B', len(nexthop)) + nexthop + b'\x00' + nlri
                        return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                            + struct.pack('!B', len(attr_value)) + attr_value
                except Exception as e:
                    raise excep.ConstructAttributeFailed(
                        reason='failed to construct attributes: %s' % e,
                        data=value)
            else:
                raise excep.ConstructAttributeFailed(
                    reason='unsupport this sub address family', data=value)

        # ipv6 unicast
        elif afi == afn.AFNUM_INET6:

            if safi == safn.SAFNUM_LAB_VPNUNICAST:  # MPLS VPN
                nexthop_hex = cls.construct_mpls_vpn_nexthop(value['nexthop'])
                nlri_hex = IPv6MPLSVPN.construct(value['nlri'])
                attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) +\
                    struct.pack('!B', len(nexthop_hex)) + nexthop_hex + b'\x00' + nlri_hex
                return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                    + struct.pack('!B', len(attr_value)) + attr_value

            elif safi == safn.SAFNUM_UNICAST:
                nexthop_len = 16
                nexthop_bin = netaddr.IPAddress(value['nexthop']).packed
                if value.get('linklocal_nexthop'):
                    nexthop_len *= 2
                    nexthop_bin += netaddr.IPAddress(
                        value['linklocal_nexthop']).packed

                nlri_bin = IPv6Unicast.construct(nlri_list=value['nlri'])

                attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + struct.pack('!B', nexthop_len) + \
                    nexthop_bin + b'\x00' + nlri_bin
                return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID)\
                    + struct.pack('!B', len(attr_value)) + attr_value
        # for l2vpn
        elif afi == afn.AFNUM_L2VPN:
            if safi == safn.SAFNUM_EVPN:
                nexthop_bin = netaddr.IPAddress(value['nexthop']).packed
                nlri_bin = EVPN.construct(nlri_list=value['nlri'])
                attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + struct.pack('!B', len(nexthop_bin)) + \
                    nexthop_bin + b'\x00' + nlri_bin
                return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID)\
                    + struct.pack('!B', len(attr_value)) + attr_value
        else:
            raise excep.ConstructAttributeFailed(
                reason='unsupport this sub address family', data=value)
Ejemplo n.º 6
0
    def construct(cls, value):

        """Construct a attribute

        :param value: python dictionary
        {'afi_safi': (1,128),
         'withdraw': []
        """
        afi, safi = value['afi_safi']
        if afi == afn.AFNUM_INET:
            if safi == safn.SAFNUM_LAB_VPNUNICAST:  # MPLS VPN
                nlri = IPv4MPLSVPN.construct(value['withdraw'], iswithdraw=True)
                if nlri:
                    attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri
                    return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                        + struct.pack('!B', len(attr_value)) + attr_value
                else:
                    return None
            elif safi == safn.SAFNUM_FSPEC_RULE:
                try:
                    nlri_list = value.get('withdraw') or []
                    if not nlri_list:
                        return None
                    nlri_hex = b''
                    for nlri in nlri_list:
                        nlri_hex += IPv4FlowSpec.construct(value=nlri)
                    attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri_hex
                    return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                        + struct.pack('!B', len(attr_value)) + attr_value

                except Exception:
                    raise excep.ConstructAttributeFailed(
                        reason='failed to construct attributes',
                        data=value
                    )
            else:
                raise excep.ConstructAttributeFailed(
                    reason='unsupport this sub address family',
                    data=value)
        elif afi == afn.AFNUM_INET6:
            if safi == safn.SAFNUM_UNICAST:
                nlri = IPv6Unicast.construct(nlri_list=value['withdraw'])
                if nlri:
                    attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri
                    return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                        + struct.pack('!B', len(attr_value)) + attr_value
            elif safi == safn.SAFNUM_LAB_VPNUNICAST:
                nlri = IPv6MPLSVPN.construct(value=value['withdraw'], iswithdraw=True)
                if nlri:
                    attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri
                    return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                        + struct.pack('!B', len(attr_value)) + attr_value
                else:
                    return None
        # for l2vpn
        elif afi == afn.AFNUM_L2VPN:
            # for evpn
            if safi == safn.SAFNUM_EVPN:
                nlri = EVPN.construct(nlri_list=value['withdraw'])
                if nlri:
                    attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri
                    return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                        + struct.pack('!B', len(attr_value)) + attr_value
            else:
                return None
        else:
            raise excep.ConstructAttributeFailed(
                reason='unsupport this sub address family',
                data=value)
Ejemplo n.º 7
0
    def construct(cls, value):

        """Construct a attribute

        :param value: python dictionary
        {'afi_safi': (1,128),
         'nexthop': {},
         'nlri': []
        """
        afi, safi = value['afi_safi']
        if afi == afn.AFNUM_INET:
            if safi == safn.SAFNUM_LAB_VPNUNICAST:  # MPLS VPN
                nexthop_hex = cls.construct_mpls_vpn_nexthop(value['nexthop'])
                nlri_hex = IPv4MPLSVPN.construct(value['nlri'])
                attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) +\
                    struct.pack('!B', len(nexthop_hex)) + nexthop_hex + b'\x00' + nlri_hex
                return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                    + struct.pack('!B', len(attr_value)) + attr_value
            elif safi == safn.SAFNUM_FSPEC_RULE:  # BGP Flow spec
                try:
                    try:
                        nexthop = netaddr.IPAddress(value['nexthop']).packed
                    except netaddr.core.AddrFormatError:
                        nexthop = ''
                    nlri_hex = b''
                    for nlri in value['nlri']:
                        nlri_hex += IPv4FlowSpec.construct(value=nlri)
                    if nlri_hex:
                        attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + \
                            struct.pack('!B', len(nexthop)) + nexthop + b'\x00' + nlri_hex
                        return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                            + struct.pack('!B', len(attr_value)) + attr_value
                except Exception as e:
                    raise excep.ConstructAttributeFailed(
                        reason='failed to construct attributes: %s' % e,
                        data=value
                    )
            else:
                raise excep.ConstructAttributeFailed(
                    reason='unsupport this sub address family',
                    data=value)

        # ipv6 unicast
        elif afi == afn.AFNUM_INET6:

            if safi == safn.SAFNUM_LAB_VPNUNICAST:  # MPLS VPN
                nexthop_hex = cls.construct_mpls_vpn_nexthop(value['nexthop'])
                nlri_hex = IPv6MPLSVPN.construct(value['nlri'])
                attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) +\
                    struct.pack('!B', len(nexthop_hex)) + nexthop_hex + b'\x00' + nlri_hex
                return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                    + struct.pack('!B', len(attr_value)) + attr_value

            elif safi == safn.SAFNUM_UNICAST:
                nexthop_len = 16
                nexthop_bin = netaddr.IPAddress(value['nexthop']).packed
                if value.get('linklocal_nexthop'):
                    nexthop_len *= 2
                    nexthop_bin += netaddr.IPAddress(value['linklocal_nexthop']).packed

                nlri_bin = IPv6Unicast.construct(nlri_list=value['nlri'])

                attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + struct.pack('!B', nexthop_len) + \
                    nexthop_bin + b'\x00' + nlri_bin
                return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID)\
                    + struct.pack('!B', len(attr_value)) + attr_value
        # for l2vpn
        elif afi == afn.AFNUM_L2VPN:
            if safi == safn.SAFNUM_EVPN:
                nexthop_bin = netaddr.IPAddress(value['nexthop']).packed
                nlri_bin = EVPN.construct(nlri_list=value['nlri'])
                attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + struct.pack('!B', len(nexthop_bin)) + \
                    nexthop_bin + b'\x00' + nlri_bin
                return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID)\
                    + struct.pack('!B', len(attr_value)) + attr_value
        else:
            raise excep.ConstructAttributeFailed(
                reason='unsupport this sub address family',
                data=value)
Ejemplo n.º 8
0
    def construct(cls, value):

        """Construct a attribute

        :param value: python dictionary
        {'afi_safi': (1,128),
         'withdraw': []
        """
        afi, safi = value['afi_safi']
        if afi == afn.AFNUM_INET:
            if safi == safn.SAFNUM_LAB_VPNUNICAST:  # MPLS VPN
                nlri = IPv4MPLSVPN.construct(value['withdraw'], iswithdraw=True)
                if nlri:
                    attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri
                    return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                        + struct.pack('!B', len(attr_value)) + attr_value
                else:
                    return None
            elif safi == safn.SAFNUM_FSPEC_RULE:
                try:
                    nlri_list = value.get('withdraw') or []
                    if not nlri_list:
                        return None
                    nlri_hex = b''
                    for nlri in nlri_list:
                        nlri_hex += IPv4FlowSpec.construct(value=nlri)
                    attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri_hex
                    return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                        + struct.pack('!B', len(attr_value)) + attr_value

                except Exception:
                    raise excep.ConstructAttributeFailed(
                        reason='failed to construct attributes',
                        data=value
                    )
            else:
                raise excep.ConstructAttributeFailed(
                    reason='unsupport this sub address family',
                    data=value)
        elif afi == afn.AFNUM_INET6:
            if safi == safn.SAFNUM_UNICAST:
                nlri = IPv6Unicast.construct(nlri_list=value['withdraw'])
                if nlri:
                    attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri
                    return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                        + struct.pack('!B', len(attr_value)) + attr_value
            elif safi == safn.SAFNUM_LAB_VPNUNICAST:
                nlri = IPv6MPLSVPN.construct(value=value['withdraw'], iswithdraw=True)
                if nlri:
                    attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri
                    return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                        + struct.pack('!B', len(attr_value)) + attr_value
                else:
                    return None
        # for l2vpn
        elif afi == afn.AFNUM_L2VPN:
            # for evpn
            if safi == safn.SAFNUM_EVPN:
                nlri = EVPN.construct(nlri_list=value['withdraw'])
                if nlri:
                    attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri
                    return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
                        + struct.pack('!B', len(attr_value)) + attr_value
            else:
                return None
        else:
            raise excep.ConstructAttributeFailed(
                reason='unsupport this sub address family',
                data=value)