mirror of
https://github.com/faucetsdn/ryu.git
synced 2026-05-08 05:46:10 +02:00
ofproto: change the way __str__ is implemented for of-wire messages
instead of explicit listing of of-wire attributes, use a heuristics to exclude internal attributes. (eg. buf, datapath, etc) this commit changes __str__ outputs. update a test case accordingly. Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
parent
359efd73aa
commit
edd7048be2
@ -16,6 +16,7 @@
|
||||
|
||||
import logging
|
||||
import struct
|
||||
import functools
|
||||
|
||||
from ryu import exception
|
||||
|
||||
@ -50,7 +51,17 @@ def msg(datapath, version, msg_type, msg_len, xid, buf):
|
||||
return msg_parser(datapath, version, msg_type, msg_len, xid, buf)
|
||||
|
||||
|
||||
def create_list_of_attributes(f):
|
||||
@functools.wraps(f)
|
||||
def wrapper(self, *args, **kwargs):
|
||||
ret = f(self, *args, **kwargs)
|
||||
self._attributes = set(dir(self))
|
||||
return ret
|
||||
return wrapper
|
||||
|
||||
|
||||
class MsgBase(object):
|
||||
@create_list_of_attributes
|
||||
def __init__(self, datapath):
|
||||
self.datapath = datapath
|
||||
self.version = None
|
||||
@ -75,9 +86,10 @@ class MsgBase(object):
|
||||
self.buf = buffer(buf)
|
||||
|
||||
def __str__(self):
|
||||
return 'version: 0x%x msg_type 0x%x xid 0x%x' % (self.version,
|
||||
self.msg_type,
|
||||
self.xid)
|
||||
buf = 'version: 0x%x msg_type 0x%x xid 0x%x' % (self.version,
|
||||
self.msg_type,
|
||||
self.xid)
|
||||
return msg_str_attr(self, buf)
|
||||
|
||||
@classmethod
|
||||
def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
|
||||
@ -135,7 +147,14 @@ def msg_pack_into(fmt, buf, offset, *args):
|
||||
struct.pack_into(fmt, buf, offset, *args)
|
||||
|
||||
|
||||
def msg_str_attr(msg, buf, attr_list):
|
||||
def msg_str_attr(msg, buf, attr_list=None):
|
||||
if attr_list is None:
|
||||
exclude = ['_attributes']
|
||||
try:
|
||||
exclude += msg._attributes
|
||||
except AttributeError:
|
||||
pass
|
||||
attr_list = set(dir(msg)) - set(exclude)
|
||||
for attr in attr_list:
|
||||
val = getattr(msg, attr, None)
|
||||
if val is not None:
|
||||
|
||||
@ -1620,12 +1620,6 @@ class OFPSwitchFeatures(MsgBase):
|
||||
def __init__(self, datapath):
|
||||
super(OFPSwitchFeatures, self).__init__(datapath)
|
||||
|
||||
def __str__(self):
|
||||
buf = super(OFPSwitchFeatures, self).__str__() + ' port'
|
||||
for _port_no, p in getattr(self, 'ports', {}).items():
|
||||
buf += ' ' + str(p)
|
||||
return buf
|
||||
|
||||
@classmethod
|
||||
def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
|
||||
msg = super(OFPSwitchFeatures, cls).parser(datapath, version, msg_type,
|
||||
@ -1675,11 +1669,6 @@ class OFPPacketIn(MsgBase):
|
||||
def __init__(self, datapath):
|
||||
super(OFPPacketIn, self).__init__(datapath)
|
||||
|
||||
def __str__(self):
|
||||
buf = super(OFPPacketIn, self).__str__()
|
||||
return msg_str_attr(self, buf,
|
||||
('buffer_id', 'total_len', 'in_port', 'reason'))
|
||||
|
||||
@classmethod
|
||||
def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
|
||||
msg = super(OFPPacketIn, cls).parser(datapath, version, msg_type,
|
||||
@ -1726,13 +1715,6 @@ class OFPFlowRemoved(MsgBase):
|
||||
def __init__(self, datapath):
|
||||
super(OFPFlowRemoved, self).__init__(datapath)
|
||||
|
||||
def __str__(self):
|
||||
buf = super(OFPFlowRemoved, self).__str__()
|
||||
return msg_str_attr(self, buf,
|
||||
('match', 'cookie', 'priority', 'reason',
|
||||
'duration_sec', 'duration_nsec',
|
||||
'idle_timeout', 'packet_count', 'byte_count'))
|
||||
|
||||
@classmethod
|
||||
def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
|
||||
msg = super(OFPFlowRemoved, cls).parser(datapath, version, msg_type,
|
||||
|
||||
@ -3560,7 +3560,7 @@ class TestOFPSwitchFeatures(unittest.TestCase):
|
||||
eq_(peer['val'], port.peer)
|
||||
|
||||
# test __str__()
|
||||
list_ = ('version:', 'msg_type', 'xid', 'port')
|
||||
list_ = ('version:', 'msg_type', 'xid', 'ports')
|
||||
check = {}
|
||||
str_ = str(res)
|
||||
str_ = str_.rsplit()
|
||||
@ -3568,13 +3568,16 @@ class TestOFPSwitchFeatures(unittest.TestCase):
|
||||
i = 0
|
||||
for s in str_:
|
||||
if s in list_:
|
||||
check[str_[i]] = str_[i + 1]
|
||||
if str_[i + 1].startswith('{'): # "{1: OFPPhyPort..."
|
||||
check[str_[i]] = str_[i + 2]
|
||||
else:
|
||||
check[str_[i]] = str_[i + 1]
|
||||
i += 1
|
||||
|
||||
eq_(hex(version['val']).find(check['version:']), 0)
|
||||
eq_(hex(msg_type['val']).find(check['msg_type']), 0)
|
||||
eq_(hex(xid['val']).find(check['xid']), 0)
|
||||
eq_(check['port'].find('OFPPhyPort'), 0)
|
||||
eq_(check['ports'].find('OFPPhyPort'), 0)
|
||||
|
||||
def test_serialize(self):
|
||||
# Not used.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user