Bundle Add Message and Request Forward Message, which is part of OpenFlow 1.4, encapsulates an OpenFlow message in side an OpenFlow message.
This patch prepares for this by adding a MsgInMsgBase class, a subclass of MsgBase which allows its subclasses to include subclasses of either MsgInMsgBase or MsgBase when when parsing classes from JSON: The MsgBase class does not allow this. This change has three parts: * Remove the assertion in ofproto_parser.py:StringifyMixin::cls_from_jsondict_key() that cls is not a subclass of MsgBase. * Pass **additional_args to various stringify.py:StringifyMixin decoder methods to make the datapath available when instantiating MsgBase subclasses. * Override _decode_value() in MsgInMsgBase to pass **additional_args to decoder. The method in the parent class, StringifyMixin, does not pass **additional_args. The effect is to pass a datapath argument if the class is a subclass of MsgInMsgBase but not if the class is a direct subclass of MsgBase. By only making messages which allow messages inside them subclasses of MsgInMsgBase this allows the datapath argument to be passed to the decoder if and only if needed. Signed-off-by: Simon Horman <[email protected]> --- v5 * No change v4 * Rework to add MsgInMsgBase class v3 * No change v2 * Update changelog to refer to Request Forward Message --- ryu/lib/stringify.py | 14 ++++++++------ ryu/ofproto/ofproto_parser.py | 10 +++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ryu/lib/stringify.py b/ryu/lib/stringify.py index ad771db..77de2b8 100644 --- a/ryu/lib/stringify.py +++ b/ryu/lib/stringify.py @@ -216,11 +216,11 @@ class StringifyMixin(object): return getattr(mod, k) @classmethod - def obj_from_jsondict(cls, jsondict): + def obj_from_jsondict(cls, jsondict, **additional_args): assert len(jsondict) == 1 for k, v in jsondict.iteritems(): obj_cls = cls.cls_from_jsondict_key(k) - return obj_cls.from_jsondict(v) + return obj_cls.from_jsondict(v, **additional_args) @classmethod def _get_decoder(cls, k, decode_string): @@ -230,19 +230,20 @@ class StringifyMixin(object): return cls._get_default_decoder(decode_string) @classmethod - def _decode_value(cls, k, json_value, decode_string=base64.b64decode): + def _decode_value(cls, k, json_value, decode_string=base64.b64decode, + **additional_args): return cls._get_decoder(k, decode_string)(json_value) @classmethod def _get_default_decoder(cls, decode_string): - def _decode(json_value): + def _decode(json_value, **additional_args): if isinstance(json_value, (bytes, unicode)): v = decode_string(json_value) elif isinstance(json_value, list): v = map(_decode, json_value) elif isinstance(json_value, dict): if cls._is_class(json_value): - v = cls.obj_from_jsondict(json_value) + v = cls.obj_from_jsondict(json_value, **additional_args) else: v = _mapdict(_decode, json_value) # XXXhack @@ -287,7 +288,8 @@ class StringifyMixin(object): additional_args (Optional) Additional kwargs for constructor. =============== ===================================================== """ - decode = lambda k, x: cls._decode_value(k, x, decode_string) + decode = lambda k, x: cls._decode_value(k, x, decode_string, + **additional_args) kwargs = cls._restore_args(_mapdict_kv(decode, dict_)) try: return cls(**dict(kwargs, **additional_args)) diff --git a/ryu/ofproto/ofproto_parser.py b/ryu/ofproto/ofproto_parser.py index 609d4d6..b8911bb 100644 --- a/ryu/ofproto/ofproto_parser.py +++ b/ryu/ofproto/ofproto_parser.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import base64 import collections import logging import struct @@ -115,7 +116,6 @@ class StringifyMixin(stringify.StringifyMixin): @classmethod def cls_from_jsondict_key(cls, k): obj_cls = super(StringifyMixin, cls).cls_from_jsondict_key(k) - assert not issubclass(obj_cls, MsgBase) return obj_cls @@ -204,6 +204,14 @@ class MsgBase(StringifyMixin): self._serialize_header() +class MsgInMsgBase(MsgBase): + @classmethod + def _decode_value(cls, k, json_value, decode_string=base64.b64decode, + **additional_args): + return cls._get_decoder(k, decode_string)(json_value, + **additional_args) + + def msg_pack_into(fmt, buf, offset, *args): if len(buf) < offset: buf += bytearray(offset - len(buf)) -- 1.8.5.2 ------------------------------------------------------------------------------ Learn Graph Databases - Download FREE O'Reilly Book "Graph Databases" is the definitive new guide to graph databases and their applications. Written by three acclaimed leaders in the field, this first edition is now available. Download your free book today! http://p.sf.net/sfu/13534_NeoTech _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel
