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 crudely allowing MsgBase subclasses to be used anywhere when parsing classes from JSON: previously they were only allowed at the top-level. This change has three parts: * Gratuitously 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. * Gratuitously try to decode JSON without **additional_args and then if that raises an exception fall back to decoding with **additional_args. The assumption is that the first pass will fail for subclasses of MsgBase and the second pass will work such cases. Signed-off-by: Simon Horman <[email protected]> --- v3 * No change v2 * Update changelog to refer to Request Forward Message --- ryu/lib/stringify.py | 20 +++++++++++++------- ryu/ofproto/ofproto_parser.py | 1 - 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/ryu/lib/stringify.py b/ryu/lib/stringify.py index ad771db..4c01168 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,24 @@ class StringifyMixin(object): return cls._get_default_decoder(decode_string) @classmethod - def _decode_value(cls, k, json_value, decode_string=base64.b64decode): - return cls._get_decoder(k, decode_string)(json_value) + def _decode_value(cls, k, json_value, decode_string=base64.b64decode, + **additional_args): + try: + return cls._get_decoder(k, decode_string)(json_value) + except TypeError: + return cls._get_decoder(k, decode_string)(json_value, + **additional_args) @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 +292,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..a95f9a4 100644 --- a/ryu/ofproto/ofproto_parser.py +++ b/ryu/ofproto/ofproto_parser.py @@ -115,7 +115,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 -- 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
