blob: 4311e80a0625624fda43570e9c5ad9738dc2b862 [file] [log] [blame]
Matteo Scandolo8a574812021-05-20 15:18:53 -07001/*
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
17package devices
18
19import (
20 "github.com/google/gopacket/layers"
21 "github.com/opencord/bbsim/internal/common"
22 omcilib "github.com/opencord/bbsim/internal/common/omci"
23 "github.com/stretchr/testify/assert"
24 "net"
25 "testing"
26)
27
28func createTestUni() *UniPort {
29 onu := &Onu{
30 ID: 0,
31 SerialNumber: NewSN(1, 1, 1),
32 PonPortID: 0,
33 }
34
35 services := []*Service{
36 {Name: "hsia", HwAddress: net.HardwareAddr{0x2e, 0x00}},
37 }
38
39 uni := UniPort{
40 ID: 1,
41 MeId: omcilib.GenerateUniPortEntityId(1),
42 PortNo: 16,
43 Onu: onu,
44 logger: uniLogger,
45 }
46
47 for _, s := range services {
48 uni.Services = append(uni.Services, s)
49 }
50 return &uni
51}
52
53func TestNewUniPortAtt(t *testing.T) {
54
55 const (
56 hsia = "hsia"
57 voip = "voip"
58 vod = "vod"
59 mc = "mc"
60 )
61
62 type args struct {
63 services []common.ServiceYaml
64 nextCtag map[string]int
65 nextStag map[string]int
66 }
67
68 type wants struct {
69 expectedCtags map[string]int
70 expectedStags map[string]int
71 }
72
73 tests := []struct {
74 name string
75 args args
76 wants wants
77 }{
78 {"newUniPort-att",
79 args{
80 services: []common.ServiceYaml{
81 {Name: hsia, CTag: 900, CTagAllocation: common.TagAllocationUnique.String(), STag: 900, STagAllocation: common.TagAllocationShared.String(), NeedsEapol: true, NeedsDhcp: true, NeedsIgmp: true},
82 },
83 nextCtag: map[string]int{hsia: 920},
84 nextStag: map[string]int{hsia: 900},
85 },
86 wants{
87 expectedCtags: map[string]int{hsia: 921},
88 expectedStags: map[string]int{hsia: 900},
89 },
90 },
91 {"newUniPort-dt",
92 args{
93 services: []common.ServiceYaml{
94 {Name: hsia, CTag: 900, CTagAllocation: common.TagAllocationShared.String(), STag: 900, STagAllocation: common.TagAllocationUnique.String(), UniTagMatch: 4096},
95 },
96 nextCtag: map[string]int{hsia: 920},
97 nextStag: map[string]int{hsia: 900},
98 },
99 wants{
100 expectedCtags: map[string]int{hsia: 920},
101 expectedStags: map[string]int{hsia: 901},
102 },
103 },
104 {"newUniPort-tt",
105 args{
106 services: []common.ServiceYaml{
107 {Name: hsia, CTag: 900, CTagAllocation: common.TagAllocationUnique.String(), STag: 900, STagAllocation: common.TagAllocationShared.String(), UniTagMatch: 35, TechnologyProfileID: 64},
108 {Name: voip, CTag: 444, CTagAllocation: common.TagAllocationShared.String(), STag: 333, STagAllocation: common.TagAllocationShared.String(), UniTagMatch: 65, TechnologyProfileID: 65, ConfigureMacAddress: true, UsPonCTagPriority: 7, UsPonSTagPriority: 7, DsPonCTagPriority: 7, DsPonSTagPriority: 7},
109 {Name: vod, CTag: 55, CTagAllocation: common.TagAllocationShared.String(), STag: 555, STagAllocation: common.TagAllocationShared.String(), UniTagMatch: 55, TechnologyProfileID: 66, NeedsDhcp: true, NeedsIgmp: true, ConfigureMacAddress: true, UsPonCTagPriority: 5, UsPonSTagPriority: 5, DsPonCTagPriority: 5, DsPonSTagPriority: 5},
110 },
111 nextCtag: map[string]int{hsia: 920},
112 nextStag: map[string]int{hsia: 900},
113 },
114 wants{
115 expectedCtags: map[string]int{hsia: 921, voip: 444, vod: 55},
116 expectedStags: map[string]int{hsia: 900, voip: 333, vod: 555},
117 },
118 },
119 }
120
121 for _, tt := range tests {
122 t.Run(tt.name, func(t *testing.T) {
123 common.Services = tt.args.services
124
125 onu := &Onu{
126 ID: 0,
127 SerialNumber: NewSN(1, 1, 1),
128 }
129
130 uni, err := NewUniPort(1, onu, tt.args.nextCtag, tt.args.nextStag)
131
132 assert.NoError(t, err)
133
134 assert.Equal(t, uint32(1), uni.ID)
135 assert.Equal(t, uint16(258), uni.MeId.ToUint16())
136 assert.Equal(t, len(tt.args.services), len(uni.Services))
137
138 assert.Equal(t, len(tt.args.services), len(uni.Services))
139
140 for i, configuredService := range tt.args.services {
141 service := uni.Services[i].(*Service)
142 assert.Equal(t, tt.wants.expectedCtags[service.Name], service.CTag)
143 assert.Equal(t, tt.wants.expectedStags[service.Name], service.STag)
144 assert.Equal(t, configuredService.NeedsEapol, service.NeedsEapol)
145 assert.Equal(t, configuredService.NeedsDhcp, service.NeedsDhcp)
146 assert.Equal(t, configuredService.NeedsIgmp, service.NeedsIgmp)
147 assert.Equal(t, configuredService.UniTagMatch, service.UniTagMatch)
148 assert.Equal(t, configuredService.TechnologyProfileID, service.TechnologyProfileID)
149 assert.Equal(t, configuredService.ConfigureMacAddress, service.ConfigureMacAddress)
150 assert.Equal(t, configuredService.UsPonCTagPriority, service.UsPonCTagPriority)
151 assert.Equal(t, configuredService.DsPonCTagPriority, service.DsPonCTagPriority)
152 assert.Equal(t, configuredService.UsPonSTagPriority, service.UsPonSTagPriority)
153 assert.Equal(t, configuredService.DsPonSTagPriority, service.DsPonSTagPriority)
154 }
155 })
156 }
157}
158
159func Test_AddGemPortToService_eapol(t *testing.T) {
160
161 uni := createTestUni()
162 hsia := Service{Name: "hsia", NeedsEapol: true, CTag: 900, UniPort: uni}
163 voip := Service{Name: "voip", NeedsEapol: false, CTag: 55, UniPort: uni}
164 uni.Services = []ServiceIf{&hsia, &voip}
165 uni.addGemPortToService(1024, uint32(layers.EthernetTypeEAPOL), 0, 0)
166
167 assert.Equal(t, hsia.GemPort, uint32(1024))
168 assert.Equal(t, voip.GemPort, uint32(0))
169}
170
171func Test_AddGemPortToService_dhcp(t *testing.T) {
172
173 uni := createTestUni()
174 hsia := Service{Name: "hsia", NeedsEapol: true, UniPort: uni}
175 voip := Service{Name: "voip", NeedsDhcp: true, CTag: 900, UniPort: uni}
176 mc := Service{Name: "mc", CTag: 900, UniPort: uni}
177 uni.Services = []ServiceIf{&hsia, &voip, &mc}
178 uni.addGemPortToService(1025, uint32(layers.EthernetTypeIPv4), 900, 0)
179
180 assert.Equal(t, hsia.GemPort, uint32(0))
181 assert.Equal(t, voip.GemPort, uint32(1025))
182 assert.Equal(t, mc.GemPort, uint32(0))
183}
184
185func Test_AddGemPortToService_dataplane(t *testing.T) {
186
187 uni := createTestUni()
188 hsia := Service{Name: "hsia", NeedsEapol: true, CTag: 900, STag: 500, UniPort: uni}
189 voip := Service{Name: "voip", NeedsDhcp: true, CTag: 900, UniPort: uni}
190 uni.Services = []ServiceIf{&hsia, &voip}
191 uni.addGemPortToService(1024, uint32(layers.EthernetTypeLLC), 500, 900)
192
193 assert.Equal(t, hsia.GemPort, uint32(1024))
194 assert.Equal(t, voip.GemPort, uint32(0))
195}
196
197func Test_FindServiceByMacAddress(t *testing.T) {
198
199 mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(2)}
200
201 uni := createTestUni()
202 hsia := Service{Name: "hsia", HwAddress: net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)}, UniPort: uni}
203 voip := Service{Name: "voip", HwAddress: mac, UniPort: uni}
204 vod := Service{Name: "vod", HwAddress: net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(3)}, UniPort: uni}
205 uni.Services = []ServiceIf{&hsia, &voip, &vod}
206
207 service, err := uni.findServiceByMacAddress(mac)
208 assert.NoError(t, err)
209 assert.Equal(t, service.HwAddress.String(), mac.String())
210}