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