old api:
    OFPActionSetField(MTInPort(OXM_OF_IN_PORT, 1))

new api:
    OFPActionSetField(in_port=1)

and make the json representation similar to OFPMatch as well.
an example:
          "OFPActionSetField": {
             "field": {
                "OXMTlv": {
                   "field": "vlan_vid",
                   "mask": null,
                   "value": 258
                }
             }
          }

Signed-off-by: YAMAMOTO Takashi <[email protected]>
---
 ryu/ofproto/ofproto_v1_3_parser.py | 93 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 89 insertions(+), 4 deletions(-)

diff --git a/ryu/ofproto/ofproto_v1_3_parser.py 
b/ryu/ofproto/ofproto_v1_3_parser.py
index 1461ff8..0d38dee 100644
--- a/ryu/ofproto/ofproto_v1_3_parser.py
+++ b/ryu/ofproto/ofproto_v1_3_parser.py
@@ -2146,20 +2146,54 @@ class OFPActionPopMpls(OFPAction):
 @OFPAction.register_action_type(ofproto_v1_3.OFPAT_SET_FIELD,
                                 ofproto_v1_3.OFP_ACTION_SET_FIELD_SIZE)
 class OFPActionSetField(OFPAction):
-    def __init__(self, field):
+    def __init__(self, field=None, **kwargs):
+        # old api
+        #   OFPActionSetField(field)
+        # new api
+        #   OFPActionSetField(eth_src="00:00:00:00:00")
         super(OFPActionSetField, self).__init__()
-        self.field = field
+        if isinstance(field, OFPMatchField):
+            # old api compat
+            assert len(kwargs) == 0
+            self.field = field
+        else:
+            # new api
+            assert len(kwargs) == 1
+            key = kwargs.keys()[0]
+            value = kwargs[key]
+            assert isinstance(key, (str, unicode))
+            assert not isinstance(value, tuple)  # no mask
+            self.key = key
+            self.value = value
 
     @classmethod
     def parser(cls, buf, offset):
         (type_, len_) = struct.unpack_from(
             ofproto_v1_3.OFP_ACTION_SET_FIELD_PACK_STR, buf, offset)
-        field = OFPMatchField.parser(buf, offset + 4)
-        action = cls(field)
+        (n, value, mask, _len) = ofproto_v1_3.oxm_parse(buf, offset + 4)
+        k, uv = ofproto_v1_3.oxm_to_user(n, value, mask)
+        action = cls(**{k: uv})
         action.len = len_
+
+        # old api compat
+        action.field = OFPMatchField.parser(buf, offset + 4)
+
         return action
 
     def serialize(self, buf, offset):
+        # old api compat
+        if self._composed_with_old_api():
+            return self.serialize_old(buf, offset)
+
+        n, value, mask = ofproto_v1_3.oxm_from_user(self.key, self.value)
+        len_ = ofproto_v1_3.oxm_serialize(n, value, mask, buf, offset + 4)
+        self.len = utils.round_up(4 + len_, 8)
+        msg_pack_into('!HH', buf, offset, self.type, self.len)
+        pad_len = self.len - len_
+        ofproto_parser.msg_pack_into("%dx" % pad_len, buf, offset + 4 + len_)
+
+    # XXX old api compat
+    def serialize_old(self, buf, offset):
         len_ = ofproto_v1_3.OFP_ACTION_SET_FIELD_SIZE + self.field.oxm_len()
         self.len = utils.round_up(len_, 8)
         pad_len = self.len - len_
@@ -2169,6 +2203,57 @@ class OFPActionSetField(OFPAction):
         offset += len_
         ofproto_parser.msg_pack_into("%dx" % pad_len, buf, offset)
 
+    # XXX old api compat
+    def _composed_with_old_api(self):
+        return not hasattr(self, 'value')
+
+    def to_jsondict(self):
+        # XXX old api compat
+        if self._composed_with_old_api():
+            # copy object first because serialize_old is destructive
+            o2 = OFPActionSetField(self.field)
+            # serialize and parse to fill new fields
+            buf = bytearray()
+            o2.serialize(buf, 0)
+            o = OFPActionSetField.parser(str(buf), 0)
+        else:
+            o = self
+        return {
+            self.__class__.__name__: {
+                'field': ofproto_v1_3.oxm_to_jsondict(self.key, self.value)
+            }
+        }
+
+    @classmethod
+    def from_jsondict(cls, dict_):
+        k, v = ofproto_v1_3.oxm_from_jsondict(dict_['field'])
+        o = OFPActionSetField(**{k: v})
+
+        # XXX old api compat
+        # serialize and parse to fill old attributes
+        buf = bytearray()
+        o.serialize(buf, 0)
+        return OFPActionSetField.parser(str(buf), 0)
+
+    # XXX old api compat
+    def __str__(self):
+        # XXX old api compat
+        if self._composed_with_old_api():
+            # copy object first because serialize_old is destructive
+            o2 = OFPActionSetField(self.field)
+            # serialize and parse to fill new fields
+            buf = bytearray()
+            o2.serialize(buf, 0)
+            o = OFPActionSetField.parser(str(buf), 0)
+        else:
+            o = self
+        return super(OFPActionSetField, o).__str__()
+
+    __repr__ = __str__
+
+    def stringify_attrs(self):
+        yield (self.key, self.value)
+
 
 @OFPAction.register_action_type(
     ofproto_v1_3.OFPAT_EXPERIMENTER,
-- 
1.8.3.1


------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead. 
Download for free and get started troubleshooting in minutes. 
http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to