blob: 7a799b831e48a778ef588885a4bbc35aa51c8c1d [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
Scott Baker51290152019-10-24 14:23:20 -070025 fu "github.com/opencord/voltha-lib-go/v2/pkg/flows"
Scott Bakerc6e54cb2019-11-04 09:31:25 -080026 ofp "github.com/opencord/voltha-protos/v2/go/openflow_13"
27 "github.com/opencord/voltha-protos/v2/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 {
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.
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000201 {"IntfIDToPortTypeName-1", args{intfID: 1048576}, voltha.Port_ETHERNET_NNI},
kdarapu891693b2019-09-16 12:33:49 +0530202 {"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 {
kdarapuf0c0e382019-09-30 05:26:31 +0530220 name string
221 args args
222 port uint32
223 IntfID uint32
224 onuID uint32
225 uniID uint32
kdarapu891693b2019-09-16 12:33:49 +0530226 }{
227 // TODO: Add test cases.
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000228 {"ExtractAccessFromFlow-1", args{inPort: 100, outPort: 1048576}, 100, 0, 6, 4},
229 {"ExtractAccessFromFlow-2", args{inPort: 1048576, outPort: 10}, 10, 0, 0, 10},
kdarapu891693b2019-09-16 12:33:49 +0530230 }
231 for _, tt := range tests {
232 t.Run(tt.name, func(t *testing.T) {
233 got, got1, got2, got3 := ExtractAccessFromFlow(tt.args.inPort, tt.args.outPort)
kdarapuf0c0e382019-09-30 05:26:31 +0530234 if got != tt.port {
235 t.Errorf("ExtractAccessFromFlow() got = %v, want %v", got, tt.port)
kdarapu891693b2019-09-16 12:33:49 +0530236 }
kdarapuf0c0e382019-09-30 05:26:31 +0530237 if got1 != tt.IntfID {
238 t.Errorf("ExtractAccessFromFlow() got1 = %v, want %v", got1, tt.IntfID)
kdarapu891693b2019-09-16 12:33:49 +0530239 }
kdarapuf0c0e382019-09-30 05:26:31 +0530240 if got2 != tt.onuID {
241 t.Errorf("ExtractAccessFromFlow() got2 = %v, want %v", got2, tt.onuID)
kdarapu891693b2019-09-16 12:33:49 +0530242 }
kdarapuf0c0e382019-09-30 05:26:31 +0530243 if got3 != tt.uniID {
244 t.Errorf("ExtractAccessFromFlow() got3 = %v, want %v", got3, tt.uniID)
kdarapu891693b2019-09-16 12:33:49 +0530245 }
246 })
247 }
kdarapuf0c0e382019-09-30 05:26:31 +0530248 //t.Error()
kdarapu891693b2019-09-16 12:33:49 +0530249}
250
251func TestIsUpstream(t *testing.T) {
252 type args struct {
253 outPort uint32
254 }
255 tests := []struct {
256 name string
257 args args
258 want bool
259 }{
260 // TODO: Add test cases.
261 {"TestIsUpstream-1", args{outPort: 65533}, true},
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000262 {"TestIsUpstream-2", args{outPort: 1048576}, true},
263 {"TestIsUpstream-3", args{outPort: 1048577}, true},
264 {"TestIsUpstream-4", args{outPort: 1048578}, true},
kdarapu891693b2019-09-16 12:33:49 +0530265 {"TestIsUpstream-6", args{outPort: 1000}, false},
266 }
267 for _, tt := range tests {
268 t.Run(tt.name, func(t *testing.T) {
269 if got := IsUpstream(tt.args.outPort); got != tt.want {
270 t.Errorf("IsUpstream() = %v, want %v", got, tt.want)
271 }
272 })
273 }
274}
275
276func TestIsControllerBoundFlow(t *testing.T) {
277 type args struct {
278 outPort uint32
279 }
280 tests := []struct {
281 name string
282 args args
283 want bool
284 }{
285 // TODO: Add test cases.
286 {"IsControllerBoundFlow-1", args{outPort: 65533}, true},
287 {"IsControllerBoundFlow-2", args{outPort: 65536}, false},
288 {"IsControllerBoundFlow-3", args{outPort: 65537}, false},
289 {"IsControllerBoundFlow-4", args{outPort: 65538}, false},
290 {"IsControllerBoundFlow-5", args{outPort: 65539}, false},
291 {"IsControllerBoundFlow-6", args{outPort: 1000}, false},
292 }
293 for _, tt := range tests {
294 t.Run(tt.name, func(t *testing.T) {
295 if got := IsControllerBoundFlow(tt.args.outPort); got != tt.want {
296 t.Errorf("IsControllerBoundFlow() = %v, want %v", got, tt.want)
297 }
298 })
299 }
300}
301
302func TestFlowExtractInfo(t *testing.T) {
303 fa := &fu.FlowArgs{
304 MatchFields: []*ofp.OfpOxmOfbField{
305 fu.InPort(2),
kdarapuf0c0e382019-09-30 05:26:31 +0530306 fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2)),
kdarapu891693b2019-09-16 12:33:49 +0530307 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 0),
308 fu.EthType(2048),
309 },
310
311 Actions: []*ofp.OfpAction{
312 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
313 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
314 fu.Output(1),
315 },
316 }
317 ofpstats := fu.MkFlowStat(fa)
318 type args struct {
319 flow *ofp.OfpFlowStats
320 flowDirection string
321 }
322 tests := []struct {
323 name string
324 args args
325 want uint32
326 want1 uint32
327 want2 uint32
328 want3 uint32
329 want4 uint32
330 want5 uint32
331 wantErr bool
332 }{
333 // TODO: Add test cases.
334 {"FlowExtractInfo-1", args{flow: ofpstats, flowDirection: "upstream"}, 2, 0, 0, 2, 0, 0, false},
335
336 // Negative Testcases
kdarapuf0c0e382019-09-30 05:26:31 +0530337 {"FlowExtractInfo-2", args{flow: ofpstats, flowDirection: "downstream"}, 1, 0, 0, 1, 2, 2048, false},
kdarapu891693b2019-09-16 12:33:49 +0530338 {"FlowExtractInfo-3", args{flow: nil, flowDirection: "downstream"}, 0, 0, 0, 0, 0, 0, true},
339 {"FlowExtractInfo-4", args{flow: &ofp.OfpFlowStats{}, flowDirection: "downstream"}, 0, 0, 0, 0, 0, 0, true},
340 }
341 for _, tt := range tests {
342 t.Run(tt.name, func(t *testing.T) {
343 got, got1, got2, got3, got4, got5, err := FlowExtractInfo(tt.args.flow, tt.args.flowDirection)
344 if (err != nil) != tt.wantErr {
345 t.Errorf("FlowExtractInfo() error = %v, wantErr %v", err, tt.wantErr)
346 return
347 }
348 if got != tt.want {
349 t.Errorf("FlowExtractInfo() got = %v, want %v", got, tt.want)
350 return
351 }
352 if got1 != tt.want1 {
353 t.Errorf("FlowExtractInfo() got1 = %v, want %v", got1, tt.want1)
354 return
355 }
356 if got2 != tt.want2 {
357 t.Errorf("FlowExtractInfo() got2 = %v, want %v", got2, tt.want2)
358 return
359 }
360 if got3 != tt.want3 {
361 t.Errorf("FlowExtractInfo() got3 = %v, want %v", got3, tt.want3)
362 return
363 }
364 if got4 != tt.want4 {
365 t.Errorf("FlowExtractInfo() got4 = %v, want %v", got4, tt.want4)
366 return
367 }
368 if got5 != tt.want5 {
369 t.Errorf("FlowExtractInfo() got5 = %v, want %v", got5, tt.want5)
370 return
371 }
372 })
373 }
374}