1 # Copyright (C) 2017 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.
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
24 class Test_OfctlString(unittest.TestCase):
25 """Test cases for ryu.ofproto.ofp_instruction_from_str.
28 def __init__(self, methodName):
29 print('init %s' % methodName)
30 self.fake_dp_of15 = ProtocolDesc(ofproto_v1_5.OFP_VERSION)
32 super(Test_OfctlString, self).__init__(methodName)
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())
43 inst = ofctl_string.ofp_instruction_from_str(
45 self.assertEqual(inst, [])
47 def test_conjunction(self):
48 self._test_str(self.fake_dp_of15,
49 'conjunction(0x234, 1/3),conjunction(0xdea, 2/2)',
50 {'OFPInstructionActions': {
52 {'NXActionConjunction': {'clause': 0,
59 {'NXActionConjunction': {'clause': 1,
69 self._test_str(self.fake_dp_of15,
71 {'OFPInstructionActions': {
72 'actions': [{'NXActionCT': {'actions': [],
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': {
91 {'OFPActionSetField': {
92 'field': {'OXMTlv': {'field': 'ct_mark',
104 'zone_ofs_nbits': 15,
105 'zone_src': u'reg8'}}],
108 def test_resubmit(self):
109 self._test_str(self.fake_dp_of15,
111 {'OFPInstructionActions':
112 {'actions': [{'NXActionResubmitTable': {
113 'experimenter': 8992,
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',
133 def test_pop_vlan(self):
134 self._test_str(self.fake_dp_of15,
136 {'OFPInstructionActions':
137 {'actions': [{'OFPActionPopVlan': {'len': 8,
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,
148 {'OFPInstructionGotoTable':
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,
159 {'OFPActionOutput': {'len': 16,
164 {'OFPInstructionGotoTable':