diff --git a/ryu/lib/ofctl_v1_0.py b/ryu/lib/ofctl_v1_0.py index a72d9b79..1a2bf414 100644 --- a/ryu/lib/ofctl_v1_0.py +++ b/ryu/lib/ofctl_v1_0.py @@ -206,18 +206,46 @@ def to_match(dp, attrs): def match_to_str(m): - return {'dl_dst': haddr_to_str(m.dl_dst), - 'dl_src': haddr_to_str(m.dl_src), - 'dl_type': m.dl_type, - 'dl_vlan': m.dl_vlan, - 'dl_vlan_pcp': m.dl_vlan_pcp, - 'in_port': m.in_port, - 'nw_dst': nw_dst_to_str(m.wildcards, m.nw_dst), - 'nw_proto': m.nw_proto, - 'nw_tos': m.nw_tos, - 'nw_src': nw_src_to_str(m.wildcards, m.nw_src), - 'tp_src': m.tp_src, - 'tp_dst': m.tp_dst} + + match = {} + + if ~m.wildcards & ofproto_v1_0.OFPFW_IN_PORT: + match['in_port'] = m.in_port + + if ~m.wildcards & ofproto_v1_0.OFPFW_DL_SRC: + match['dl_src'] = haddr_to_str(m.dl_src) + + if ~m.wildcards & ofproto_v1_0.OFPFW_DL_DST: + match['dl_dst'] = haddr_to_str(m.dl_dst) + + if ~m.wildcards & ofproto_v1_0.OFPFW_DL_VLAN: + match['dl_vlan'] = m.dl_vlan + + if ~m.wildcards & ofproto_v1_0.OFPFW_DL_VLAN_PCP: + match['dl_vlan_pcp'] = m.dl_vlan_pcp + + if ~m.wildcards & ofproto_v1_0.OFPFW_DL_TYPE: + match['dl_type'] = m.dl_type + + if ~m.wildcards & ofproto_v1_0.OFPFW_NW_TOS: + match['nw_tos'] = m.nw_tos + + if ~m.wildcards & ofproto_v1_0.OFPFW_NW_PROTO: + match['nw_proto'] = m.nw_proto + + if ~m.wildcards & ofproto_v1_0.OFPFW_NW_SRC_ALL: + match['nw_src'] = nw_src_to_str(m.wildcards, m.nw_src) + + if ~m.wildcards & ofproto_v1_0.OFPFW_NW_DST_ALL: + match['nw_dst'] = nw_dst_to_str(m.wildcards, m.nw_dst) + + if ~m.wildcards & ofproto_v1_0.OFPFW_TP_SRC: + match['tp_src'] = m.tp_src + + if ~m.wildcards & ofproto_v1_0.OFPFW_TP_DST: + match['tp_dst'] = m.tp_dst + + return match def nw_src_to_str(wildcards, addr): diff --git a/ryu/tests/unit/lib/test_ofctl_v1_0.py b/ryu/tests/unit/lib/test_ofctl_v1_0.py new file mode 100644 index 00000000..e8c2762a --- /dev/null +++ b/ryu/tests/unit/lib/test_ofctl_v1_0.py @@ -0,0 +1,60 @@ +# Copyright (C) 2015 Nippon Telegraph and Telephone Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +import unittest +import logging +from nose.tools import * + +from ryu.lib import ofctl_v1_0 +from ryu.ofproto import ofproto_v1_0, ofproto_v1_0_parser +from ryu.ofproto import ofproto_protocol + +LOG = logging.getLogger('test_ofctl_v1_0') + + +class Test_ofctl_v1_0(unittest.TestCase): + + """ Test case for ofctl_v1_0 + """ + + def setUp(self): + self.dp = ofproto_protocol.ProtocolDesc( + version=ofproto_v1_0.OFP_VERSION) + self.attrs_list = [ + {"in_port": 3}, + {"dl_vlan": 3}, + {"dl_src": "11:11:11:11:11:11"}, + {"dl_dst": "11:11:11:11:11:12"}, + {"nw_tos": 16, "dl_type": 2048}, + {"nw_proto": 5, "dl_type": 2048}, + {"tp_src": 1, "nw_proto": 6, "dl_type": 2048}, + {"tp_dst": 2, "nw_proto": 6, "dl_type": 2048}, + {"nw_src": "192.168.1.5", "dl_type": 2048}, + {"nw_dst": "192.168.1.5/12", "dl_type": 2048}, + {"nw_dst": "192.168.1.5/1"}, + {"nw_dst": "192.168.1.5/12"}, + {"dl_vlan_pcp": 3} + ] + + def tearDown(self): + pass + + def test_match_to_str(self): + for attrs in self.attrs_list: + match = ofctl_v1_0.to_match(self.dp, attrs) + str = ofctl_v1_0.match_to_str(match) + eq_(attrs, str)