summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIWASE Yusuke <iwase.yusuke0@gmail.com>2016-11-25 17:02:53 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2016-12-10 22:00:22 +0900
commit5e8b6c69f26fa073fc91e4813b2c59e2683eb727 (patch)
tree9ae4067ae63fd2f2a00608bd40518c50a4cebf36
parent4a8c7245faccbb2064852fdc2e666eacf50526ca (diff)
type_desc: Define TypeDisp class for re-usability
Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/lib/packet/bgp.py56
-rw-r--r--ryu/lib/packet/bmp.py45
-rw-r--r--ryu/lib/packet/ospf.py57
-rw-r--r--ryu/lib/type_desc.py37
4 files changed, 64 insertions, 131 deletions
diff --git a/ryu/lib/packet/bgp.py b/ryu/lib/packet/bgp.py
index 1c79052e..34025ad8 100644
--- a/ryu/lib/packet/bgp.py
+++ b/ryu/lib/packet/bgp.py
@@ -42,6 +42,7 @@ from ryu.lib.packet import vxlan
from ryu.lib.packet import mpls
from ryu.lib import addrconv
from ryu.lib import type_desc
+from ryu.lib.type_desc import TypeDisp
from ryu.lib import ip
from ryu.lib.pack_utils import msg_pack_into
from ryu.utils import binary_str
@@ -192,43 +193,6 @@ class _Value(object):
return struct.pack(self._VALUE_PACK_STR, *args)
-class _TypeDisp(object):
- _TYPES = {}
- _REV_TYPES = None
- _UNKNOWN_TYPE = None
-
- @classmethod
- def register_unknown_type(cls):
- def _register_type(subcls):
- cls._UNKNOWN_TYPE = subcls
- return subcls
- return _register_type
-
- @classmethod
- def register_type(cls, type_):
- cls._TYPES = cls._TYPES.copy()
-
- def _register_type(subcls):
- cls._TYPES[type_] = subcls
- cls._REV_TYPES = None
- return subcls
- return _register_type
-
- @classmethod
- def _lookup_type(cls, type_):
- try:
- return cls._TYPES[type_]
- except KeyError:
- return cls._UNKNOWN_TYPE
-
- @classmethod
- def _rev_lookup_type(cls, targ_cls):
- if cls._REV_TYPES is None:
- rev = dict((v, k) for k, v in cls._TYPES.items())
- cls._REV_TYPES = rev
- return cls._REV_TYPES[targ_cls]
-
-
class BgpExc(Exception):
"""Base bgp exception."""
@@ -629,7 +593,7 @@ def pad(binary, len_):
return binary + b'\0' * (len_ - len(binary))
-class _RouteDistinguisher(StringifyMixin, _TypeDisp, _Value):
+class _RouteDistinguisher(StringifyMixin, TypeDisp, _Value):
_PACK_STR = '!H'
TWO_OCTET_AS = 0
IPV4_ADDRESS = 1
@@ -1039,7 +1003,7 @@ class LabelledVPNIP6AddrPrefix(_LabelledAddrPrefix, _VPNAddrPrefix,
return "%s:%s" % (self.route_dist, self.prefix)
-class EvpnEsi(StringifyMixin, _TypeDisp, _Value):
+class EvpnEsi(StringifyMixin, TypeDisp, _Value):
"""
Ethernet Segment Identifier
"""
@@ -1286,7 +1250,7 @@ class EvpnASBasedEsi(EvpnEsi):
self.local_disc = local_disc
-class EvpnNLRI(StringifyMixin, _TypeDisp):
+class EvpnNLRI(StringifyMixin, TypeDisp):
"""
BGP Network Layer Reachability Information (NLRI) for EVPN
"""
@@ -2122,7 +2086,7 @@ def _get_addr_class(afi, safi):
return _BinAddrPrefix
-class _OptParam(StringifyMixin, _TypeDisp, _Value):
+class _OptParam(StringifyMixin, TypeDisp, _Value):
_PACK_STR = '!BB' # type, length
def __init__(self, type_, value=None, length=None):
@@ -2169,7 +2133,7 @@ class BGPOptParamUnknown(_OptParam):
@_OptParam.register_type(BGP_OPT_CAPABILITY)
-class _OptParamCapability(_OptParam, _TypeDisp):
+class _OptParamCapability(_OptParam, TypeDisp):
_CAP_HDR_PACK_STR = '!BB'
def __init__(self, cap_code=None, cap_value=None, cap_length=None,
@@ -2338,7 +2302,7 @@ class BGPWithdrawnRoute(IPAddrPrefix):
pass
-class _PathAttribute(StringifyMixin, _TypeDisp, _Value):
+class _PathAttribute(StringifyMixin, TypeDisp, _Value):
_PACK_STR = '!BB' # flags, type
_PACK_STR_LEN = '!B' # length
_PACK_STR_EXT_LEN = '!H' # length w/ BGP_ATTR_FLAG_EXTENDED_LENGTH
@@ -2898,7 +2862,7 @@ class BGPPathAttributeExtendedCommunities(_PathAttribute):
return self._community_list(3)
-class _ExtendedCommunity(StringifyMixin, _TypeDisp, _Value):
+class _ExtendedCommunity(StringifyMixin, TypeDisp, _Value):
_PACK_STR = '!B7s' # type high (+ type low), value
_PACK_STR_SIZE = struct.calcsize(_PACK_STR)
_SUBTYPE_PACK_STR = '!B' # subtype
@@ -3549,7 +3513,7 @@ class BGPPathAttributePmsiTunnel(_PathAttribute):
return ins
-class _PmsiTunnelId(StringifyMixin, _TypeDisp):
+class _PmsiTunnelId(StringifyMixin, TypeDisp):
@classmethod
def parse(cls, tunnel_type, buf):
@@ -3616,7 +3580,7 @@ class BGPNLRI(IPAddrPrefix):
pass
-class BGPMessage(packet_base.PacketBase, _TypeDisp):
+class BGPMessage(packet_base.PacketBase, TypeDisp):
"""Base class for BGP-4 messages.
An instance has the following attributes at least.
diff --git a/ryu/lib/packet/bmp.py b/ryu/lib/packet/bmp.py
index b3a097c0..9d1cda3b 100644
--- a/ryu/lib/packet/bmp.py
+++ b/ryu/lib/packet/bmp.py
@@ -17,13 +17,15 @@
BGP Monitoring Protocol draft-ietf-grow-bmp-07
"""
-import six
import struct
+import six
+
+from ryu.lib import addrconv
from ryu.lib.packet import packet_base
from ryu.lib.packet import stream_parser
from ryu.lib.packet.bgp import BGPMessage
-from ryu.lib import addrconv
+from ryu.lib.type_desc import TypeDisp
VERSION = 3
@@ -66,44 +68,7 @@ BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION = 3
BMP_PEER_DOWN_REASON_REMOTE_NO_NOTIFICATION = 4
-class _TypeDisp(object):
- _TYPES = {}
- _REV_TYPES = None
- _UNKNOWN_TYPE = None
-
- @classmethod
- def register_unknown_type(cls):
- def _register_type(subcls):
- cls._UNKNOWN_TYPE = subcls
- return subcls
- return _register_type
-
- @classmethod
- def register_type(cls, type_):
- cls._TYPES = cls._TYPES.copy()
-
- def _register_type(subcls):
- cls._TYPES[type_] = subcls
- cls._REV_TYPES = None
- return subcls
- return _register_type
-
- @classmethod
- def _lookup_type(cls, type_):
- try:
- return cls._TYPES[type_]
- except KeyError:
- return cls._UNKNOWN_TYPE
-
- @classmethod
- def _rev_lookup_type(cls, targ_cls):
- if cls._REV_TYPES is None:
- rev = dict((v, k) for k, v in cls._TYPES.items())
- cls._REV_TYPES = rev
- return cls._REV_TYPES[targ_cls]
-
-
-class BMPMessage(packet_base.PacketBase, _TypeDisp):
+class BMPMessage(packet_base.PacketBase, TypeDisp):
"""Base class for BGP Monitoring Protocol messages.
An instance has the following attributes at least.
diff --git a/ryu/lib/packet/ospf.py b/ryu/lib/packet/ospf.py
index 464f089b..552be1fd 100644
--- a/ryu/lib/packet/ospf.py
+++ b/ryu/lib/packet/ospf.py
@@ -17,7 +17,7 @@
RFC 2328 OSPF version 2
"""
-import six
+import logging
import struct
try:
@@ -27,13 +27,17 @@ except ImportError:
# Python 2
pass
-from ryu.lib.stringify import StringifyMixin
+import six
+
+from ryu.lib import addrconv
from ryu.lib.packet import packet_base
from ryu.lib.packet import packet_utils
from ryu.lib.packet import stream_parser
+from ryu.lib.stringify import StringifyMixin
+from ryu.lib import type_desc
-from ryu.lib import addrconv
-import logging
+
+LOG = logging.getLogger(__name__)
_VERSION = 2
@@ -88,43 +92,6 @@ class InvalidChecksum(Exception):
pass
-class _TypeDisp(object):
- _TYPES = {}
- _REV_TYPES = None
- _UNKNOWN_TYPE = None
-
- @classmethod
- def register_unknown_type(cls):
- def _register_type(subcls):
- cls._UNKNOWN_TYPE = subcls
- return subcls
- return _register_type
-
- @classmethod
- def register_type(cls, type_):
- cls._TYPES = cls._TYPES.copy()
-
- def _register_type(subcls):
- cls._TYPES[type_] = subcls
- cls._REV_TYPES = None
- return subcls
- return _register_type
-
- @classmethod
- def _lookup_type(cls, type_):
- try:
- return cls._TYPES[type_]
- except KeyError:
- return cls._UNKNOWN_TYPE
-
- @classmethod
- def _rev_lookup_type(cls, targ_cls):
- if cls._REV_TYPES is None:
- rev = dict((v, k) for k, v in cls._TYPES.items())
- cls._REV_TYPES = rev
- return cls._REV_TYPES[targ_cls]
-
-
class LSAHeader(StringifyMixin):
_HDR_PACK_STR = '!HBB4s4sIHH'
_HDR_LEN = struct.calcsize(_HDR_PACK_STR)
@@ -189,7 +156,7 @@ class LSAHeader(StringifyMixin):
self.ls_seqnum, self.checksum, self.length))
-class LSA(_TypeDisp, StringifyMixin):
+class LSA(type_desc.TypeDisp, StringifyMixin):
def __init__(self, ls_age=0, options=0, type_=OSPF_UNKNOWN_LSA,
id_='0.0.0.0', adv_router='0.0.0.0', ls_seqnum=0,
checksum=0, length=0, opaque_type=OSPF_OPAQUE_TYPE_UNKNOWN,
@@ -468,7 +435,7 @@ class NSSAExternalLSA(LSA):
pass
-class ExtendedPrefixTLV(StringifyMixin, _TypeDisp):
+class ExtendedPrefixTLV(StringifyMixin, type_desc.TypeDisp):
pass
@@ -541,7 +508,7 @@ class PrefixSIDSubTLV(ExtendedPrefixTLV):
self.algorithm, 0, self.range_size, 0, self.index)
-class OpaqueBody(StringifyMixin, _TypeDisp):
+class OpaqueBody(StringifyMixin, type_desc.TypeDisp):
def __init__(self, tlvs=None):
tlvs = tlvs if tlvs else []
self.tlvs = tlvs
@@ -639,7 +606,7 @@ class ASOpaqueLSA(OpaqueLSA):
length, opaque_type, opaque_id)
-class OSPFMessage(packet_base.PacketBase, _TypeDisp):
+class OSPFMessage(packet_base.PacketBase, type_desc.TypeDisp):
"""Base class for OSPF version 2 messages.
"""
diff --git a/ryu/lib/type_desc.py b/ryu/lib/type_desc.py
index cb10b09b..3e4061a5 100644
--- a/ryu/lib/type_desc.py
+++ b/ryu/lib/type_desc.py
@@ -122,3 +122,40 @@ class UnknownType(TypeDescr):
return base64.b64encode(data)
from_user = staticmethod(base64.b64decode)
+
+
+class TypeDisp(object):
+ _TYPES = {}
+ _REV_TYPES = None
+ _UNKNOWN_TYPE = None
+
+ @classmethod
+ def register_unknown_type(cls):
+ def _register_type(subcls):
+ cls._UNKNOWN_TYPE = subcls
+ return subcls
+ return _register_type
+
+ @classmethod
+ def register_type(cls, type_):
+ cls._TYPES = cls._TYPES.copy()
+
+ def _register_type(subcls):
+ cls._TYPES[type_] = subcls
+ cls._REV_TYPES = None
+ return subcls
+ return _register_type
+
+ @classmethod
+ def _lookup_type(cls, type_):
+ try:
+ return cls._TYPES[type_]
+ except KeyError:
+ return cls._UNKNOWN_TYPE
+
+ @classmethod
+ def _rev_lookup_type(cls, targ_cls):
+ if cls._REV_TYPES is None:
+ rev = dict((v, k) for k, v in cls._TYPES.items())
+ cls._REV_TYPES = rev
+ return cls._REV_TYPES[targ_cls]