To support query whether a match instance contains a specific field,
This patch adds __contains__ method into OFPMatch.

Example:
  >>> if 'nw_src' in match:
  ...     print match['nw_src']
  ...
  '192.168.0.1'

Signed-off-by: IWASE Yusuke <[email protected]>
---
 ryu/ofproto/ofproto_v1_0.py        | 2 ++
 ryu/ofproto/ofproto_v1_0_parser.py | 4 ++++
 2 files changed, 6 insertions(+)

diff --git a/ryu/ofproto/ofproto_v1_0.py b/ryu/ofproto/ofproto_v1_0.py
index 4cac262..c6cd973 100644
--- a/ryu/ofproto/ofproto_v1_0.py
+++ b/ryu/ofproto/ofproto_v1_0.py
@@ -265,10 +265,12 @@ OFPFW_TP_DST = 1 << 7       # TCP/UDP destination port.
 OFPFW_NW_SRC_SHIFT = 8
 OFPFW_NW_SRC_BITS = 6
 OFPFW_NW_SRC_MASK = ((1 << OFPFW_NW_SRC_BITS) - 1) << OFPFW_NW_SRC_SHIFT
+OFPFW_NW_SRC = OFPFW_NW_SRC_MASK  # IP source address (not in OF Spec).
 OFPFW_NW_SRC_ALL = 32 << OFPFW_NW_SRC_SHIFT
 OFPFW_NW_DST_SHIFT = 14
 OFPFW_NW_DST_BITS = 6
 OFPFW_NW_DST_MASK = ((1 << OFPFW_NW_DST_BITS) - 1) << OFPFW_NW_DST_SHIFT
+OFPFW_NW_DST = OFPFW_NW_DST_MASK  # IP destination address (not in OF Spec).
 OFPFW_NW_DST_ALL = 32 << OFPFW_NW_DST_SHIFT
 OFPFW_DL_VLAN_PCP = 1 << 20     # VLAN priority.
 OFPFW_NW_TOS = 1 << 21  # IP ToS (DSCP field, 6 bits).
diff --git a/ryu/ofproto/ofproto_v1_0_parser.py 
b/ryu/ofproto/ofproto_v1_0_parser.py
index 73d3409..fd2687d 100644
--- a/ryu/ofproto/ofproto_v1_0_parser.py
+++ b/ryu/ofproto/ofproto_v1_0_parser.py
@@ -241,6 +241,10 @@ class OFPMatch(StringifyMixin):
         else:
             raise KeyError(name)
 
+    def __contains__(self, name):
+        wc = getattr(ofproto, 'OFPFW_' + name.upper(), 0)
+        return ~self.wildcards & wc
+
     def serialize(self, buf, offset):
         msg_pack_into(ofproto.OFP_MATCH_PACK_STR, buf, offset,
                       self.wildcards, self.in_port, self.dl_src,
-- 
1.9.1


------------------------------------------------------------------------------
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to