blob: 6ff32db74f29a9408767e004a43d58040193cc85 [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"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070022 fu "github.com/opencord/voltha-lib-go/v5/pkg/flows"
Thomas Lee S94109f12020-03-03 16:39:29 +053023 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070024 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
25 "github.com/opencord/voltha-protos/v4/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 }
Kent Hagermane6ff1012020-07-14 15:07:53 -0400203 input := uint32(2*math.Pow(2, 28)) | 3
kdarapu891693b2019-09-16 12:33:49 +0530204 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)),
Andrey Pozolotin32b36562021-06-02 10:23:26 +0300316 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
kdarapu891693b2019-09-16 12:33:49 +0530317 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 }
divyadesaid26f6b12020-03-19 06:30:28 +0000326 ofpstats, _ := fu.MkFlowStat(fa)
kdarapu891693b2019-09-16 12:33:49 +0530327 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) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000352 got, got1, got2, got3, got4, got5, err := FlowExtractInfo(context.Background(), tt.args.flow, tt.args.flowDirection)
kdarapu891693b2019-09-16 12:33:49 +0530353 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}