blob: 6f3eb64009afa3486b04528e993f56cfd61a1465 [file] [log] [blame]
kdarapu4cf5cc72019-09-16 12:33:49 +05301/*
2 * Copyright 2018-present Open Networking Foundation
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 implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//Package adaptercore provides the utility for olt devices, flows and statistics
18package adaptercore
19
20import (
21 "math"
22 "reflect"
23 "testing"
24
25 fu "github.com/opencord/voltha-go/rw_core/utils"
26 ofp "github.com/opencord/voltha-protos/go/openflow_13"
27 "github.com/opencord/voltha-protos/go/voltha"
28)
29
30func TestMkUniPortNum(t *testing.T) {
31 type args struct {
32 intfID uint32
33 onuID uint32
34 uniID uint32
35 }
36 tests := []struct {
37 name string
38 args args
39 want uint32
40 }{
41 // TODO: Add test cases.
42 {"MkUniPortNum-1", args{1, 1, 1}, ((1 * 2048) + (1 * 16) + 1)},
43 {"MkUniPortNum-2", args{4, 5, 6}, ((4 * 2048) + (5 * 16) + 6)},
44 // Negative test cases to cover the log.warn
45 {"MkUniPortNum-3", args{4, 130, 6}, ((4 * 2048) + (130 * 16) + 6)},
46 }
47 for _, tt := range tests {
48 t.Run(tt.name, func(t *testing.T) {
49 if got := MkUniPortNum(tt.args.intfID, tt.args.onuID, tt.args.uniID); got != tt.want {
50 t.Errorf("MkUniPortNum() = %v, want %v", got, tt.want)
51 } else {
52 t.Logf("Expected %v , Actual %v \n", tt.want, got)
53 }
54 })
55 }
56}
57
58func TestOnuIDFromPortNum(t *testing.T) {
59 type args struct {
60 portNum uint32
61 }
62 tests := []struct {
63 name string
64 args args
65 want uint32
66 }{
67 // TODO: Add test cases.
68 {"OnuIDFromPortNum-1", args{portNum: 8096}, ((8096 / 16) & 127)},
69 {"OnuIDFromPortNum-2", args{portNum: 9095}, ((9095 / 16) & 127)},
70 }
71 for _, tt := range tests {
72 t.Run(tt.name, func(t *testing.T) {
73 if got := OnuIDFromPortNum(tt.args.portNum); got != tt.want {
74 t.Errorf("OnuIDFromPortNum() = %v, want %v", got, tt.want)
75 } else {
76 t.Logf("Expected %v , Actual %v \n", tt.want, got)
77 }
78 })
79 }
80}
81
82func TestIntfIDFromUniPortNum(t *testing.T) {
83 type args struct {
84 portNum uint32
85 }
86 tests := []struct {
87 name string
88 args args
89 want uint32
90 }{
91 // TODO: Add test cases.
92 {"IntfIDFromUniPortNum-1", args{portNum: 8096}, ((8096 / 2048) & 15)},
93 // Negative Testcase
94 {"IntfIDFromUniPortNum-2", args{portNum: 1024}, ((1024 / 2048) & 15)},
95 }
96 for _, tt := range tests {
97 t.Run(tt.name, func(t *testing.T) {
98 if got := IntfIDFromUniPortNum(tt.args.portNum); got != tt.want {
99 t.Errorf("IntfIDFromUniPortNum() = %v, want %v", got, tt.want)
100 } else {
101 t.Logf("Expected %v , Actual %v \n", tt.want, got)
102 }
103 })
104 }
105}
106
107func TestUniIDFromPortNum(t *testing.T) {
108 type args struct {
109 portNum uint32
110 }
111 tests := []struct {
112 name string
113 args args
114 want uint32
115 }{
116
117 // TODO: Add test cases.
118 {"UniIDFromPortNum-1", args{portNum: 8096}, (8096 & 15)},
119 {"UniIDFromPortNum-2", args{portNum: 1024}, (1024 & 15)},
120 }
121 for _, tt := range tests {
122 t.Run(tt.name, func(t *testing.T) {
123 if got := UniIDFromPortNum(tt.args.portNum); got != tt.want {
124 t.Errorf("UniIDFromPortNum() = %v, want %v", got, tt.want)
125 } else {
126 t.Logf("Expected %v , Actual %v \n", tt.want, got)
127 }
128 })
129 }
130}
131
132func TestIntfIDToPortNo(t *testing.T) {
133 type args struct {
134 intfID uint32
135 intfType voltha.Port_PortType
136 }
137 tests := []struct {
138 name string
139 args args
140 want uint32
141 }{
142 // TODO: Add test cases.
143 {"IntfIDToPortNo-1", args{intfID: 120, intfType: voltha.Port_ETHERNET_NNI}, (uint32(math.Pow(2, 16)) + 120)},
144 {"IntfIDToPortNo-2", args{intfID: 1024, intfType: voltha.Port_ETHERNET_UNI}, 0},
145 {"IntfIDToPortNo-3", args{intfID: 456, intfType: voltha.Port_PON_OLT}, (uint32(2*math.Pow(2, 28)) + 456)},
146 {"IntfIDToPortNo-4", args{intfID: 28, intfType: voltha.Port_PON_ONU}, 0},
147 {"IntfIDToPortNo-5", args{intfID: 45, intfType: voltha.Port_UNKNOWN}, 0},
148 {"IntfIDToPortNo-6", args{intfID: 45, intfType: voltha.Port_VENET_OLT}, 0},
149 {"IntfIDToPortNo-7", args{intfID: 45, intfType: voltha.Port_VENET_ONU}, 0},
150 }
151 for _, tt := range tests {
152 t.Run(tt.name, func(t *testing.T) {
153 if got := IntfIDToPortNo(tt.args.intfID, tt.args.intfType); got != tt.want {
154 t.Errorf("IntfIDToPortNo() = %v, want %v", got, tt.want)
155 } else {
156 t.Logf("Expected %v , Actual %v \n", tt.want, got)
157 }
158 })
159 }
160}
161
162func TestIntfIDFromNniPortNum(t *testing.T) {
163 type args struct {
164 portNum uint32
165 }
166
167 tests := []struct {
168 name string
169 args args
170 want uint32
171 }{
172 // TODO: Add test cases.
173 {"IntfIDFromNniPortNum-1", args{portNum: 8081}, 8081},
174 {"IntfIDFromNniPortNum-2", args{portNum: 9090}, 9090},
175 {"IntfIDFromNniPortNum-3", args{portNum: 0}, 0},
176 {"IntfIDFromNniPortNum-3", args{portNum: 65535}, 65535},
177 }
178 for _, tt := range tests {
179 t.Run(tt.name, func(t *testing.T) {
180 if got := IntfIDFromNniPortNum(tt.args.portNum); got != tt.want {
181 t.Errorf("IntfIDFromNniPortNum() = %v, want %v", got, tt.want)
182 } else {
183 t.Logf("Expected %v , Actual %v \n", tt.want, got)
184 }
185 })
186 }
187}
188
189func TestIntfIDToPortTypeName(t *testing.T) {
190 type args struct {
191 intfID uint32
192 }
193 var input uint32
194 input = uint32(2*math.Pow(2, 28)) | 3
195 tests := []struct {
196 name string
197 args args
198 want voltha.Port_PortType
199 }{
200 // TODO: Add test cases.
201 {"IntfIDToPortTypeName-1", args{intfID: 65536}, voltha.Port_ETHERNET_NNI},
202 {"IntfIDToPortTypeName-2", args{intfID: 1000}, voltha.Port_ETHERNET_UNI},
203 {"IntfIDToPortTypeName-2", args{intfID: input}, voltha.Port_PON_OLT},
204 }
205 for _, tt := range tests {
206 t.Run(tt.name, func(t *testing.T) {
207 if got := IntfIDToPortTypeName(tt.args.intfID); !reflect.DeepEqual(got, tt.want) {
208 t.Errorf("IntfIDToPortTypeName() = %v, want %v", got, tt.want)
209 }
210 })
211 }
212}
213
214func TestExtractAccessFromFlow(t *testing.T) {
215 type args struct {
216 inPort uint32
217 outPort uint32
218 }
219 tests := []struct {
220 name string
221 args args
222 want uint32
223 want1 uint32
224 want2 uint32
225 want3 uint32
226 }{
227 // TODO: Add test cases.
228 {"ExtractAccessFromFlow-1", args{inPort: 10, outPort: 65536}, 10, 0, 0, 10},
229 }
230 for _, tt := range tests {
231 t.Run(tt.name, func(t *testing.T) {
232 got, got1, got2, got3 := ExtractAccessFromFlow(tt.args.inPort, tt.args.outPort)
233 if got != tt.want {
234 t.Errorf("ExtractAccessFromFlow() got = %v, want %v", got, tt.want)
235 }
236 if got1 != tt.want1 {
237 t.Errorf("ExtractAccessFromFlow() got1 = %v, want %v", got1, tt.want1)
238 }
239 if got2 != tt.want2 {
240 t.Errorf("ExtractAccessFromFlow() got2 = %v, want %v", got2, tt.want2)
241 }
242 if got3 != tt.want3 {
243 t.Errorf("ExtractAccessFromFlow() got3 = %v, want %v", got3, tt.want3)
244 }
245 })
246 }
247}
248
249func TestIsUpstream(t *testing.T) {
250 type args struct {
251 outPort uint32
252 }
253 tests := []struct {
254 name string
255 args args
256 want bool
257 }{
258 // TODO: Add test cases.
259 {"TestIsUpstream-1", args{outPort: 65533}, true},
260 {"TestIsUpstream-2", args{outPort: 65536}, true},
261 {"TestIsUpstream-3", args{outPort: 65537}, true},
262 {"TestIsUpstream-4", args{outPort: 65538}, true},
263 {"TestIsUpstream-5", args{outPort: 65539}, true},
264 {"TestIsUpstream-6", args{outPort: 1000}, false},
265 }
266 for _, tt := range tests {
267 t.Run(tt.name, func(t *testing.T) {
268 if got := IsUpstream(tt.args.outPort); got != tt.want {
269 t.Errorf("IsUpstream() = %v, want %v", got, tt.want)
270 }
271 })
272 }
273}
274
275func TestIsControllerBoundFlow(t *testing.T) {
276 type args struct {
277 outPort uint32
278 }
279 tests := []struct {
280 name string
281 args args
282 want bool
283 }{
284 // TODO: Add test cases.
285 {"IsControllerBoundFlow-1", args{outPort: 65533}, true},
286 {"IsControllerBoundFlow-2", args{outPort: 65536}, false},
287 {"IsControllerBoundFlow-3", args{outPort: 65537}, false},
288 {"IsControllerBoundFlow-4", args{outPort: 65538}, false},
289 {"IsControllerBoundFlow-5", args{outPort: 65539}, false},
290 {"IsControllerBoundFlow-6", args{outPort: 1000}, false},
291 }
292 for _, tt := range tests {
293 t.Run(tt.name, func(t *testing.T) {
294 if got := IsControllerBoundFlow(tt.args.outPort); got != tt.want {
295 t.Errorf("IsControllerBoundFlow() = %v, want %v", got, tt.want)
296 }
297 })
298 }
299}
300
301func TestFlowExtractInfo(t *testing.T) {
302 fa := &fu.FlowArgs{
303 MatchFields: []*ofp.OfpOxmOfbField{
304 fu.InPort(2),
305 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
306 fu.EthType(2048),
307 },
308
309 Actions: []*ofp.OfpAction{
310 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
311 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
312 fu.Output(1),
313 },
314 }
315 ofpstats := fu.MkFlowStat(fa)
316 type args struct {
317 flow *ofp.OfpFlowStats
318 flowDirection string
319 }
320 tests := []struct {
321 name string
322 args args
323 want uint32
324 want1 uint32
325 want2 uint32
326 want3 uint32
327 want4 uint32
328 want5 uint32
329 wantErr bool
330 }{
331 // TODO: Add test cases.
332 {"FlowExtractInfo-1", args{flow: ofpstats, flowDirection: "upstream"}, 2, 0, 0, 2, 0, 0, false},
333
334 // Negative Testcases
335 {"FlowExtractInfo-2", args{flow: ofpstats, flowDirection: "downstream"}, 0, 0, 0, 0, 0, 0, true},
336 {"FlowExtractInfo-3", args{flow: nil, flowDirection: "downstream"}, 0, 0, 0, 0, 0, 0, true},
337 {"FlowExtractInfo-4", args{flow: &ofp.OfpFlowStats{}, flowDirection: "downstream"}, 0, 0, 0, 0, 0, 0, true},
338 }
339 for _, tt := range tests {
340 t.Run(tt.name, func(t *testing.T) {
341 got, got1, got2, got3, got4, got5, err := FlowExtractInfo(tt.args.flow, tt.args.flowDirection)
342 if (err != nil) != tt.wantErr {
343 t.Errorf("FlowExtractInfo() error = %v, wantErr %v", err, tt.wantErr)
344 return
345 }
346 if got != tt.want {
347 t.Errorf("FlowExtractInfo() got = %v, want %v", got, tt.want)
348 return
349 }
350 if got1 != tt.want1 {
351 t.Errorf("FlowExtractInfo() got1 = %v, want %v", got1, tt.want1)
352 return
353 }
354 if got2 != tt.want2 {
355 t.Errorf("FlowExtractInfo() got2 = %v, want %v", got2, tt.want2)
356 return
357 }
358 if got3 != tt.want3 {
359 t.Errorf("FlowExtractInfo() got3 = %v, want %v", got3, tt.want3)
360 return
361 }
362 if got4 != tt.want4 {
363 t.Errorf("FlowExtractInfo() got4 = %v, want %v", got4, tt.want4)
364 return
365 }
366 if got5 != tt.want5 {
367 t.Errorf("FlowExtractInfo() got5 = %v, want %v", got5, tt.want5)
368 return
369 }
370 })
371 }
372}