blob: 536e13a571ea0c97d956e869480900a59c3e6b3f [file] [log] [blame]
kdarapu891693b2019-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
Esin Karamanccb714b2019-11-29 15:02:06 +000025 fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
26 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
27 "github.com/opencord/voltha-protos/v3/go/voltha"
kdarapu891693b2019-09-16 12:33:49 +053028)
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.
Amit Ghoshd4cbe482019-11-21 12:07:14 +000042 {"MkUniPortNum-1", args{1, 1, 1}, ((1 * 4096) + (1 * 16) + 1)},
43 {"MkUniPortNum-2", args{4, 5, 6}, ((4 * 4096) + (5 * 16) + 6)},
kdarapu891693b2019-09-16 12:33:49 +053044 // Negative test cases to cover the log.warn
Amit Ghoshd4cbe482019-11-21 12:07:14 +000045 {"MkUniPortNum-3", args{4, 130, 6}, ((4 * 4096) + (130 * 16) + 6)},
kdarapu891693b2019-09-16 12:33:49 +053046 }
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.
Amit Ghoshd4cbe482019-11-21 12:07:14 +000068 {"OnuIDFromPortNum-1", args{portNum: 8096}, ((8096 / 16) & 255)},
69 {"OnuIDFromPortNum-2", args{portNum: 9095}, ((9095 / 16) & 255)},
kdarapu891693b2019-09-16 12:33:49 +053070 }
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.
Amit Ghoshd4cbe482019-11-21 12:07:14 +000092 {"IntfIDFromUniPortNum-1", args{portNum: 8096}, ((8096 / 4096) & 15)},
kdarapu891693b2019-09-16 12:33:49 +053093 // Negative Testcase
Amit Ghoshd4cbe482019-11-21 12:07:14 +000094 {"IntfIDFromUniPortNum-2", args{portNum: 1024}, ((1024 / 4096) & 15)},
kdarapu891693b2019-09-16 12:33:49 +053095 }
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.
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000143 {"IntfIDToPortNo-1", args{intfID: 120, intfType: voltha.Port_ETHERNET_NNI}, (uint32(math.Pow(2, 20)) + 120)},
kdarapu891693b2019-09-16 12:33:49 +0530144 {"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 {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800168 name string
169 args args
170 want uint32
171 wantErr error
kdarapu891693b2019-09-16 12:33:49 +0530172 }{
173 // TODO: Add test cases.
David K. Bainbridge794735f2020-02-11 21:01:37 -0800174 {"IntfIDFromNniPortNum-01", args{portNum: 8081}, 0, ErrInvalidPortRange},
175 {"IntfIDFromNniPortNum-02", args{portNum: 9090}, 0, ErrInvalidPortRange},
176 {"IntfIDFromNniPortNum-03", args{portNum: 0}, 0, ErrInvalidPortRange},
177 {"IntfIDFromNniPortNum-04", args{portNum: 65535}, 0, ErrInvalidPortRange},
178 {"IntfIDFromNniPortNum-05", args{portNum: 1048575}, 0, ErrInvalidPortRange},
179 {"IntfIDFromNniPortNum-06", args{portNum: 1048576}, 0, nil},
180 {"IntfIDFromNniPortNum-07", args{portNum: 1048577}, 1, nil},
181 {"IntfIDFromNniPortNum-08", args{portNum: 1048578}, 2, nil},
182 {"IntfIDFromNniPortNum-09", args{portNum: 1048579}, 3, nil},
183 {"IntfIDFromNniPortNum-10", args{portNum: 2097150}, 65534, nil},
184 {"IntfIDFromNniPortNum-11", args{portNum: 2097151}, 65535, nil},
185 {"IntfIDFromNniPortNum-12", args{portNum: 3000000}, 0, ErrInvalidPortRange},
kdarapu891693b2019-09-16 12:33:49 +0530186 }
187 for _, tt := range tests {
188 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800189 got, err := IntfIDFromNniPortNum(tt.args.portNum)
190 if got != tt.want || err != tt.wantErr {
191 t.Errorf("IntfIDFromNniPortNum(): FOR[%v] WANT[%v and %v] GOT[%v and %v]",
192 tt.args.portNum, tt.want, tt.wantErr, got, err)
kdarapu891693b2019-09-16 12:33:49 +0530193 }
194 })
195 }
196}
197
198func TestIntfIDToPortTypeName(t *testing.T) {
199 type args struct {
200 intfID uint32
201 }
202 var input uint32
203 input = uint32(2*math.Pow(2, 28)) | 3
204 tests := []struct {
205 name string
206 args args
207 want voltha.Port_PortType
208 }{
209 // TODO: Add test cases.
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000210 {"IntfIDToPortTypeName-1", args{intfID: 1048576}, voltha.Port_ETHERNET_NNI},
kdarapu891693b2019-09-16 12:33:49 +0530211 {"IntfIDToPortTypeName-2", args{intfID: 1000}, voltha.Port_ETHERNET_UNI},
212 {"IntfIDToPortTypeName-2", args{intfID: input}, voltha.Port_PON_OLT},
213 }
214 for _, tt := range tests {
215 t.Run(tt.name, func(t *testing.T) {
216 if got := IntfIDToPortTypeName(tt.args.intfID); !reflect.DeepEqual(got, tt.want) {
217 t.Errorf("IntfIDToPortTypeName() = %v, want %v", got, tt.want)
218 }
219 })
220 }
221}
222
223func TestExtractAccessFromFlow(t *testing.T) {
224 type args struct {
225 inPort uint32
226 outPort uint32
227 }
228 tests := []struct {
kdarapuf0c0e382019-09-30 05:26:31 +0530229 name string
230 args args
231 port uint32
232 IntfID uint32
233 onuID uint32
234 uniID uint32
kdarapu891693b2019-09-16 12:33:49 +0530235 }{
236 // TODO: Add test cases.
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000237 {"ExtractAccessFromFlow-1", args{inPort: 100, outPort: 1048576}, 100, 0, 6, 4},
238 {"ExtractAccessFromFlow-2", args{inPort: 1048576, outPort: 10}, 10, 0, 0, 10},
kdarapu891693b2019-09-16 12:33:49 +0530239 }
240 for _, tt := range tests {
241 t.Run(tt.name, func(t *testing.T) {
242 got, got1, got2, got3 := ExtractAccessFromFlow(tt.args.inPort, tt.args.outPort)
kdarapuf0c0e382019-09-30 05:26:31 +0530243 if got != tt.port {
244 t.Errorf("ExtractAccessFromFlow() got = %v, want %v", got, tt.port)
kdarapu891693b2019-09-16 12:33:49 +0530245 }
kdarapuf0c0e382019-09-30 05:26:31 +0530246 if got1 != tt.IntfID {
247 t.Errorf("ExtractAccessFromFlow() got1 = %v, want %v", got1, tt.IntfID)
kdarapu891693b2019-09-16 12:33:49 +0530248 }
kdarapuf0c0e382019-09-30 05:26:31 +0530249 if got2 != tt.onuID {
250 t.Errorf("ExtractAccessFromFlow() got2 = %v, want %v", got2, tt.onuID)
kdarapu891693b2019-09-16 12:33:49 +0530251 }
kdarapuf0c0e382019-09-30 05:26:31 +0530252 if got3 != tt.uniID {
253 t.Errorf("ExtractAccessFromFlow() got3 = %v, want %v", got3, tt.uniID)
kdarapu891693b2019-09-16 12:33:49 +0530254 }
255 })
256 }
kdarapuf0c0e382019-09-30 05:26:31 +0530257 //t.Error()
kdarapu891693b2019-09-16 12:33:49 +0530258}
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},
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000271 {"TestIsUpstream-2", args{outPort: 1048576}, true},
272 {"TestIsUpstream-3", args{outPort: 1048577}, true},
273 {"TestIsUpstream-4", args{outPort: 1048578}, true},
kdarapu891693b2019-09-16 12:33:49 +0530274 {"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),
kdarapuf0c0e382019-09-30 05:26:31 +0530315 fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2)),
kdarapu891693b2019-09-16 12:33:49 +0530316 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
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
kdarapuf0c0e382019-09-30 05:26:31 +0530346 {"FlowExtractInfo-2", args{flow: ofpstats, flowDirection: "downstream"}, 1, 0, 0, 1, 2, 2048, false},
kdarapu891693b2019-09-16 12:33:49 +0530347 {"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(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}