1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 from __future__ import print_function
22 from nose.tools import eq_
23 from nose.tools import ok_
25 from ryu.ofproto import ofproto_v1_2
26 from ryu.ofproto import ofproto_v1_3
27 from ryu.ofproto import ofproto_v1_2_parser
28 from ryu.ofproto import ofproto_v1_3_parser
30 from ryu.lib import addrconv
31 from ryu.tests import test_lib
32 from struct import unpack
35 class Test_Parser_Compat(unittest.TestCase):
36 def __init__(self, methodName):
37 print('init %s' % methodName)
38 super(Test_Parser_Compat, self).__init__(methodName)
46 def _test(self, name, ofpp):
48 ofproto_v1_2_parser: ofproto_v1_2,
49 ofproto_v1_3_parser: ofproto_v1_3,
53 eth_src = 'aa:bb:cc:dd:ee:ff'
54 ipv4_src = '192.0.2.9'
55 ipv6_src = 'fe80::f00b:a4ff:feef:5d8f'
58 old_eth_src = addrconv.mac.text_to_bin(eth_src)
59 old_ipv4_src = unpack('!I', addrconv.ipv4.text_to_bin(ipv4_src))[0]
60 old_ipv6_src = list(unpack('!8H',
61 addrconv.ipv6.text_to_bin(ipv6_src)))
73 get_value = lambda m, t: get_field(m, t).value
75 eq_(get_value(o, ofpp.MTInPort), old_in_port)
76 eq_(get_value(o, ofpp.MTEthSrc), old_eth_src)
77 eq_(get_value(o, ofpp.MTIPV4Src), old_ipv4_src)
78 eq_(get_value(o, ofpp.MTIPv6Src), old_ipv6_src)
82 eq_(o['in_port'], in_port)
83 eq_(o['eth_src'], eth_src)
84 eq_(o['ipv4_src'], ipv4_src)
85 eq_(o['ipv6_src'], ipv6_src)
87 # ensure that old and new api produces the same thing
91 old.set_in_port(old_in_port)
92 old.set_dl_src(old_eth_src)
93 old.set_ipv4_src(old_ipv4_src)
94 old.set_ipv6_src(old_ipv6_src)
97 old.serialize(old_buf, 0)
99 # note: you can't inspect an object composed with the old set_XXX api
100 # before serialize().
103 # another variant of old api; originally it was intended to be
104 # internal but actually used in the field. eg. LINC l2_switch_v1_3.py
105 old2 = ofpp.OFPMatch()
106 old2.append_field(ofp.OXM_OF_IN_PORT, old_in_port)
107 old2.append_field(ofp.OXM_OF_ETH_SRC, old_eth_src)
108 old2.append_field(ofp.OXM_OF_IPV4_SRC, old_ipv4_src)
109 old2.append_field(ofp.OXM_OF_IPV6_SRC, old_ipv6_src)
112 old2_buf = bytearray()
113 old2.serialize(old2_buf, 0)
116 new = ofpp.OFPMatch(in_port=in_port, eth_src=eth_src,
117 ipv4_src=ipv4_src, ipv6_src=ipv6_src)
120 new_buf = bytearray()
121 new.serialize(new_buf, 0)
122 eq_(new_buf, old_buf)
123 eq_(new_buf, old2_buf)
125 old_jsondict = old.to_jsondict()
126 old2_jsondict = old2.to_jsondict()
127 new_jsondict = new.to_jsondict()
128 eq_(new_jsondict, old_jsondict)
129 eq_(new_jsondict, old2_jsondict)
131 eq_(str(new), str(old))
132 eq_(str(new), str(old2))
134 # a parsed object can be inspected by old and new api
136 check(ofpp.OFPMatch.parser(six.binary_type(new_buf), 0))
137 check(ofpp.OFPMatch.from_jsondict(list(new_jsondict.values())[0]))
144 ofpps = [ofproto_v1_2_parser, ofproto_v1_3_parser]
146 mod = ofpp.__name__.split('.')[-1]
147 method_name = 'test_' + mod + '_ofpmatch_compat'
149 def _run(self, name, ofpp):
150 print('processing %s ...' % name)
152 self._test(self, name, ofpp)
154 self._test(name, ofpp)
155 print('adding %s ...' % method_name)
156 f = functools.partial(_run, name=method_name,
158 test_lib.add_method(Test_Parser_Compat, method_name, f)