From 9e542b885110404fd5ac8bfd25f5c4c97adc933a Mon Sep 17 00:00:00 2001 From: Yusuke Iwase Date: Tue, 6 Oct 2015 16:49:27 +0900 Subject: [PATCH] of10: Add __contains__ method into OFPMatch 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 Signed-off-by: FUJITA Tomonori --- 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 4cac2626..c6cd9736 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 73d3409b..fd2687d4 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,