packet lib: bgp: fix reversibility about json

although BGP is using internal classes, no class is registered into '_class_prefixes'.
therefore, from_jsondict() does not work correctly.
this patch makes from_jsondict() to work correctly by registering internal classes into '_class_prefixes'.

examination code:

    from ryu.lib.packet import bgp
    msg1 = bgp.BGPUpdate(withdrawn_routes=[bgp.BGPWithdrawnRoute(length=0, addr='192.168.0.1')])
    print msg1
    jsondict = msg1.to_jsondict()
    msg2 = bgp.BGPUpdate.from_jsondict(jsondict['BGPUpdate'])
    print msg2
    print str(msg1) == str(msg2)

before applying this patch:

    BGPUpdate(len=None,marker='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff',nlri=[],path_attributes=[],total_path_attribute_len=None,type=2,withdrawn_routes=[BGPWithdrawnRoute(addr='192.168.0.1',length=0)],withdrawn_routes_len=None)
    BGPUpdate(len=None,marker='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff',nlri=[],path_attributes=[],total_path_attribute_len=None,type=2,withdrawn_routes=[{'BGPWithdrawnRoute': {'length': 0, 'addr': '192.168.0.1'}}],withdrawn_routes_len=None)
    False

after applying this patch:

    BGPUpdate(len=None,marker='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff',nlri=[],path_attributes=[],total_path_attribute_len=None,type=2,withdrawn_routes=[BGPWithdrawnRoute(addr='192.168.0.1',length=0)],withdrawn_routes_len=None)
    BGPUpdate(len=None,marker='\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff',nlri=[],path_attributes=[],total_path_attribute_len=None,type=2,withdrawn_routes=[BGPWithdrawnRoute(addr='192.168.0.1',length=0)],withdrawn_routes_len=None)
    True

Signed-off-by: Yuichi Ito <ito.yuichi0@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
Yuichi Ito 2013-12-12 15:31:21 +09:00 committed by FUJITA Tomonori
parent 3f9a902198
commit 9b0c24c6df

View File

@ -668,6 +668,7 @@ class BGPPathAttributeCommunities(_PathAttribute):
@_PathAttribute.register_type(BGP_ATTR_TYPE_EXTENDED_COMMUNITIES)
class BGPPathAttributeExtendedCommunities(_PathAttribute):
_ATTR_FLAGS = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANSITIVE
_class_prefixes = ['BGP']
def __init__(self, communities,
flags=0, type_=None, length=None):
@ -799,6 +800,7 @@ class BGPUnknownExtendedCommunity(_ExtendedCommunity):
class BGPPathAttributeMpReachNLRI(_PathAttribute):
_VALUE_PACK_STR = '!HBB' # afi, safi, next hop len
_ATTR_FLAGS = BGP_ATTR_FLAG_OPTIONAL
_class_prefixes = ['_BinAddrPrefix']
def __init__(self, afi, safi, next_hop, nlri,
next_hop_len=0, reserved='\0',
@ -856,6 +858,7 @@ class BGPPathAttributeMpReachNLRI(_PathAttribute):
class BGPPathAttributeMpUnreachNLRI(_PathAttribute):
_VALUE_PACK_STR = '!HB' # afi, safi
_ATTR_FLAGS = BGP_ATTR_FLAG_OPTIONAL
_class_prefixes = ['_BinAddrPrefix']
def __init__(self, afi, safi, withdrawn_routes,
flags=0, type_=None, length=None):
@ -913,6 +916,7 @@ class BGPMessage(packet_base.PacketBase, _TypeDisp):
_HDR_PACK_STR = '!16sHB' # marker, len, type
_HDR_LEN = struct.calcsize(_HDR_PACK_STR)
_class_prefixes = ['BGP']
def __init__(self, type_, len_=None, marker=None):
if marker is None: