blob: 7e9bcd89223e30113867b681717c8a0135348300 [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001# Copyright 2017-present Open Networking Foundation
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 implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -080014from unittest import TestCase, main
15
16from loxi import of13
Gamze Abaka61c2e982018-02-14 11:03:36 +000017from ofagent.loxi.of13.instruction import clear_actions
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -080018from voltha.protos import third_party
Gamze Abaka61c2e982018-02-14 11:03:36 +000019from ofagent.converter import to_loxi, to_grpc
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -080020from voltha.core.flow_decomposer import *
21
22_ = third_party
23
24
25class TestConverter(TestCase):
26
27 def gen_pb_flow_stats(self):
28
29 # device level flows
30
31 flow_stats = [
32 mk_flow_stat(
33 priority=2000,
34 match_fields=[in_port(2), vlan_vid(4096 + 4000), vlan_pcp(0)],
35 actions=[pop_vlan(), output(1)]
36 ),
37 mk_flow_stat(
38 priority=2000,
39 match_fields=[in_port(1), eth_type(0x888e)],
40 actions=[push_vlan(0x8100), set_field(vlan_vid(4096 + 4000)),
41 output(2)]
42 ),
43 mk_flow_stat(
44 priority=1000,
45 match_fields=[in_port(1), eth_type(0x800), ip_proto(2)],
46 actions=[push_vlan(0x8100), set_field(vlan_vid(4096 + 4000)),
47 output(2)]
48 ),
49 mk_flow_stat(
50 priority=1000,
51 match_fields=[in_port(1), eth_type(0x800), ip_proto(17),
52 udp_src(68), udp_dst(67)],
53 actions=[push_vlan(0x8100), set_field(vlan_vid(4096 + 4000)),
54 output(2)]
55 ),
56 mk_flow_stat(
57 priority=1000,
58 match_fields=[in_port(2), vlan_vid(4096 + 140)],
59 actions=[pop_vlan(), output(1)]
60 ),
61 mk_flow_stat(
62 priority=500,
63 match_fields=[in_port(2), vlan_vid(4096 + 1000), metadata(128)],
64 actions=[pop_vlan(), output(1)]
65 ),
66 mk_flow_stat(
67 priority=500,
68 match_fields=[in_port(1), vlan_vid(4096 + 128)],
69 actions=[
70 push_vlan(0x8100), set_field(vlan_vid(4096 + 1000)),
71 output(2)]
72 ),
73 mk_flow_stat(
74 priority=500,
75 match_fields=[in_port(1), vlan_vid(4096 + 129)],
76 actions=[
77 push_vlan(0x8100), set_field(vlan_vid(4096 + 1000)),
78 output(2)]
79 ),
80 ] + [
81 mk_flow_stat(
82 priority=500,
83 match_fields=[in_port(2), vlan_vid(4096 + 0)],
84 actions=[
85 set_field(vlan_vid(4096 + 128)), output(1)]
86 ),
87 mk_flow_stat(
88 priority=1000,
89 match_fields=[
90 in_port(1), eth_type(0x800), ipv4_dst(0xe4010102)],
91 actions=[output(2)]
92 ),
93 mk_flow_stat(
94 priority=1000,
95 match_fields=[
96 in_port(1), eth_type(0x800), ipv4_dst(0xe4010104)],
97 actions=[output(2)]
98 ),
99 mk_flow_stat(
100 priority=500,
101 match_fields=[in_port(1), vlan_vid(4096 + 128)],
102 actions=[set_field(vlan_vid(4096 + 0)), output(2)]
103 ),
104 mk_flow_stat(
105 priority=500,
106 match_fields=[in_port(2), vlan_vid(0)],
107 actions=[push_vlan(0x8100), set_field(vlan_vid(4096 + 128)),
108 output(1)]
109 )
110
111 ]
112
113 # logical device level flows
114
115 # Various controller-bound rules
116 for _in_port in (1, 2):
117 flow_stats.append(mk_flow_stat(
118 priority=2000,
119 match_fields=[in_port(_in_port), eth_type(0x888e)],
120 actions=[
121 push_vlan(0x8100),
122 set_field(vlan_vid(4096 + 4000)),
123 output(ofp.OFPP_CONTROLLER)
124 ]
125 ))
126 flow_stats.append(mk_flow_stat(
127 priority=1000,
128 match_fields=[eth_type(0x800), ip_proto(2)],
129 actions=[output(ofp.OFPP_CONTROLLER)]
130 ))
131 flow_stats.append(mk_flow_stat(
132 priority=1000,
133 match_fields=[eth_type(0x800), ip_proto(17),
134 udp_src(68), udp_dst(67)],
135 actions=[output(ofp.OFPP_CONTROLLER)]
136 ))
137
138 # Multicast channels
139 mcast_setup = (
140 (1, 0xe4010101, ()),
141 (2, 0xe4010102, (1,)),
142 (3, 0xe4010103, (2,)),
143 (4, 0xe4010104, (1, 2)),
144 )
alshabibf4fb2682017-01-12 00:32:56 -0600145
146 group_stats = []
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800147 for group_id, mcast_addr, ports in mcast_setup:
148 # self.lda.update_group_table(mk_multicast_group_mod(
149 # group_id=group_id,
150 # buckets=[
151 # ofp.ofp_bucket(actions=[
152 # pop_vlan(),
153 # output(port)
154 # ]) for port in ports
155 # ]))
156 flow_stats.append(mk_flow_stat(
157 priority=1000,
158 match_fields=[
159 in_port(0),
160 eth_type(0x800),
161 vlan_vid(4096 + 140),
162 ipv4_dst(mcast_addr)
163 ],
164 actions=[
165 group(group_id)
166 ]
167 ))
alshabibf4fb2682017-01-12 00:32:56 -0600168 group_stats.append(group_entry_from_group_mod(
169 mk_multicast_group_mod(
170 group_id=group_id,
171 buckets=[
172 ofp.ofp_bucket(actions=[
173 pop_vlan(),
174 output(port)
175 ]) for port in ports
176 ])))
177
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800178
179 # Unicast channels for each subscriber
180 # Downstream flow 1 for both
181 flow_stats.append(mk_flow_stat(
182 priority=500,
183 match_fields=[
184 in_port(0),
185 vlan_vid(4096 + 1000),
186 metadata(128)
187 ],
188 actions=[pop_vlan()],
189 next_table_id=1
190 ))
191 # Downstream flow 2 and upsrteam flow 1 for each ONU
192 for port, c_vid in ((1, 101), (2, 102)):
193 flow_stats.append(mk_flow_stat(
194 priority=500,
195 match_fields=[in_port(0), vlan_vid(4096 + c_vid)],
196 actions=[set_field(vlan_vid(4096 + 0)), output(port)]
197 ))
198 # for the 0-tagged case
199 flow_stats.append(mk_flow_stat(
200 priority=500,
201 match_fields=[in_port(port), vlan_vid(4096 + 0)],
202 actions=[set_field(vlan_vid(4096 + c_vid))],
203 next_table_id=1
204 ))
205 # for the untagged case
206 flow_stats.append(mk_flow_stat(
207 priority=500,
208 match_fields=[in_port(port), vlan_vid(0)],
209 actions=[push_vlan(0x8100), set_field(vlan_vid(4096 + c_vid))],
210 next_table_id=1
211 ))
212 # Upstream flow 2 for s-tag
213 flow_stats.append(mk_flow_stat(
214 priority=500,
215 match_fields=[in_port(port), vlan_vid(4096 + c_vid)],
216 actions=[
217 push_vlan(0x8100),
218 set_field(vlan_vid(4096 + 1000)),
219 output(0)
220 ]
221 ))
222
alshabibf4fb2682017-01-12 00:32:56 -0600223 return (flow_stats, group_stats)
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800224
225 def test_flow_spec_pb_to_loxi_conversion(self):
alshabibf4fb2682017-01-12 00:32:56 -0600226 flow_stats, _ = self.gen_pb_flow_stats()
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800227 for flow_stat in flow_stats:
228 loxi_flow_stats = to_loxi(flow_stat)
229
alshabibf4fb2682017-01-12 00:32:56 -0600230 def test_group_stat_spec_pb_to_loxi_conversion(self):
231 _, group_stats = self.gen_pb_flow_stats()
232 for group_stat in group_stats:
233 loxi_group_stat = to_loxi(group_stat.stats)
234
235 def test_group_desc_spec_pb_to_loxi_conversion(self):
236 _, group_stats = self.gen_pb_flow_stats()
237 for group_stat in group_stats:
238 loxi_group_desc = to_loxi(group_stat.desc)
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800239
Gamze Abaka61c2e982018-02-14 11:03:36 +0000240 def test_clear_actions_instruction(self):
241 obj = clear_actions()
242 ofp_instruction = to_grpc(obj)
243 self.assertEqual(ofp_instruction.type, 5)
Zsolt Haraszti66927732017-01-14 16:50:59 -0800244
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800245if __name__ == '__main__':
246 main()