1 # Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # vim: tabstop=4 shiftwidth=4 softtabstop=4
23 from nose.tools import *
24 from ryu.ofproto.ofproto_v1_3_parser import *
25 from ryu.ofproto import ofproto_v1_3_parser
26 from ryu.ofproto import ofproto_v1_3
27 from ryu.ofproto import ofproto_protocol
28 from ryu.ofproto import ether
29 from ryu.ofproto.ofproto_parser import MsgBase
31 from ryu.lib import addrconv
33 LOG = logging.getLogger('test_ofproto_v13')
36 _Datapath = ofproto_protocol.ProtocolDesc(version=ofproto_v1_3.OFP_VERSION)
39 class TestOFPMatch(unittest.TestCase):
41 """ Test case for ofproto_v1_3_parser.OFPMatch
48 eq_(res._wc.vlan_vid_mask, 0)
51 eq_(res._flow.vlan_vid, 0)
53 def _test_serialize_and_parser(self, match, header, value, mask=None):
54 cls_ = OFPMatchField._FIELDS_HEADERS.get(header)
55 pack_str = cls_.pack_str.replace('!', '')
56 fmt = '!HHI' + pack_str
60 length = match.serialize(buf, 0)
62 if mask and len(buf) > calcsize(fmt):
65 res = list(unpack_from(fmt, six.binary_type(buf), 0)[3:])
66 if type(value) is list:
67 res_value = res[:calcsize(pack_str) // 2]
70 res_mask = res[calcsize(pack_str) // 2:]
73 res_value = res.pop(0)
74 if cls_.__name__ == 'MTVlanVid':
75 eq_(res_value, value | ofproto.OFPVID_PRESENT)
78 if mask and res and res[0]:
83 res = match.parser(six.binary_type(buf), 0)
84 eq_(res.type, ofproto.OFPMT_OXM)
85 eq_(res.fields[0].header, header)
86 eq_(res.fields[0].value, value)
87 if mask and res.fields[0].mask is not None:
88 eq_(res.fields[0].mask, mask)
91 jsondict = match.to_jsondict()
94 match2 = match.from_jsondict(jsondict["OFPMatch"])
96 match2.serialize(buf2, 0)
97 eq_(str(match), str(match2))
101 def _test_set_vlan_vid(self, vid, mask=None):
102 header = ofproto.OXM_OF_VLAN_VID
105 match.set_vlan_vid(vid)
107 header = ofproto.OXM_OF_VLAN_VID_W
108 match.set_vlan_vid_masked(vid, mask)
109 self._test_serialize_and_parser(match, header, vid, mask)
111 def _test_set_vlan_vid_none(self):
112 header = ofproto.OXM_OF_VLAN_VID
114 match.set_vlan_vid_none()
115 value = ofproto.OFPVID_NONE
116 cls_ = OFPMatchField._FIELDS_HEADERS.get(header)
117 pack_str = cls_.pack_str.replace('!', '')
118 fmt = '!HHI' + pack_str
122 length = match.serialize(buf, 0)
123 eq_(length, len(buf))
125 res = list(unpack_from(fmt, six.binary_type(buf), 0)[3:])
126 res_value = res.pop(0)
127 eq_(res_value, value)
130 res = match.parser(six.binary_type(buf), 0)
131 eq_(res.type, ofproto.OFPMT_OXM)
132 eq_(res.fields[0].header, header)
133 eq_(res.fields[0].value, value)
136 jsondict = match.to_jsondict()
139 match2 = match.from_jsondict(jsondict["OFPMatch"])
141 match2.serialize(buf2, 0)
142 eq_(str(match), str(match2))
145 def test_set_vlan_vid_mid(self):
146 self._test_set_vlan_vid(2047)
148 def test_set_vlan_vid_max(self):
149 self._test_set_vlan_vid(0xfff)
151 def test_set_vlan_vid_min(self):
152 self._test_set_vlan_vid(0)
154 def test_set_vlan_vid_masked_mid(self):
155 self._test_set_vlan_vid(2047, 0xf0f)
157 def test_set_vlan_vid_masked_max(self):
158 self._test_set_vlan_vid(2047, 0xfff)
160 def test_set_vlan_vid_masked_min(self):
161 self._test_set_vlan_vid(2047, 0)
163 def test_set_vlan_vid_none(self):
164 self._test_set_vlan_vid_none()