blob: 83a8f5b79caa246b32d7ab985a32f4631dd07117 [file] [log] [blame]
Matteo Scandolo4a036262020-08-17 15:56:13 -07001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo4a036262020-08-17 15:56:13 -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 common
18
19import (
Elia Battistonb7bea222022-02-18 16:25:00 +010020 "fmt"
Matteo Scandolo4a036262020-08-17 15:56:13 -070021 "testing"
Elia Battistonb7bea222022-02-18 16:25:00 +010022
23 "github.com/imdario/mergo"
24 "gotest.tools/assert"
Matteo Scandolo4a036262020-08-17 15:56:13 -070025)
26
27func TestLoadBBSimServices(t *testing.T) {
28 services, err := loadBBSimServices("../../configs/att-services.yaml")
29
30 assert.NilError(t, err)
31
32 assert.Equal(t, services[0].Name, "hsia")
33 assert.Equal(t, services[0].CTag, 900)
34 assert.Equal(t, services[0].STag, 900)
35 assert.Equal(t, services[0].CTagAllocation, TagAllocationUnique.String())
36 assert.Equal(t, services[0].STagAllocation, TagAllocationShared.String())
37 assert.Equal(t, services[0].NeedsEapol, true)
Matteo Scandolo8a574812021-05-20 15:18:53 -070038 assert.Equal(t, services[0].NeedsDhcp, true)
Matteo Scandolo4a036262020-08-17 15:56:13 -070039 assert.Equal(t, services[0].NeedsIgmp, false)
40 assert.Equal(t, services[0].TechnologyProfileID, 64)
41}
Elia Battistonb7bea222022-02-18 16:25:00 +010042
43func TestLoadPonsConfigDefaults(t *testing.T) {
Holger Hildebrandtb9a3dd02022-05-12 08:20:41 +000044 Config = GetDefaultOps()
Elia Battistonb7bea222022-02-18 16:25:00 +010045 // The default options define 1 PON per OLT
46 // and 1 ONU per PON
47
48 Services = []ServiceYaml{
49 {
50 Name: "test",
51 },
52 }
53
54 ponsConf, err := getDefaultPonsConfig()
55
56 assert.NilError(t, err, "Can't get defaults")
57
58 assert.Equal(t, ponsConf.Number, uint32(1))
59 assert.Equal(t, len(ponsConf.Ranges), 1)
60
61 ranges := ponsConf.Ranges
62
63 //The default should replicate the old way bbsim used to compute ranges
64 assert.Equal(t, ranges[0].PonRange, IdRange{0, 0})
65 assert.Equal(t, ranges[0].Technology, XGSPON.String())
66 assert.Equal(t, ranges[0].OnuRange, IdRange{defaultOnuIdStart, defaultOnuIdStart})
67 assert.Equal(t, ranges[0].AllocIdRange, IdRange{defaultAllocIdStart, defaultAllocIdStart + 4})
68 assert.Equal(t, ranges[0].GemportRange, IdRange{defaultGemportIdStart, defaultGemportIdStart + 32})
69
70 assert.NilError(t, validatePonsConfig(ponsConf), "Configuration is not valid")
71}
72
73func TestLoadPonsConfigFile(t *testing.T) {
74
Holger Hildebrandtb9a3dd02022-05-12 08:20:41 +000075 Config = GetDefaultOps()
Elia Battistonb7bea222022-02-18 16:25:00 +010076
77 Services = []ServiceYaml{
78 {
79 Name: "test",
80 },
81 }
82
83 ponsConf, err := getDefaultPonsConfig()
84
85 assert.NilError(t, err, "Can't get defaults")
86
87 yamlConf, err := loadBBSimPons("../../configs/pon-interfaces.yaml")
88
89 assert.NilError(t, err, "Can't read config file")
90
91 // merging Yaml and Default Values
92 err = mergo.Merge(ponsConf, yamlConf, mergo.WithOverride)
93 assert.NilError(t, err, "Can't merge YAML and Config")
94
95 assert.Equal(t, ponsConf.Number, uint32(16))
96 assert.Equal(t, len(ponsConf.Ranges), 1)
97
98 ranges := ponsConf.Ranges
99
100 assert.Equal(t, ranges[0].PonRange, IdRange{0, 15})
101 assert.Equal(t, ranges[0].Technology, XGSPON.String())
102 assert.Equal(t, ranges[0].OnuRange, IdRange{1, 255})
103 assert.Equal(t, ranges[0].AllocIdRange, IdRange{256, 1024})
104 assert.Equal(t, ranges[0].GemportRange, IdRange{256, 1024})
105
106 assert.NilError(t, validatePonsConfig(ponsConf), "Configuration is not valid")
107}
108
109func getTestPonsConfiguration() *PonPortsConfig {
110 return &PonPortsConfig{
111 Number: 16,
112 Ranges: []PonRangeConfig{
113 {
114 PonRange: IdRange{0, 7},
115 Technology: GPON.String(),
116 OnuRange: IdRange{defaultOnuIdStart, defaultOnuIdStart},
117 AllocIdRange: IdRange{defaultAllocIdStart, defaultAllocIdStart + 4},
118 GemportRange: IdRange{defaultGemportIdStart, defaultGemportIdStart + 32},
119 },
120 {
121 PonRange: IdRange{8, 15},
122 Technology: XGSPON.String(),
123 OnuRange: IdRange{defaultOnuIdStart, defaultOnuIdStart},
124 AllocIdRange: IdRange{defaultAllocIdStart, defaultAllocIdStart + 4},
125 GemportRange: IdRange{defaultGemportIdStart, defaultGemportIdStart + 32},
126 },
127 },
128 }
129}
130
131func TestPonsValidationTechnology(t *testing.T) {
132 ponsConf := getTestPonsConfiguration()
133 assert.NilError(t, validatePonsConfig(ponsConf), "Test configuration is not valid")
134
135 ponsConf.Ranges[0].Technology = XGSPON.String()
136 assert.NilError(t, validatePonsConfig(ponsConf), "Correct technology considered invalid")
137
138 ponsConf.Ranges[0].Technology = GPON.String()
139 assert.NilError(t, validatePonsConfig(ponsConf), "Correct technology considered invalid")
140
141 ponsConf.Ranges[0].Technology = "TEST"
142 assert.ErrorContains(t, validatePonsConfig(ponsConf), "technology", "Incorrect technology considered valid")
143}
144
145func TestPonsValidationPortsInRanges(t *testing.T) {
146 ponsConf := getTestPonsConfiguration()
147 assert.NilError(t, validatePonsConfig(ponsConf), "Test configuration is not valid")
148
149 //The second range now misses pon 8
150 ponsConf.Ranges[1].PonRange.StartId = 9
151
152 assert.ErrorContains(t, validatePonsConfig(ponsConf), "not-defined", "Missing pon definition considered valid")
153
154 //The second range defines pon 7 a second time
155 ponsConf.Ranges[1].PonRange.StartId = 7
156
157 assert.ErrorContains(t, validatePonsConfig(ponsConf), "duplicate", "Duplicate pon definition considered valid")
158
159 //Get back to a known good configuration
160 ponsConf = getTestPonsConfiguration()
161 //The second range uses an Id that is out of range
162 ponsConf.Ranges[1].PonRange.EndId = 16
163
164 assert.ErrorContains(t, validatePonsConfig(ponsConf), "max", "Out of range pon definition considered valid")
165
166 //Fix the start of the second range
167 ponsConf.Ranges[1].PonRange.EndId = 15
168
169 ponsConf.Number = 0
170
171 assert.ErrorContains(t, validatePonsConfig(ponsConf), "no-pon-ports", "Zero pons considered valid")
172}
173
174func TestPonsValidationRangeLimits(t *testing.T) {
175 ponsConf := getTestPonsConfiguration()
176 assert.NilError(t, validatePonsConfig(ponsConf), "Test configuration is not valid")
177
178 ponsConf.Ranges[0].PonRange = IdRange{0, 0}
179 ponsConf.Ranges[1].PonRange = IdRange{1, 15}
180
181 assert.NilError(t, validatePonsConfig(ponsConf), "Single pon range considered invalid")
182
183 //Get back to a known good configuration
184 ponsConf = getTestPonsConfiguration()
185 ponsConf.Ranges[0].PonRange = IdRange{5, 4}
186
187 assert.ErrorContains(t, validatePonsConfig(ponsConf), "limits", "Invalid pons range limits considered valid")
188
189 ponsConf = getTestPonsConfiguration()
190 ponsConf.Ranges[0].OnuRange = IdRange{5, 4}
191
192 assert.ErrorContains(t, validatePonsConfig(ponsConf), "limits", "Invalid onus range limits considered valid")
193
194 ponsConf = getTestPonsConfiguration()
195 ponsConf.Ranges[0].AllocIdRange = IdRange{5, 4}
196
197 assert.ErrorContains(t, validatePonsConfig(ponsConf), "limits", "Invalid alloc-ids range limits considered valid")
198
199 ponsConf = getTestPonsConfiguration()
200 ponsConf.Ranges[0].GemportRange = IdRange{5, 4}
201
202 assert.ErrorContains(t, validatePonsConfig(ponsConf), "limits", "Invalid gemports range limits considered valid")
203}
204
205func TestGetPonConfigById(t *testing.T) {
206 PonsConfig = getTestPonsConfiguration()
207
208 for id := uint32(0); id < PonsConfig.Number-1; id++ {
209 conf, err := GetPonConfigById(id)
210 assert.NilError(t, err, fmt.Sprintf("Cannot get configuration for pon %d", id))
211
212 if id > conf.PonRange.EndId || id < conf.PonRange.StartId {
213 assert.NilError(t, err, fmt.Sprintf("Got wrong configuration for pon %d", id))
214 }
215 }
216
217 _, err := GetPonConfigById(16)
218
219 assert.Assert(t, err != nil, "Invalid pon id returned configuration")
220
221 _, err = GetPonConfigById(100)
222
223 assert.Assert(t, err != nil, "Invalid pon id returned configuration")
224}