blob: e3b732874c13ded43c9be46829433abb1ce327a5 [file] [log] [blame]
Akash Soni9fad7362023-10-03 12:19:37 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16package application
17
18import (
19 "context"
20 "net"
21 "reflect"
22 "sync"
23 "testing"
24 "voltha-go-controller/internal/pkg/controller"
25 "voltha-go-controller/internal/pkg/of"
26 common "voltha-go-controller/internal/pkg/types"
27 "voltha-go-controller/internal/pkg/util"
28 "voltha-go-controller/internal/test/mocks"
29
30 "github.com/golang/mock/gomock"
31 "github.com/stretchr/testify/assert"
32)
33
34func Test_newIgmpProfile(t *testing.T) {
35 type args struct {
36 igmpProfileConfig *common.IGMPConfig
37 }
38 b := true
39 tests := []struct {
40 name string
41 args args
42 want *IgmpProfile
43 }{
44 {
45 name: "DelExclSource",
46 args: args{
47 igmpProfileConfig: &common.IGMPConfig{
48 FastLeave: &b,
49 PeriodicQuery: &b,
50 WithRAUpLink: &b,
51 WithRADownLink: &b,
52 },
53 },
54 want: &IgmpProfile{},
55 },
56 }
57 for _, tt := range tests {
58 t.Run(tt.name, func(t *testing.T) {
59 got := newIgmpProfile(tt.args.igmpProfileConfig)
60 assert.NotNil(t, got)
61 })
62 }
63}
64
65func TestNewMvlanProfile(t *testing.T) {
66 type args struct {
67 name string
68 mvlan of.VlanType
69 ponVlan of.VlanType
70 isChannelBasedGroup bool
71 OLTSerialNums []string
72 actChannelPerPon uint32
73 }
74 tests := []struct {
75 name string
76 args args
77 want *MvlanProfile
78 }{
79 {
80 name: "DelExclSource",
81 args: args{
82 name: "test_mvlan",
83 },
84 want: &MvlanProfile{},
85 },
86 }
87
88 for _, tt := range tests {
89 t.Run(tt.name, func(t *testing.T) {
90 got := NewMvlanProfile(tt.args.name, tt.args.mvlan, tt.args.ponVlan, tt.args.isChannelBasedGroup, tt.args.OLTSerialNums, tt.args.actChannelPerPon)
91 assert.NotNil(t, got)
92 })
93 }
94}
95
96func TestMvlanProfile_AddMvlanProxy(t *testing.T) {
97 proxy := map[string]*MCGroupProxy{}
98 proxy["test_key"] = &MCGroupProxy{
99 Mode: common.Exclude,
100 }
101 grp := make(map[string]*MvlanGroup)
102 grp["test_key"] = &MvlanGroup{
103 Name: "test_key",
104 IsStatic: true,
105 }
106 type args struct {
107 name string
108 proxyInfo common.MulticastGroupProxy
109 }
110 tests := []struct {
111 name string
112 args args
113 }{
114 {
115 name: "AddMvlanProxy",
116 args: args{
117 name: "test_key",
118 proxyInfo: common.MulticastGroupProxy{
119 IsStatic: common.IsStaticYes,
120 },
121 },
122 },
123 }
124 for _, tt := range tests {
125 t.Run(tt.name, func(t *testing.T) {
126 mvp := &MvlanProfile{
127 Proxy: proxy,
128 Groups: grp,
129 }
130 mvp.AddMvlanProxy(tt.args.name, tt.args.proxyInfo)
131 })
132 }
133}
134
135func TestMvlanProfile_AddMvlanGroup(t *testing.T) {
136 type args struct {
137 name string
138 ips []string
139 }
140 grp := make(map[string]*MvlanGroup)
141 grp["test_key"] = &MvlanGroup{
142 Name: "test_key",
143 IsStatic: true,
144 }
145 tests := []struct {
146 name string
147 args args
148 }{
149 {
150 name: "AddMvlanProxy",
151 args: args{
152 name: "test_key",
153 ips: []string{
154 "0.0.0.0",
155 },
156 },
157 },
158 }
159 for _, tt := range tests {
160 t.Run(tt.name, func(t *testing.T) {
161 mvp := &MvlanProfile{
162 Groups: grp,
163 }
164 mvp.AddMvlanGroup(tt.args.name, tt.args.ips)
165 })
166 }
167}
168
169func TestMvlanProfile_GetUsMatchVlan(t *testing.T) {
170 tests := []struct {
171 name string
172 want of.VlanType
173 }{
174 {
175 name: "GetUsMatchVlan",
176 want: of.VlanAny,
177 },
178 {
179 name: "GetUsMatchVlan_IsPonVlanPresent",
180 want: of.VlanAny,
181 },
182 }
183 for _, tt := range tests {
184 t.Run(tt.name, func(t *testing.T) {
185 mvp := &MvlanProfile{
186 PonVlan: of.VlanAny,
187 Mvlan: of.VlanAny,
188 }
189 switch tt.name {
190 case "GetUsMatchVlan":
191 mvp.IsPonVlanPresent = true
192 if got := mvp.GetUsMatchVlan(); !reflect.DeepEqual(got, tt.want) {
193 t.Errorf("MvlanProfile.GetUsMatchVlan() = %v, want %v", got, tt.want)
194 }
195 case "GetUsMatchVlan_IsPonVlanPresent":
196 if got := mvp.GetUsMatchVlan(); !reflect.DeepEqual(got, tt.want) {
197 t.Errorf("MvlanProfile.GetUsMatchVlan() = %v, want %v", got, tt.want)
198 }
199 }
200 })
201 }
202}
203
204func TestMvlanProfile_isChannelStatic(t *testing.T) {
205 type args struct {
206 channel net.IP
207 }
208 grp := make(map[string]*MvlanGroup)
209 grp["test_key"] = &MvlanGroup{
210 Name: "test_key",
211 IsStatic: true,
212 McIPs: []string{
213 "224.0.0.1",
214 },
215 }
216 tests := []struct {
217 name string
218 args args
219 want bool
220 }{
221 {
222 name: "isChannelStatic",
223 args: args{
224 channel: AllSystemsMulticastGroupIP,
225 },
226 want: true,
227 },
228 {
229 name: "isChannelStatic_false",
230 want: false,
231 },
232 {
233 name: "containsStaticChannels",
234 want: true,
235 },
236 }
237 for _, tt := range tests {
238 t.Run(tt.name, func(t *testing.T) {
239 mvp := &MvlanProfile{
240 Groups: grp,
241 }
242 switch tt.name {
243 case "isChannelStatic", "isChannelStatic_false":
244 if got := mvp.isChannelStatic(tt.args.channel); got != tt.want {
245 t.Errorf("MvlanProfile.isChannelStatic() = %v, want %v", got, tt.want)
246 }
247
248 case "containsStaticChannels":
249 if got := mvp.containsStaticChannels(); got != tt.want {
250 t.Errorf("MvlanProfile.isChannelStatic() = %v, want %v", got, tt.want)
251 }
252 }
253 })
254 }
255}
256
257func TestMvlanProfile_getAllStaticChannels(t *testing.T) {
258 grp := make(map[string]*MvlanGroup)
259 grp["test_key"] = &MvlanGroup{
260 Name: "test_key",
261 IsStatic: true,
262 McIPs: []string{
263 "224.0.0.1",
264 },
265 }
266 tests := []struct {
267 name string
268 want []net.IP
269 want1 bool
270 }{
271 {
272 name: "getAllStaticChannels",
273 want: []net.IP{
274 AllSystemsMulticastGroupIP,
275 },
276 want1: true,
277 },
278 {
279 name: "getAllOldGroupStaticChannels",
280 want: []net.IP{
281 AllSystemsMulticastGroupIP,
282 },
283 want1: true,
284 },
285 }
286 for _, tt := range tests {
287 t.Run(tt.name, func(t *testing.T) {
288 mvp := &MvlanProfile{
289 Groups: grp,
290 oldGroups: grp,
291 }
292 switch tt.name {
293 case "getAllStaticChannels":
294 got, got1 := mvp.getAllStaticChannels()
295 if !reflect.DeepEqual(got, tt.want) {
296 t.Errorf("MvlanProfile.getAllStaticChannels() got = %v, want %v", got, tt.want)
297 }
298 if got1 != tt.want1 {
299 t.Errorf("MvlanProfile.getAllStaticChannels() got1 = %v, want %v", got1, tt.want1)
300 }
301 case "getAllOldGroupStaticChannels":
302 got, got1 := mvp.getAllOldGroupStaticChannels()
303 if !reflect.DeepEqual(got, tt.want) {
304 t.Errorf("MvlanProfile.getAllStaticChannels() got = %v, want %v", got, tt.want)
305 }
306 if got1 != tt.want1 {
307 t.Errorf("MvlanProfile.getAllStaticChannels() got1 = %v, want %v", got1, tt.want1)
308 }
309 }
310 })
311 }
312}
313
314func TestMvlanProfile_DelFlows(t *testing.T) {
315 type args struct {
316 cntx context.Context
317 device *VoltDevice
318 flow *of.VoltFlow
319 }
320 appMock := mocks.NewMockApp(gomock.NewController(t))
321 controller.NewController(ctx, appMock)
322 pendingDeleteFlow := map[string]map[string]bool{}
323 delFlow := map[string]bool{}
324 delFlow["SDX6320031"] = true
325 pendingDeleteFlow["SDX6320031"] = delFlow
326 voltDev := &VoltDevice{
327 Name: "SDX6320031",
328 SerialNum: "SDX6320031",
329 FlowDelEventMap: util.NewConcurrentMap(),
330 }
331 subFlows := map[uint64]*of.VoltSubFlow{}
332 vltSubFlow := &of.VoltSubFlow{
333 Cookie: 103112802816,
334 State: of.FlowAddSuccess,
335 }
336 subFlows[0] = vltSubFlow
337 flow := &of.VoltFlow{
338 PortName: "SDX6320031-1",
339 SubFlows: subFlows,
340 }
341 tests := []struct {
342 name string
343 args args
344 wantErr bool
345 }{
346 {
347 name: "getAllStaticChannels",
348 args: args{
349 cntx: context.Background(),
350 device: voltDev,
351 flow: flow,
352 },
353 wantErr: true,
354 },
355 }
356 for _, tt := range tests {
357 t.Run(tt.name, func(t *testing.T) {
358 mvp := &MvlanProfile{
359 PendingDeleteFlow: pendingDeleteFlow,
360 }
361 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
362 db = dbintf
363 dbintf.EXPECT().PutMvlan(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
364 if err := mvp.DelFlows(tt.args.cntx, tt.args.device, tt.args.flow); (err != nil) != tt.wantErr {
365 t.Errorf("MvlanProfile.DelFlows() error = %v, wantErr %v", err, tt.wantErr)
366 }
367 })
368 }
369}
370
371func TestMvlanProfile_generateGroupKey(t *testing.T) {
372 type args struct {
373 name string
374 ipAddr string
375 }
376 tests := []struct {
377 name string
378 args args
379 want string
380 }{
381 {
382 name: "generateGroupKey",
383 args: args{
384 name: "test-key",
385 ipAddr: "0.0.0.0",
386 },
387 want: "0_test-key",
388 },
389 {
390 name: "generateGroupKey_IsChannelBasedGroup",
391 args: args{
392 name: "test-key",
393 ipAddr: "0.0.0.0",
394 },
395 want: "0_0.0.0.0",
396 },
397 }
398 for _, tt := range tests {
399 t.Run(tt.name, func(t *testing.T) {
400 mvp := &MvlanProfile{}
401 switch tt.name {
402 case "generateGroupKey":
403 if got := mvp.generateGroupKey(tt.args.name, tt.args.ipAddr); got != tt.want {
404 t.Errorf("MvlanProfile.generateGroupKey() = %v, want %v", got, tt.want)
405 }
406 case "generateGroupKey_IsChannelBasedGroup":
407 mvp.IsChannelBasedGroup = true
408 if got := mvp.generateGroupKey(tt.args.name, tt.args.ipAddr); got != tt.want {
409 t.Errorf("MvlanProfile.generateGroupKey() = %v, want %v", got, tt.want)
410 }
411 }
412 })
413 }
414}
415
416func TestMvlanProfile_GetStaticGroupName(t *testing.T) {
417 type args struct {
418 gip net.IP
419 }
420 grp := make(map[string]*MvlanGroup)
421 grp["test_key"] = &MvlanGroup{
422 Name: "test_key",
423 IsStatic: true,
424 McIPs: []string{
425 "224.0.0.1",
426 },
427 }
428 tests := []struct {
429 name string
430 args args
431 want string
432 }{
433 {
434 name: "GetStaticGroupName",
435 args: args{
436 gip: AllSystemsMulticastGroupIP,
437 },
438 want: "test_key",
439 },
440 {
441 name: "GetStaticGroupName",
442 },
443 }
444 for _, tt := range tests {
445 t.Run(tt.name, func(t *testing.T) {
446 mvp := &MvlanProfile{
447 Groups: grp,
448 }
449 if got := mvp.GetStaticGroupName(tt.args.gip); got != tt.want {
450 t.Errorf("MvlanProfile.GetStaticGroupName() = %v, want %v", got, tt.want)
451 }
452 })
453 }
454}
455
456func TestMvlanProfile_pushIgmpMcastFlows(t *testing.T) {
457 type args struct {
458 cntx context.Context
459 OLTSerialNum string
460 }
461 grp := make(map[string]*MvlanGroup)
462 grp["test_key"] = &MvlanGroup{
463 Name: "test_key",
464 IsStatic: true,
465 McIPs: []string{
466 "224.0.0.1",
467 },
468 }
469 devicesList := make(map[string]OperInProgress)
470 devicesList["SDX6320031"] = opt82
471 va := GetApplication()
472 d := &VoltDevice{
473 Name: "SDX6320031",
474 SerialNum: "SDX6320031",
475 Ports: sync.Map{},
476 NniPort: "16777472",
477 }
478 voltPort := &VoltPort{
479 Name: "16777472",
480 Device: "SDX6320031",
481 ID: 16777472,
482 State: PortStateUp,
483 ChannelPerSubAlarmRaised: false,
484 Type: VoltPortTypeNni,
485 }
486 d.Ports.Store("16777472", voltPort)
487 va.DevicesDisc.Store("SDX6320031", d)
488 mvp := &MvlanProfile{
489 Name: "mvlan_test",
490 }
491 va.MvlanProfilesByTag.Store(of.VlanAny, mvp)
492 tests := []struct {
493 name string
494 args args
495 }{
496 {
497 name: "GetStaticGroupName",
498 args: args{
499 cntx: context.Background(),
500 OLTSerialNum: "SDX6320031",
501 },
502 },
503 }
504 for _, tt := range tests {
505 t.Run(tt.name, func(t *testing.T) {
506 mvp := &MvlanProfile{
507 DevicesList: devicesList,
508 Groups: grp,
509 Mvlan: of.VlanAny,
510 }
511 mvp.pushIgmpMcastFlows(tt.args.cntx, tt.args.OLTSerialNum)
512 })
513 }
514}
515
516func TestMvlanProfile_updateStaticGroups(t *testing.T) {
517 type args struct {
518 cntx context.Context
519 deviceID string
520 added []net.IP
521 removed []net.IP
522 }
523 devicesList := make(map[string]OperInProgress)
524 devicesList["SDX6320031"] = opt82
525 va := GetApplication()
526 d := &VoltDevice{
527 Name: "SDX6320031",
528 SerialNum: "SDX6320031",
529 Ports: sync.Map{},
530 NniPort: "16777472",
531 FlowDelEventMap: util.NewConcurrentMap(),
532 }
533 va.DevicesDisc.Store("SDX6320031", d)
534 tests := []struct {
535 name string
536 args args
537 }{
538 {
539 name: "updateStaticGroups",
540 args: args{
541 cntx: context.Background(),
542 deviceID: "SDX6320031",
543 added: []net.IP{AllSystemsMulticastGroupIP},
544 removed: []net.IP{AllSystemsMulticastGroupIP},
545 },
546 },
547 }
548 for _, tt := range tests {
549 t.Run(tt.name, func(t *testing.T) {
550 mvp := &MvlanProfile{
551 Name: "mvlan_test",
552 DevicesList: devicesList,
553 Mvlan: of.VlanAny,
554 }
555 va.MvlanProfilesByTag.Store(of.VlanAny, mvp)
556 va.MvlanProfilesByName.Store("mvlan_test", mvp)
557 mvp.updateStaticGroups(tt.args.cntx, tt.args.deviceID, tt.args.added, tt.args.removed)
558 })
559 }
560}
561func TestMvlanProfile_updateDynamicGroups(t *testing.T) {
562 type args struct {
563 cntx context.Context
564 deviceID string
565 added []net.IP
566 removed []net.IP
567 }
568 grp := make(map[string]*MvlanGroup)
569 grp["test_key"] = &MvlanGroup{
570 Name: "test_key",
571 IsStatic: true,
572 McIPs: []string{
573 "224.0.0.1",
574 },
575 }
576 devicesList := make(map[string]OperInProgress)
577 devicesList["SDX6320031"] = opt82
578 va := GetApplication()
579 d := &VoltDevice{
580 Name: "SDX6320031",
581 SerialNum: "SDX6320031",
582 Ports: sync.Map{},
583 NniPort: "16777472",
584 FlowDelEventMap: util.NewConcurrentMap(),
585 }
586 va.DevicesDisc.Store("SDX6320031", d)
587 devices := map[string]*IgmpGroupDevice{}
588 igmpDevice := &IgmpGroupDevice{
589 Device: "SDX6320031",
590 SerialNo: "SDX6320031",
591 GroupName: "4096_test_key",
592 Mvlan: of.VlanAny,
593 GroupChannels: sync.Map{},
594 GroupAddr: AllSystemsMulticastGroupIP,
595 }
596 devices["SDX6320031"] = igmpDevice
597 group := &IgmpGroup{
598 GroupName: "4096_test_key",
599 GroupID: uint32(256),
600 Mvlan: of.VlanAny,
601 Devices: devices,
602 }
603 va.IgmpGroups.Store("4096_test_key", group)
604 newReceivers := map[string]*IgmpGroupPort{}
605 igp := &IgmpGroupPort{
606 Port: "16777470",
607 }
608 newReceivers["16777472"] = igp
609 b := IgmpVersion2
610 igmpChanel := &IgmpGroupChannel{
611 GroupAddr: AllSystemsMulticastGroupIP,
612 GroupName: "test_key",
613 NewReceivers: newReceivers,
614 Version: IgmpVersion2,
615 ServVersion: &b,
616 }
617 igmpDevice.GroupChannels.Store(AllSystemsMulticastGroupIP.String(), igmpChanel)
618 proxy := map[string]*MCGroupProxy{}
619 mgGroupProxy := &MCGroupProxy{
620 Mode: common.Include,
621 SourceList: []net.IP{
622 AllSystemsMulticastGroupIP,
623 },
624 }
625 proxy[igmpChanel.GroupName] = mgGroupProxy
626 tests := []struct {
627 name string
628 args args
629 }{
630 {
631 name: "updateDynamicGroups",
632 args: args{
633 cntx: context.Background(),
634 deviceID: "SDX6320031",
635 added: []net.IP{AllSystemsMulticastGroupIP},
636 removed: []net.IP{AllSystemsMulticastGroupIP},
637 },
638 },
639 }
640 for _, tt := range tests {
641 t.Run(tt.name, func(t *testing.T) {
642 mvp := &MvlanProfile{
643 Name: "test_key",
644 DevicesList: devicesList,
645 Mvlan: of.VlanAny,
646 Groups: grp,
647 Proxy: proxy,
648 }
649 va.MvlanProfilesByTag.Store(of.VlanAny, mvp)
650 va.MvlanProfilesByName.Store("test_key", mvp)
651 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
652 db = dbintf
653 dbintf.EXPECT().PutIgmpRcvr(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
654 dbintf.EXPECT().PutIgmpChannel(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
655 mvp.updateDynamicGroups(tt.args.cntx, tt.args.deviceID, tt.args.added, tt.args.removed)
656 })
657 }
658}
659
660func TestMvlanProfile_checkStaticGrpSSMProxyDiff(t *testing.T) {
661 type args struct {
662 oldProxy *MCGroupProxy
663 newProxy *MCGroupProxy
664 }
665 tests := []struct {
666 name string
667 args args
668 want bool
669 }{
670 {
671 name: "updateDynamicGroups",
672 args: args{
673 oldProxy: &MCGroupProxy{
674 Mode: common.Exclude,
675 SourceList: []net.IP{
676 AllSystemsMulticastGroupIP,
677 },
678 },
679 newProxy: &MCGroupProxy{
680 Mode: common.Exclude,
681 SourceList: []net.IP{
682 AllSystemsMulticastGroupIP,
683 },
684 },
685 },
686 },
687 {
688 name: "updateDynamicGroups_true",
689 args: args{
690 newProxy: &MCGroupProxy{},
691 },
692 want: true,
693 },
694 {
695 name: "updateDynamicGroups_nil",
696 },
697 }
698 for _, tt := range tests {
699 t.Run(tt.name, func(t *testing.T) {
700 mvp := &MvlanProfile{}
701 if got := mvp.checkStaticGrpSSMProxyDiff(tt.args.oldProxy, tt.args.newProxy); got != tt.want {
702 t.Errorf("MvlanProfile.checkStaticGrpSSMProxyDiff() = %v, want %v", got, tt.want)
703 }
704 })
705 }
706}
707
708func TestMvlanProfile_UpdateActiveChannelSubscriberAlarm(t *testing.T) {
709 devicesList := make(map[string]OperInProgress)
710 devicesList["SDX6320031"] = opt82
711 voltDev := &VoltDevice{
712 Name: "SDX6320031",
713 SerialNum: "SDX6320031",
714 FlowDelEventMap: util.NewConcurrentMap(),
715 Ports: sync.Map{},
716 }
717 va := GetApplication()
718 va.DevicesDisc.Store("SDX6320031", voltDev)
719 voltPort := &VoltPort{
720 Name: "16777472",
721 Device: "SDX6320031",
722 ID: 16777472,
723 State: PortStateUp,
724 ChannelPerSubAlarmRaised: true,
725 Type: VoltPortTypeAccess,
726 ActiveChannels: uint32(2),
727 }
728 voltDev.Ports.Store("16777472", voltPort)
729 tests := []struct {
730 name string
731 }{
732 {
733 name: "UpdateActiveChannelSubscriberAlarm",
734 },
735 {
736 name: "UpdateActiveChannelSubscriberAlarm_else",
737 },
738 }
739 for _, tt := range tests {
740 t.Run(tt.name, func(t *testing.T) {
741 mvp := &MvlanProfile{
742 DevicesList: devicesList,
743 MaxActiveChannels: uint32(5),
744 }
745 switch tt.name {
746 case "UpdateActiveChannelSubscriberAlarm":
747 mvp.UpdateActiveChannelSubscriberAlarm()
748 case "UpdateActiveChannelSubscriberAlarm_else":
749 voltPort.ActiveChannels = uint32(6)
750 voltPort.ChannelPerSubAlarmRaised = false
751 mvp.UpdateActiveChannelSubscriberAlarm()
752 }
753 })
754 }
755}