def test_construct_in_mul_eth_tag(self): data_hex = b'\x03\x11\x00\x01\xac\x10\x00\x01\x17\x10\x00\x00\x00\x64\x20\xc0\xa8\x00\x01' data_list = [{ 'type': bgp_cons.BGPNLRI_EVPN_INCLUSIVE_MULTICAST_ETHERNET_TAG, 'value': { 'rd': '172.16.0.1:5904', 'eth_tag_id': 100, 'ip': '192.168.0.1' } }] self.assertEqual(data_hex, EVPN.construct(data_list))
def test_construct_eth_segment(self): data_hex = b'\x04\x17\x00\x01\xac\x10\x00\x01\x17\x10\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x20\xc0\xa8\x00\x01' data_list = [{ 'type': bgp_cons.BGPNLRI_EVPN_ETHERNET_SEGMENT, 'value': { 'rd': '172.16.0.1:5904', 'esi': 0, 'ip': '192.168.0.1' } }] self.assertEqual(data_hex, EVPN.construct(data_list))
def test_construct_eth_auto_dis(self): data_hex = b'\x01\x19\x00\x01\x01\x01\x01\x01\x80\x63\x00\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x64\x00\x00\xa1' data_list = [{ 'type': bgp_cons.BGPNLRI_EVPN_ETHERNET_AUTO_DISCOVERY, 'value': { 'rd': '1.1.1.1:32867', 'esi': 0, 'eth_tag_id': 100, 'label': [10] } }] self.assertEqual(data_hex, EVPN.construct(data_list))
def test_construct_ip_route_prefix_v4(self): data_hex = b'\x05\x22\x00\x02\x00\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x01\x18\x01\x01\x01\x00\x01\x01\x01\x01\x00\x00\xa1' data_list = [{ 'type': 5, 'value': { 'esi': 0, 'eth_tag_id': 1, 'gateway': '1.1.1.1', 'label': [10], 'prefix': '1.1.1.0/24', 'rd': '65536:2'}}] self.assertEqual(data_hex, EVPN.construct(data_list))
def test_construct_mac_ip_adv(self): data_hex = b'\x02\x25\x00\x01\xac\x11\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x6c\x30\x00\x11\x22\x33\x44\x55\x20\x0b\x0b\x0b\x01\x00\x00\x00' data_list = [{ 'type': bgp_cons.BGPNLRI_EVPN_MAC_IP_ADVERTISEMENT, 'value': { 'rd': '172.17.0.3:2', 'mac': '00-11-22-33-44-55', 'eth_tag_id': 108, 'esi': 0, 'ip': '11.11.11.1', 'label': [0]} }] self.assertEqual(data_hex, EVPN.construct(data_list))
def test_construct_ip_route_prefix_v6(self): data_hex = b'\x05' \ b'\x3a' \ b'\x00\x02\x00\x01\x00\x00\x00\x02' \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x01' \ b'\x40' \ b'\x20\x01\x32\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01' \ b'\x20\x01\x32\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01' \ b'\x00\x00\xa1' data_list = [{ 'type': 5, 'value': { 'esi': 0, 'eth_tag_id': 1, 'gateway': '2001:3232::1', 'label': [10], 'prefix': '2001:3232::1/64', 'rd': '65536:2'}}] self.assertEqual(data_hex, EVPN.construct(data_list))
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)
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)
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)