backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / ofproto / test_parser_compat.py
1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
3 #
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
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
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
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 from __future__ import print_function
18
19 import six
20 import sys
21 import unittest
22 from nose.tools import eq_
23 from nose.tools import ok_
24
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
29
30 from ryu.lib import addrconv
31 from ryu.tests import test_lib
32 from struct import unpack
33
34
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)
39
40     def setUp(self):
41         pass
42
43     def tearDown(self):
44         pass
45
46     def _test(self, name, ofpp):
47         ofp = {
48             ofproto_v1_2_parser: ofproto_v1_2,
49             ofproto_v1_3_parser: ofproto_v1_3,
50         }[ofpp]
51
52         in_port = 987654321
53         eth_src = 'aa:bb:cc:dd:ee:ff'
54         ipv4_src = '192.0.2.9'
55         ipv6_src = 'fe80::f00b:a4ff:feef:5d8f'
56
57         old_in_port = in_port
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)))
62
63         def check(o):
64             check_old(o)
65             check_new(o)
66
67         def check_old(o):
68             # old api
69             def get_field(m, t):
70                 for f in m.fields:
71                     if isinstance(f, t):
72                         return f
73             get_value = lambda m, t: get_field(m, t).value
74
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)
79
80         def check_new(o):
81             # new api
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)
86
87         # ensure that old and new api produces the same thing
88
89         # old api
90         old = ofpp.OFPMatch()
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)
95
96         old_buf = bytearray()
97         old.serialize(old_buf, 0)
98
99         # note: you can't inspect an object composed with the old set_XXX api
100         # before serialize().
101         check_old(old)
102
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)
110         check_old(old2)
111
112         old2_buf = bytearray()
113         old2.serialize(old2_buf, 0)
114
115         # new api
116         new = ofpp.OFPMatch(in_port=in_port, eth_src=eth_src,
117                             ipv4_src=ipv4_src, ipv6_src=ipv6_src)
118         check_new(new)
119
120         new_buf = bytearray()
121         new.serialize(new_buf, 0)
122         eq_(new_buf, old_buf)
123         eq_(new_buf, old2_buf)
124
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)
130
131         eq_(str(new), str(old))
132         eq_(str(new), str(old2))
133
134         # a parsed object can be inspected by old and new api
135
136         check(ofpp.OFPMatch.parser(six.binary_type(new_buf), 0))
137         check(ofpp.OFPMatch.from_jsondict(list(new_jsondict.values())[0]))
138
139
140 def _add_tests():
141     import functools
142     import itertools
143
144     ofpps = [ofproto_v1_2_parser, ofproto_v1_3_parser]
145     for ofpp in ofpps:
146         mod = ofpp.__name__.split('.')[-1]
147         method_name = 'test_' + mod + '_ofpmatch_compat'
148
149         def _run(self, name, ofpp):
150             print('processing %s ...' % name)
151             if six.PY3:
152                 self._test(self, name, ofpp)
153             else:
154                 self._test(name, ofpp)
155         print('adding %s ...' % method_name)
156         f = functools.partial(_run, name=method_name,
157                               ofpp=ofpp)
158         test_lib.add_method(Test_Parser_Compat, method_name, f)
159
160
161 _add_tests()