backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / lib / test_ofctl_string.py
1 # Copyright (C) 2017 Nippon Telegraph and Telephone Corporation.
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import unittest
17
18 from ryu.lib import ofctl_string
19 from ryu.ofproto import ofproto_parser
20 from ryu.ofproto.ofproto_protocol import ProtocolDesc
21 from ryu.ofproto import ofproto_v1_5
22
23
24 class Test_OfctlString(unittest.TestCase):
25     """Test cases for ryu.ofproto.ofp_instruction_from_str.
26     """
27
28     def __init__(self, methodName):
29         print('init %s' % methodName)
30         self.fake_dp_of15 = ProtocolDesc(ofproto_v1_5.OFP_VERSION)
31         self.maxDiff = None
32         super(Test_OfctlString, self).__init__(methodName)
33
34     def _test_str(self, dp, ofctl_str, *jsondict):
35         json = ofctl_string.ofp_instruction_from_str(
36             ofproto_v1_5, ofctl_str)
37         inst = ofproto_parser.ofp_instruction_from_jsondict(dp, json)
38         self.assertEqual(len(inst), len(jsondict))
39         for i in range(len(inst)):
40             self.assertEqual(jsondict[i], inst[i].to_jsondict())
41
42     def test_drop(self):
43         inst = ofctl_string.ofp_instruction_from_str(
44             ofproto_v1_5, 'drop')
45         self.assertEqual(inst, [])
46
47     def test_conjunction(self):
48         self._test_str(self.fake_dp_of15,
49                        'conjunction(0x234, 1/3),conjunction(0xdea, 2/2)',
50                        {'OFPInstructionActions': {
51                            'actions': [
52                                {'NXActionConjunction': {'clause': 0,
53                                                         'experimenter': 8992,
54                                                         'id': 0x234,
55                                                         'len': None,
56                                                         'n_clauses': 3,
57                                                         'subtype': 34,
58                                                         'type': 65535}},
59                                {'NXActionConjunction': {'clause': 1,
60                                                         'experimenter': 8992,
61                                                         'id': 0xdea,
62                                                         'len': None,
63                                                         'n_clauses': 2,
64                                                         'subtype': 34,
65                                                         'type': 65535}}],
66                            'type': 4}})
67
68     def test_ct(self):
69         self._test_str(self.fake_dp_of15,
70                        'ct(commit)',
71                        {'OFPInstructionActions': {
72                            'actions': [{'NXActionCT': {'actions': [],
73                                                        'alg': 0,
74                                                        'experimenter': 8992,
75                                                        'flags': 1,
76                                                        'len': None,
77                                                        'recirc_table': 255,
78                                                        'subtype': 35,
79                                                        'type': 65535,
80                                                        'zone_ofs_nbits': 0,
81                                                        'zone_src': u''}}],
82                            'type': 4}})
83
84     def test_ct_2(self):
85         self._test_str(self.fake_dp_of15,
86                        'ct(commit,zone=NXM_NX_REG8[0..15],'
87                        'exec(set_field:1->ct_mark))',
88                        {'OFPInstructionActions': {
89                            'actions': [{'NXActionCT': {
90                                'actions': [
91                                    {'OFPActionSetField': {
92                                        'field': {'OXMTlv': {'field': 'ct_mark',
93                                                             'mask': None,
94                                                             'value': 1}},
95                                        'len': 8,
96                                        'type': 25}}],
97                                'alg': 0,
98                                'experimenter': 8992,
99                                'flags': 1,
100                                'len': None,
101                                'recirc_table': 255,
102                                'subtype': 35,
103                                'type': 65535,
104                                'zone_ofs_nbits': 15,
105                                'zone_src': u'reg8'}}],
106                            'type': 4}})
107
108     def test_resubmit(self):
109         self._test_str(self.fake_dp_of15,
110                        'resubmit(,10)',
111                        {'OFPInstructionActions':
112                         {'actions': [{'NXActionResubmitTable': {
113                             'experimenter': 8992,
114                             'in_port': 65528,
115                             'len': None,
116                             'subtype': 14,
117                             'table_id': 10,
118                             'type': 65535}}],
119                          'type': 4}})
120
121     def test_set_field(self):
122         self._test_str(self.fake_dp_of15,
123                        'set_field:10/0xff->tun_id',
124                        {'OFPInstructionActions':
125                         {'actions': [{'OFPActionSetField': {
126                             'field': {'OXMTlv': {'field': 'tunnel_id',
127                                                  'mask': 255,
128                                                  'value': 10}},
129                             'len': 8,
130                             'type': 25}}],
131                          'type': 4}})
132
133     def test_pop_vlan(self):
134         self._test_str(self.fake_dp_of15,
135                        'pop_vlan',
136                        {'OFPInstructionActions':
137                         {'actions': [{'OFPActionPopVlan': {'len': 8,
138                                                            'type': 18}}],
139                          'type': 4}})
140
141     def test_multi(self):
142         self._test_str(self.fake_dp_of15,
143                        'pop_vlan,goto_table:33',
144                        {'OFPInstructionActions':
145                         {'actions': [{'OFPActionPopVlan': {'len': 8,
146                                                            'type': 18}}],
147                          'type': 4}},
148                        {'OFPInstructionGotoTable':
149                         {'len': 8,
150                          'table_id': 33,
151                          'type': 1}})
152
153     def test_multi_unordered(self):
154         self._test_str(self.fake_dp_of15,
155                        'pop_vlan,goto_table:33,output:1',
156                        {'OFPInstructionActions':
157                         {'actions': [{'OFPActionPopVlan': {'len': 8,
158                                                            'type': 18}},
159                                      {'OFPActionOutput': {'len': 16,
160                                                           'max_len': 65509,
161                                                           'port': 1,
162                                                           'type': 0}}],
163                          'type': 4}},
164                        {'OFPInstructionGotoTable':
165                         {'len': 8,
166                          'table_id': 33,
167                          'type': 1}})