blob: 71414c100eed5c406f442dcd8cdd0ac8a777623e [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
Scott Bakerdbd960e2020-02-28 08:57:51 -080017//Package core provides the utility for olt devices, flows and statistics
18package core
kdarapu891693b2019-09-16 12:33:49 +053019
20import (
Neha Sharma96b7bf22020-06-15 10:37:32 +000021 "context"
Esin Karamanccb714b2019-11-29 15:02:06 +000022 fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
Thomas Lee S94109f12020-03-03 16:39:29 +053023 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Esin Karamanccb714b2019-11-29 15:02:06 +000024 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
25 "github.com/opencord/voltha-protos/v3/go/voltha"
Neha Sharma96b7bf22020-06-15 10:37:32 +000026 "math"
27 "reflect"
28 "testing"
kdarapu891693b2019-09-16 12:33:49 +053029)
30
31func TestMkUniPortNum(t *testing.T) {
32 type args struct {
33 intfID uint32
34 onuID uint32
35 uniID uint32
36 }
37 tests := []struct {
38 name string
39 args args
40 want uint32
41 }{
42 // TODO: Add test cases.
Amit Ghoshd4cbe482019-11-21 12:07:14 +000043 {"MkUniPortNum-1", args{1, 1, 1}, ((1 * 4096) + (1 * 16) + 1)},
44 {"MkUniPortNum-2", args{4, 5, 6}, ((4 * 4096) + (5 * 16) + 6)},
kdarapu891693b2019-09-16 12:33:49 +053045 // Negative test cases to cover the log.warn
Amit Ghoshd4cbe482019-11-21 12:07:14 +000046 {"MkUniPortNum-3", args{4, 130, 6}, ((4 * 4096) + (130 * 16) + 6)},
kdarapu891693b2019-09-16 12:33:49 +053047 }
48 for _, tt := range tests {
49 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000050 if got := MkUniPortNum(context.Background(), tt.args.intfID, tt.args.onuID, tt.args.uniID); got != tt.want {
kdarapu891693b2019-09-16 12:33:49 +053051 t.Errorf("MkUniPortNum() = %v, want %v", got, tt.want)
52 } else {
53 t.Logf("Expected %v , Actual %v \n", tt.want, got)
54 }
55 })
56 }
57}
58
59func TestOnuIDFromPortNum(t *testing.T) {
60 type args struct {
61 portNum uint32
62 }
63 tests := []struct {
64 name string
65 args args
66 want uint32
67 }{
68 // TODO: Add test cases.
Amit Ghoshd4cbe482019-11-21 12:07:14 +000069 {"OnuIDFromPortNum-1", args{portNum: 8096}, ((8096 / 16) & 255)},
70 {"OnuIDFromPortNum-2", args{portNum: 9095}, ((9095 / 16) & 255)},
kdarapu891693b2019-09-16 12:33:49 +053071 }
72 for _, tt := range tests {
73 t.Run(tt.name, func(t *testing.T) {
74 if got := OnuIDFromPortNum(tt.args.portNum); got != tt.want {
75 t.Errorf("OnuIDFromPortNum() = %v, want %v", got, tt.want)
76 } else {
77 t.Logf("Expected %v , Actual %v \n", tt.want, got)
78 }
79 })
80 }
81}
82
83func TestIntfIDFromUniPortNum(t *testing.T) {
84 type args struct {
85 portNum uint32
86 }
87 tests := []struct {
88 name string
89 args args
90 want uint32
91 }{
92 // TODO: Add test cases.
Amit Ghoshd4cbe482019-11-21 12:07:14 +000093 {"IntfIDFromUniPortNum-1", args{portNum: 8096}, ((8096 / 4096) & 15)},
kdarapu891693b2019-09-16 12:33:49 +053094 // Negative Testcase
Amit Ghoshd4cbe482019-11-21 12:07:14 +000095 {"IntfIDFromUniPortNum-2", args{portNum: 1024}, ((1024 / 4096) & 15)},
kdarapu891693b2019-09-16 12:33:49 +053096 }
97 for _, tt := range tests {
98 t.Run(tt.name, func(t *testing.T) {
99 if got := IntfIDFromUniPortNum(tt.args.portNum); got != tt.want {
100 t.Errorf("IntfIDFromUniPortNum() = %v, want %v", got, tt.want)
101 } else {
102 t.Logf("Expected %v , Actual %v \n", tt.want, got)
103 }
104 })
105 }
106}
107
108func TestUniIDFromPortNum(t *testing.T) {
109 type args struct {
110 portNum uint32
111 }
112 tests := []struct {
113 name string
114 args args
115 want uint32
116 }{
117
118 // TODO: Add test cases.
119 {"UniIDFromPortNum-1", args{portNum: 8096}, (8096 & 15)},
120 {"UniIDFromPortNum-2", args{portNum: 1024}, (1024 & 15)},
121 }
122 for _, tt := range tests {
123 t.Run(tt.name, func(t *testing.T) {
124 if got := UniIDFromPortNum(tt.args.portNum); got != tt.want {
125 t.Errorf("UniIDFromPortNum() = %v, want %v", got, tt.want)
126 } else {
127 t.Logf("Expected %v , Actual %v \n", tt.want, got)
128 }
129 })
130 }
131}
132
133func TestIntfIDToPortNo(t *testing.T) {
134 type args struct {
135 intfID uint32
136 intfType voltha.Port_PortType
137 }
138 tests := []struct {
139 name string
140 args args
141 want uint32
142 }{
143 // TODO: Add test cases.
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000144 {"IntfIDToPortNo-1", args{intfID: 120, intfType: voltha.Port_ETHERNET_NNI}, (uint32(math.Pow(2, 20)) + 120)},
kdarapu891693b2019-09-16 12:33:49 +0530145 {"IntfIDToPortNo-2", args{intfID: 1024, intfType: voltha.Port_ETHERNET_UNI}, 0},
146 {"IntfIDToPortNo-3", args{intfID: 456, intfType: voltha.Port_PON_OLT}, (uint32(2*math.Pow(2, 28)) + 456)},
147 {"IntfIDToPortNo-4", args{intfID: 28, intfType: voltha.Port_PON_ONU}, 0},
148 {"IntfIDToPortNo-5", args{intfID: 45, intfType: voltha.Port_UNKNOWN}, 0},
149 {"IntfIDToPortNo-6", args{intfID: 45, intfType: voltha.Port_VENET_OLT}, 0},
150 {"IntfIDToPortNo-7", args{intfID: 45, intfType: voltha.Port_VENET_ONU}, 0},
151 }
152 for _, tt := range tests {
153 t.Run(tt.name, func(t *testing.T) {
154 if got := IntfIDToPortNo(tt.args.intfID, tt.args.intfType); got != tt.want {
155 t.Errorf("IntfIDToPortNo() = %v, want %v", got, tt.want)
156 } else {
157 t.Logf("Expected %v , Actual %v \n", tt.want, got)
158 }
159 })
160 }
161}
162
163func TestIntfIDFromNniPortNum(t *testing.T) {
164 type args struct {
165 portNum uint32
166 }
167
168 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800169 name string
170 args args
171 want uint32
172 wantErr error
kdarapu891693b2019-09-16 12:33:49 +0530173 }{
174 // TODO: Add test cases.
Thomas Lee S94109f12020-03-03 16:39:29 +0530175 {"IntfIDFromNniPortNum-01", args{portNum: 8081}, 0, olterrors.ErrInvalidPortRange},
176 {"IntfIDFromNniPortNum-02", args{portNum: 9090}, 0, olterrors.ErrInvalidPortRange},
177 {"IntfIDFromNniPortNum-03", args{portNum: 0}, 0, olterrors.ErrInvalidPortRange},
178 {"IntfIDFromNniPortNum-04", args{portNum: 65535}, 0, olterrors.ErrInvalidPortRange},
179 {"IntfIDFromNniPortNum-05", args{portNum: 1048575}, 0, olterrors.ErrInvalidPortRange},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800180 {"IntfIDFromNniPortNum-06", args{portNum: 1048576}, 0, nil},
181 {"IntfIDFromNniPortNum-07", args{portNum: 1048577}, 1, nil},
182 {"IntfIDFromNniPortNum-08", args{portNum: 1048578}, 2, nil},
183 {"IntfIDFromNniPortNum-09", args{portNum: 1048579}, 3, nil},
184 {"IntfIDFromNniPortNum-10", args{portNum: 2097150}, 65534, nil},
185 {"IntfIDFromNniPortNum-11", args{portNum: 2097151}, 65535, nil},
Thomas Lee S94109f12020-03-03 16:39:29 +0530186 {"IntfIDFromNniPortNum-12", args{portNum: 3000000}, 0, olterrors.ErrInvalidPortRange},
kdarapu891693b2019-09-16 12:33:49 +0530187 }
188 for _, tt := range tests {
189 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000190 got, err := IntfIDFromNniPortNum(context.Background(), tt.args.portNum)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800191 if got != tt.want || err != tt.wantErr {
192 t.Errorf("IntfIDFromNniPortNum(): FOR[%v] WANT[%v and %v] GOT[%v and %v]",
193 tt.args.portNum, tt.want, tt.wantErr, got, err)
kdarapu891693b2019-09-16 12:33:49 +0530194 }
195 })
196 }
197}
198
199func TestIntfIDToPortTypeName(t *testing.T) {
200 type args struct {
201 intfID uint32
202 }
203 var input uint32
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.
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000211 {"IntfIDToPortTypeName-1", args{intfID: 1048576}, voltha.Port_ETHERNET_NNI},
kdarapu891693b2019-09-16 12:33:49 +0530212 {"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 {
kdarapuf0c0e382019-09-30 05:26:31 +0530230 name string
231 args args
232 port uint32
233 IntfID uint32
234 onuID uint32
235 uniID uint32
kdarapu891693b2019-09-16 12:33:49 +0530236 }{
237 // TODO: Add test cases.
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000238 {"ExtractAccessFromFlow-1", args{inPort: 100, outPort: 1048576}, 100, 0, 6, 4},
239 {"ExtractAccessFromFlow-2", args{inPort: 1048576, outPort: 10}, 10, 0, 0, 10},
kdarapu891693b2019-09-16 12:33:49 +0530240 }
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)
kdarapuf0c0e382019-09-30 05:26:31 +0530244 if got != tt.port {
245 t.Errorf("ExtractAccessFromFlow() got = %v, want %v", got, tt.port)
kdarapu891693b2019-09-16 12:33:49 +0530246 }
kdarapuf0c0e382019-09-30 05:26:31 +0530247 if got1 != tt.IntfID {
248 t.Errorf("ExtractAccessFromFlow() got1 = %v, want %v", got1, tt.IntfID)
kdarapu891693b2019-09-16 12:33:49 +0530249 }
kdarapuf0c0e382019-09-30 05:26:31 +0530250 if got2 != tt.onuID {
251 t.Errorf("ExtractAccessFromFlow() got2 = %v, want %v", got2, tt.onuID)
kdarapu891693b2019-09-16 12:33:49 +0530252 }
kdarapuf0c0e382019-09-30 05:26:31 +0530253 if got3 != tt.uniID {
254 t.Errorf("ExtractAccessFromFlow() got3 = %v, want %v", got3, tt.uniID)
kdarapu891693b2019-09-16 12:33:49 +0530255 }
256 })
257 }
kdarapuf0c0e382019-09-30 05:26:31 +0530258 //t.Error()
kdarapu891693b2019-09-16 12:33:49 +0530259}
260
261func TestIsUpstream(t *testing.T) {
262 type args struct {
263 outPort uint32
264 }
265 tests := []struct {
266 name string
267 args args
268 want bool
269 }{
270 // TODO: Add test cases.
271 {"TestIsUpstream-1", args{outPort: 65533}, true},
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000272 {"TestIsUpstream-2", args{outPort: 1048576}, true},
273 {"TestIsUpstream-3", args{outPort: 1048577}, true},
274 {"TestIsUpstream-4", args{outPort: 1048578}, true},
kdarapu891693b2019-09-16 12:33:49 +0530275 {"TestIsUpstream-6", args{outPort: 1000}, false},
276 }
277 for _, tt := range tests {
278 t.Run(tt.name, func(t *testing.T) {
279 if got := IsUpstream(tt.args.outPort); got != tt.want {
280 t.Errorf("IsUpstream() = %v, want %v", got, tt.want)
281 }
282 })
283 }
284}
285
286func TestIsControllerBoundFlow(t *testing.T) {
287 type args struct {
288 outPort uint32
289 }
290 tests := []struct {
291 name string
292 args args
293 want bool
294 }{
295 // TODO: Add test cases.
296 {"IsControllerBoundFlow-1", args{outPort: 65533}, true},
297 {"IsControllerBoundFlow-2", args{outPort: 65536}, false},
298 {"IsControllerBoundFlow-3", args{outPort: 65537}, false},
299 {"IsControllerBoundFlow-4", args{outPort: 65538}, false},
300 {"IsControllerBoundFlow-5", args{outPort: 65539}, false},
301 {"IsControllerBoundFlow-6", args{outPort: 1000}, false},
302 }
303 for _, tt := range tests {
304 t.Run(tt.name, func(t *testing.T) {
305 if got := IsControllerBoundFlow(tt.args.outPort); got != tt.want {
306 t.Errorf("IsControllerBoundFlow() = %v, want %v", got, tt.want)
307 }
308 })
309 }
310}
311
312func TestFlowExtractInfo(t *testing.T) {
313 fa := &fu.FlowArgs{
314 MatchFields: []*ofp.OfpOxmOfbField{
315 fu.InPort(2),
kdarapuf0c0e382019-09-30 05:26:31 +0530316 fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2)),
kdarapu891693b2019-09-16 12:33:49 +0530317 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
318 fu.EthType(2048),
319 },
320
321 Actions: []*ofp.OfpAction{
322 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
323 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
324 fu.Output(1),
325 },
326 }
divyadesaid26f6b12020-03-19 06:30:28 +0000327 ofpstats, _ := fu.MkFlowStat(fa)
kdarapu891693b2019-09-16 12:33:49 +0530328 type args struct {
329 flow *ofp.OfpFlowStats
330 flowDirection string
331 }
332 tests := []struct {
333 name string
334 args args
335 want uint32
336 want1 uint32
337 want2 uint32
338 want3 uint32
339 want4 uint32
340 want5 uint32
341 wantErr bool
342 }{
343 // TODO: Add test cases.
344 {"FlowExtractInfo-1", args{flow: ofpstats, flowDirection: "upstream"}, 2, 0, 0, 2, 0, 0, false},
345
346 // Negative Testcases
kdarapuf0c0e382019-09-30 05:26:31 +0530347 {"FlowExtractInfo-2", args{flow: ofpstats, flowDirection: "downstream"}, 1, 0, 0, 1, 2, 2048, false},
kdarapu891693b2019-09-16 12:33:49 +0530348 {"FlowExtractInfo-3", args{flow: nil, flowDirection: "downstream"}, 0, 0, 0, 0, 0, 0, true},
349 {"FlowExtractInfo-4", args{flow: &ofp.OfpFlowStats{}, flowDirection: "downstream"}, 0, 0, 0, 0, 0, 0, true},
350 }
351 for _, tt := range tests {
352 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000353 got, got1, got2, got3, got4, got5, err := FlowExtractInfo(context.Background(), tt.args.flow, tt.args.flowDirection)
kdarapu891693b2019-09-16 12:33:49 +0530354 if (err != nil) != tt.wantErr {
355 t.Errorf("FlowExtractInfo() error = %v, wantErr %v", err, tt.wantErr)
356 return
357 }
358 if got != tt.want {
359 t.Errorf("FlowExtractInfo() got = %v, want %v", got, tt.want)
360 return
361 }
362 if got1 != tt.want1 {
363 t.Errorf("FlowExtractInfo() got1 = %v, want %v", got1, tt.want1)
364 return
365 }
366 if got2 != tt.want2 {
367 t.Errorf("FlowExtractInfo() got2 = %v, want %v", got2, tt.want2)
368 return
369 }
370 if got3 != tt.want3 {
371 t.Errorf("FlowExtractInfo() got3 = %v, want %v", got3, tt.want3)
372 return
373 }
374 if got4 != tt.want4 {
375 t.Errorf("FlowExtractInfo() got4 = %v, want %v", got4, tt.want4)
376 return
377 }
378 if got5 != tt.want5 {
379 t.Errorf("FlowExtractInfo() got5 = %v, want %v", got5, tt.want5)
380 return
381 }
382 })
383 }
384}