1# Copyright (C) 2016 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
16import logging
17import unittest
18
19from ryu.lib import ofctl_utils
20from ryu.ofproto import ofproto_v1_3
21
22
23LOG = logging.getLogger(__name__)
24
25
26class Test_ofctl_utils(unittest.TestCase):
27    # prepare test target
28    util = ofctl_utils.OFCtlUtil(ofproto_v1_3)
29
30    def _test_str_to_int(self, input_value, expected_value):
31        output_value = ofctl_utils.str_to_int(input_value)
32        self.assertEqual(expected_value, output_value)
33
34    def test_str_to_int(self):
35        self._test_str_to_int(1, 1)        # int
36        self._test_str_to_int('0b10', 2)   # binary digit
37        self._test_str_to_int('0o10', 8)   # octal digit
38        self._test_str_to_int('0x10', 16)  # hexadecimal digit
39
40    def test_ofp_port_from_user(self):
41        self.assertEqual(
42            ofproto_v1_3.OFPP_CONTROLLER,
43            self.util.ofp_port_from_user(ofproto_v1_3.OFPP_CONTROLLER)  # int
44        )
45        self.assertEqual(
46            ofproto_v1_3.OFPP_CONTROLLER,
47            self.util.ofp_port_from_user('CONTROLLER')       # str without prefix
48        )
49        self.assertEqual(
50            ofproto_v1_3.OFPP_CONTROLLER,
51            self.util.ofp_port_from_user('OFPP_CONTROLLER')  # str with prefix
52        )
53
54    def test_ofp_port_to_user(self):
55        self.assertEqual(
56            'CONTROLLER',
57            self.util.ofp_port_to_user(ofproto_v1_3.OFPP_CONTROLLER)
58        )
59        self.assertEqual(
60            1,
61            self.util.ofp_port_to_user(1)  # not matched
62        )
63
64    def test_ofp_table_from_user(self):
65        self.assertEqual(
66            ofproto_v1_3.OFPTT_ALL,
67            self.util.ofp_table_from_user('ALL')
68        )
69
70    def test_ofp_table_to_user(self):
71        self.assertEqual(
72            'ALL',
73            self.util.ofp_table_to_user(ofproto_v1_3.OFPTT_ALL)
74        )
75
76    def test_ofp_cml_from_user(self):
77        self.assertEqual(
78            ofproto_v1_3.OFPCML_NO_BUFFER,
79            self.util.ofp_cml_from_user('NO_BUFFER')
80        )
81
82    def test_ofp_cml_to_user(self):
83        self.assertEqual(
84            'NO_BUFFER',
85            self.util.ofp_cml_to_user(ofproto_v1_3.OFPCML_NO_BUFFER)
86        )
87
88    def test_ofp_group_from_user(self):
89        self.assertEqual(
90            ofproto_v1_3.OFPG_ANY,
91            self.util.ofp_group_from_user('ANY')
92        )
93
94    def test_ofp_group_to_user(self):
95        self.assertEqual(
96            'ANY',
97            self.util.ofp_group_to_user(ofproto_v1_3.OFPG_ANY)
98        )
99
100    def test_ofp_buffer_from_user(self):
101        self.assertEqual(
102            ofproto_v1_3.OFP_NO_BUFFER,
103            self.util.ofp_buffer_from_user('NO_BUFFER')
104        )
105        self.assertEqual(
106            1,
107            self.util.ofp_buffer_from_user(1)  # not matched
108        )
109
110    def test_ofp_buffer_to_user(self):
111        self.assertEqual(
112            'NO_BUFFER',
113            self.util.ofp_buffer_to_user(ofproto_v1_3.OFP_NO_BUFFER)
114        )
115        self.assertEqual(
116            1,
117            self.util.ofp_buffer_to_user(1)  # not matched
118        )
119
120    def test_ofp_meter_from_user(self):
121        self.assertEqual(
122            ofproto_v1_3.OFPM_ALL,
123            self.util.ofp_meter_from_user('ALL')
124        )
125
126    def test_ofp_meter_to_user(self):
127        self.assertEqual(
128            'ALL',
129            self.util.ofp_meter_to_user(ofproto_v1_3.OFPM_ALL)
130        )
131
132    def test_ofp_queue_from_user(self):
133        self.assertEqual(
134            ofproto_v1_3.OFPQ_ALL,
135            self.util.ofp_queue_from_user('ALL')
136        )
137
138    def test_ofp_queue_to_user(self):
139        self.assertEqual(
140            'ALL',
141            self.util.ofp_queue_to_user(ofproto_v1_3.OFPQ_ALL)
142        )
143