blob: 451abd19e8861a50c3dedae8378e22331f46303f [file] [log] [blame]
Matteo Scandolo8a574812021-05-20 15:18:53 -07001/*
Joey Armstrong14628cd2023-01-10 08:38:31 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo8a574812021-05-20 15:18:53 -07003
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 },
Andrea Campanella29890452022-02-03 16:00:19 +0100119 {"newUniPort-tt-maclearning-pppoe",
120 args{
121 services: []common.ServiceYaml{
122 {Name: hsia, CTag: 900, CTagAllocation: common.TagAllocationUnique.String(), STag: 900, STagAllocation: common.TagAllocationShared.String(), UniTagMatch: 35, TechnologyProfileID: 64},
123 {Name: voip, CTag: 444, CTagAllocation: common.TagAllocationShared.String(), STag: 333, STagAllocation: common.TagAllocationShared.String(), UniTagMatch: 65, TechnologyProfileID: 65, EnableMacLearning: true, UsPonCTagPriority: 7, UsPonSTagPriority: 7, DsPonCTagPriority: 7, DsPonSTagPriority: 7},
124 {Name: vod, CTag: 55, CTagAllocation: common.TagAllocationShared.String(), STag: 555, STagAllocation: common.TagAllocationShared.String(), UniTagMatch: 55, TechnologyProfileID: 66, NeedsDhcp: true, NeedsIgmp: true, NeedsPPPoE: true, EnableMacLearning: true, UsPonCTagPriority: 5, UsPonSTagPriority: 5, DsPonCTagPriority: 5, DsPonSTagPriority: 5},
125 },
126 nextCtag: map[string]int{hsia: 920},
127 nextStag: map[string]int{hsia: 900},
128 },
129 wants{
130 expectedCtags: map[string]int{hsia: 921, voip: 444, vod: 55},
131 expectedStags: map[string]int{hsia: 900, voip: 333, vod: 555},
132 },
133 },
Matteo Scandolo8a574812021-05-20 15:18:53 -0700134 }
135
136 for _, tt := range tests {
137 t.Run(tt.name, func(t *testing.T) {
138 common.Services = tt.args.services
139
140 onu := &Onu{
141 ID: 0,
142 SerialNumber: NewSN(1, 1, 1),
143 }
144
145 uni, err := NewUniPort(1, onu, tt.args.nextCtag, tt.args.nextStag)
146
147 assert.NoError(t, err)
148
149 assert.Equal(t, uint32(1), uni.ID)
150 assert.Equal(t, uint16(258), uni.MeId.ToUint16())
151 assert.Equal(t, len(tt.args.services), len(uni.Services))
152
153 assert.Equal(t, len(tt.args.services), len(uni.Services))
154
155 for i, configuredService := range tt.args.services {
156 service := uni.Services[i].(*Service)
157 assert.Equal(t, tt.wants.expectedCtags[service.Name], service.CTag)
158 assert.Equal(t, tt.wants.expectedStags[service.Name], service.STag)
159 assert.Equal(t, configuredService.NeedsEapol, service.NeedsEapol)
160 assert.Equal(t, configuredService.NeedsDhcp, service.NeedsDhcp)
161 assert.Equal(t, configuredService.NeedsIgmp, service.NeedsIgmp)
Andrea Campanella29890452022-02-03 16:00:19 +0100162 assert.Equal(t, configuredService.NeedsPPPoE, service.NeedsPPPoE)
Matteo Scandolo8a574812021-05-20 15:18:53 -0700163 assert.Equal(t, configuredService.UniTagMatch, service.UniTagMatch)
164 assert.Equal(t, configuredService.TechnologyProfileID, service.TechnologyProfileID)
165 assert.Equal(t, configuredService.ConfigureMacAddress, service.ConfigureMacAddress)
Andrea Campanella29890452022-02-03 16:00:19 +0100166 assert.Equal(t, configuredService.EnableMacLearning, service.EnableMacLearning)
Matteo Scandolo8a574812021-05-20 15:18:53 -0700167 assert.Equal(t, configuredService.UsPonCTagPriority, service.UsPonCTagPriority)
168 assert.Equal(t, configuredService.DsPonCTagPriority, service.DsPonCTagPriority)
169 assert.Equal(t, configuredService.UsPonSTagPriority, service.UsPonSTagPriority)
170 assert.Equal(t, configuredService.DsPonSTagPriority, service.DsPonSTagPriority)
171 }
172 })
173 }
174}
175
176func Test_AddGemPortToService_eapol(t *testing.T) {
177
178 uni := createTestUni()
179 hsia := Service{Name: "hsia", NeedsEapol: true, CTag: 900, UniPort: uni}
180 voip := Service{Name: "voip", NeedsEapol: false, CTag: 55, UniPort: uni}
181 uni.Services = []ServiceIf{&hsia, &voip}
182 uni.addGemPortToService(1024, uint32(layers.EthernetTypeEAPOL), 0, 0)
183
184 assert.Equal(t, hsia.GemPort, uint32(1024))
185 assert.Equal(t, voip.GemPort, uint32(0))
186}
187
188func Test_AddGemPortToService_dhcp(t *testing.T) {
189
190 uni := createTestUni()
191 hsia := Service{Name: "hsia", NeedsEapol: true, UniPort: uni}
192 voip := Service{Name: "voip", NeedsDhcp: true, CTag: 900, UniPort: uni}
193 mc := Service{Name: "mc", CTag: 900, UniPort: uni}
194 uni.Services = []ServiceIf{&hsia, &voip, &mc}
195 uni.addGemPortToService(1025, uint32(layers.EthernetTypeIPv4), 900, 0)
196
197 assert.Equal(t, hsia.GemPort, uint32(0))
198 assert.Equal(t, voip.GemPort, uint32(1025))
199 assert.Equal(t, mc.GemPort, uint32(0))
200}
201
202func Test_AddGemPortToService_dataplane(t *testing.T) {
203
204 uni := createTestUni()
205 hsia := Service{Name: "hsia", NeedsEapol: true, CTag: 900, STag: 500, UniPort: uni}
206 voip := Service{Name: "voip", NeedsDhcp: true, CTag: 900, UniPort: uni}
207 uni.Services = []ServiceIf{&hsia, &voip}
208 uni.addGemPortToService(1024, uint32(layers.EthernetTypeLLC), 500, 900)
209
210 assert.Equal(t, hsia.GemPort, uint32(1024))
211 assert.Equal(t, voip.GemPort, uint32(0))
212}
213
214func Test_FindServiceByMacAddress(t *testing.T) {
215
216 mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(2)}
217
218 uni := createTestUni()
219 hsia := Service{Name: "hsia", HwAddress: net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)}, UniPort: uni}
220 voip := Service{Name: "voip", HwAddress: mac, UniPort: uni}
221 vod := Service{Name: "vod", HwAddress: net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(3)}, UniPort: uni}
222 uni.Services = []ServiceIf{&hsia, &voip, &vod}
223
224 service, err := uni.findServiceByMacAddress(mac)
225 assert.NoError(t, err)
226 assert.Equal(t, service.HwAddress.String(), mac.String())
227}