blob: 44b571a94cf30e2254c380ec61c406c054e62808 [file] [log] [blame]
Matteo Scandolo40e067f2019-10-16 16:59:41 -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 common
18
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010019import (
20 "flag"
21 "fmt"
22 "io/ioutil"
23 "net"
Matteo Scandolof65e6872020-04-15 15:18:43 -070024 "strings"
Shrey Baid64cda472020-04-24 18:58:18 +053025
Matteo Scandolo4a036262020-08-17 15:56:13 -070026 "github.com/imdario/mergo"
Shrey Baid64cda472020-04-24 18:58:18 +053027 log "github.com/sirupsen/logrus"
Matteo Scandolo4a036262020-08-17 15:56:13 -070028 "gopkg.in/yaml.v2"
Matteo Scandolof65e6872020-04-15 15:18:43 -070029)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070030
Matteo Scandolof65e6872020-04-15 15:18:43 -070031var tagAllocationValues = []string{
32 "unknown",
33 "shared",
34 "unique",
35}
36
Matteo Scandolo94967142021-05-28 11:37:06 -070037const (
38 BP_FORMAT_MEF = "mef"
39 BP_FORMAT_IETF = "ietf"
40)
41
Matteo Scandolof65e6872020-04-15 15:18:43 -070042type TagAllocation int
43
44func (t TagAllocation) String() string {
45 return tagAllocationValues[t]
46}
47
48func tagAllocationFromString(s string) (TagAllocation, error) {
49 for i, v := range tagAllocationValues {
Matteo Scandolo4a036262020-08-17 15:56:13 -070050 if v == strings.TrimSpace(s) {
Matteo Scandolof65e6872020-04-15 15:18:43 -070051 return TagAllocation(i), nil
52 }
53 }
54 log.WithFields(log.Fields{
55 "ValidValues": strings.Join(tagAllocationValues[1:], ", "),
56 }).Errorf("%s-is-not-a-valid-tag-allocation", s)
Shrey Baid688b4242020-07-10 20:40:10 +053057 return TagAllocation(0), fmt.Errorf("%s-is-not-a-valid-tag-allocation", s)
Matteo Scandolof65e6872020-04-15 15:18:43 -070058}
59
60const (
61 _ TagAllocation = iota
62 TagAllocationShared
63 TagAllocationUnique
64)
65
Matteo Scandolo40e067f2019-10-16 16:59:41 -070066type BBRCliOptions struct {
Matteo Scandolo4a036262020-08-17 15:56:13 -070067 *GlobalConfig
Matteo Scandolo40e067f2019-10-16 16:59:41 -070068 BBSimIp string
69 BBSimPort string
70 BBSimApiPort string
Matteo Scandolof5c537e2019-10-28 16:45:57 -070071 LogFile string
Matteo Scandolo40e067f2019-10-16 16:59:41 -070072}
73
Matteo Scandolo4a036262020-08-17 15:56:13 -070074type GlobalConfig struct {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010075 BBSim BBSimConfig
76 Olt OltConfig
77 BBR BBRConfig
78}
Matteo Scandolo40e067f2019-10-16 16:59:41 -070079
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010080type OltConfig struct {
81 Model string `yaml:"model"`
82 Vendor string `yaml:"vendor"`
83 HardwareVersion string `yaml:"hardware_version"`
84 FirmwareVersion string `yaml:"firmware_version"`
85 DeviceId string `yaml:"device_id"`
86 DeviceSerialNumber string `yaml:"device_serial_number"`
87 PonPorts uint32 `yaml:"pon_ports"`
88 NniPorts uint32 `yaml:"nni_ports"`
Elia Battiston420c9092022-02-02 12:17:54 +010089 NniSpeed uint32 `yaml:"nni_speed"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010090 OnusPonPort uint32 `yaml:"onus_per_port"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010091 ID int `yaml:"id"`
92 OltRebootDelay int `yaml:"reboot_delay"`
Shrey Baid688b4242020-07-10 20:40:10 +053093 PortStatsInterval int `yaml:"port_stats_interval"`
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000094 OmciResponseRate uint8 `yaml:"omci_response_rate"`
Mahir Gunyela1753ae2021-06-23 00:24:56 -070095 UniPorts uint32 `yaml:"uni_ports"`
Elia Battistonac63b112022-01-12 18:40:49 +010096 PotsPorts uint32 `yaml:"pots_ports"`
Andrea Campanella6f5f3552022-03-10 17:14:25 +010097 NniDhcpTrapVid uint32 `yaml:"nni_dhcp_trap_vid"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010098}
Matteo Scandolo40e067f2019-10-16 16:59:41 -070099
Elia Battistonb7bea222022-02-18 16:25:00 +0100100type PonPortsConfig struct {
101 Number uint32 `yaml:"num_pon_ports"`
102 Ranges []PonRangeConfig `yaml:"ranges"`
103}
104
105type IdRange struct {
106 StartId uint32 `yaml:"start"`
107 EndId uint32 `yaml:"end"`
108}
109
110type PonTechnology int
111
112var ponTechnologyValues = []string{
113 "GPON", "XGS-PON",
114}
115
116func (t PonTechnology) String() string {
117 return ponTechnologyValues[t]
118}
119
120const (
121 GPON PonTechnology = iota
122 XGSPON
123)
124
125func PonTechnologyFromString(s string) (PonTechnology, error) {
126 for i, val := range ponTechnologyValues {
127 if val == s {
128 return PonTechnology(i), nil
129 }
130 }
131 log.WithFields(log.Fields{
132 "ValidValues": strings.Join(ponTechnologyValues[:], ", "),
133 }).Errorf("%s-is-not-a-valid-pon-technology", s)
134 return -1, fmt.Errorf("%s-is-not-a-valid-pon-technology", s)
135}
136
137//Constants for default allocation ranges
138const (
139 defaultOnuIdStart = 1
140 defaultAllocIdStart = 1024
141 defaultGemPortIdPerAllocId = 8
142 defaultGemportIdStart = 1024
143)
144
145type PonRangeConfig struct {
146 PonRange IdRange `yaml:"pon_id_range"`
147 Technology string `yaml:"tech"`
148 OnuRange IdRange `yaml:"onu_id_range"`
149 AllocIdRange IdRange `yaml:"alloc_id_range"`
150 GemportRange IdRange `yaml:"gemport_id_range"`
151}
152
153func GetPonConfigById(id uint32) (*PonRangeConfig, error) {
154 if PonsConfig == nil {
155 return nil, fmt.Errorf("pons-config-nil")
156 }
157
158 for _, r := range PonsConfig.Ranges {
159 if id >= r.PonRange.StartId && id <= r.PonRange.EndId {
160 return &r, nil
161 }
162 }
163
164 return nil, fmt.Errorf("pon-config-for-id-%d-not-found", id)
165}
166
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100167type BBSimConfig struct {
Matteo Scandolocfedba42022-02-14 16:08:54 -0800168 ConfigFile string
169 ServiceConfigFile string
170 PonsConfigFile string
171 DhcpRetry bool `yaml:"dhcp_retry"`
172 AuthRetry bool `yaml:"auth_retry"`
173 LogLevel string `yaml:"log_level"`
174 LogCaller bool `yaml:"log_caller"`
175 Delay int `yaml:"delay"`
176 CpuProfile *string `yaml:"cpu_profile"`
177 OpenOltAddress string `yaml:"openolt_address"`
178 ApiAddress string `yaml:"api_address"`
179 RestApiAddress string `yaml:"rest_api_address"`
180 LegacyApiAddress string `yaml:"legacy_api_address"`
181 LegacyRestApiAddress string `yaml:"legacy_rest_api_address"`
182 SadisRestAddress string `yaml:"sadis_rest_address"`
183 SadisServer bool `yaml:"sadis_server"`
184 KafkaAddress string `yaml:"kafka_address"`
185 Events bool `yaml:"enable_events"`
186 ControlledActivation string `yaml:"controlled_activation"`
187 EnablePerf bool `yaml:"enable_perf"`
188 KafkaEventTopic string `yaml:"kafka_event_topic"`
189 DmiServerAddress string `yaml:"dmi_server_address"`
190 BandwidthProfileFormat string `yaml:"bp_format"`
191 InjectOmciUnknownAttributes bool `yaml:"inject_omci_unknown_attributes"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100192}
Matteo Scandoloc1147092019-10-29 09:38:33 -0700193
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100194type BBRConfig struct {
195 Log string `yaml:"log"`
196 LogLevel string `yaml:"log_level"`
197 LogCaller bool `yaml:"log_caller"`
198}
199
Matteo Scandolo4a036262020-08-17 15:56:13 -0700200type ServiceYaml struct {
201 Name string
202 CTag int `yaml:"c_tag"`
203 STag int `yaml:"s_tag"`
204 NeedsEapol bool `yaml:"needs_eapol"`
Matteo Scandolo8a574812021-05-20 15:18:53 -0700205 NeedsDhcp bool `yaml:"needs_dhcp"`
Matteo Scandolo4a036262020-08-17 15:56:13 -0700206 NeedsIgmp bool `yaml:"needs_igmp"`
Andrea Campanella29890452022-02-03 16:00:19 +0100207 NeedsPPPoE bool `yaml:"needs_pppoe"`
Matteo Scandolo4a036262020-08-17 15:56:13 -0700208 CTagAllocation string `yaml:"c_tag_allocation"`
209 STagAllocation string `yaml:"s_tag_allocation"`
210 TechnologyProfileID int `yaml:"tp_id"`
211 UniTagMatch int `yaml:"uni_tag_match"`
212 ConfigureMacAddress bool `yaml:"configure_mac_address"`
Andrea Campanella29890452022-02-03 16:00:19 +0100213 EnableMacLearning bool `yaml:"enable_mac_learning"`
Matteo Scandolo8d281372020-09-03 16:23:37 -0700214 UsPonCTagPriority uint8 `yaml:"us_pon_c_tag_priority"`
215 UsPonSTagPriority uint8 `yaml:"us_pon_s_tag_priority"`
216 DsPonCTagPriority uint8 `yaml:"ds_pon_c_tag_priority"`
217 DsPonSTagPriority uint8 `yaml:"ds_pon_s_tag_priority"`
Matteo Scandolo4a036262020-08-17 15:56:13 -0700218}
219type YamlServiceConfig struct {
220 Workflow string
221 Services []ServiceYaml `yaml:"services,flow"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100222}
223
Matteo Scandolo4a036262020-08-17 15:56:13 -0700224func (cfg *YamlServiceConfig) String() string {
225 str := fmt.Sprintf("[workflow: %s, Services: ", cfg.Workflow)
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100226
Matteo Scandolo4a036262020-08-17 15:56:13 -0700227 for _, s := range cfg.Services {
228 str = fmt.Sprintf("%s[", str)
229 str = fmt.Sprintf("%sname=%s, c_tag=%d, s_tag=%d, ",
230 str, s.Name, s.CTag, s.STag)
231 str = fmt.Sprintf("%sc_tag_allocation=%s, s_tag_allocation=%s, ",
232 str, s.CTagAllocation, s.STagAllocation)
233 str = fmt.Sprintf("%sneeds_eapol=%t, needs_dhcp=%t, needs_igmp=%t",
Matteo Scandolo8a574812021-05-20 15:18:53 -0700234 str, s.NeedsEapol, s.NeedsDhcp, s.NeedsIgmp)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700235 str = fmt.Sprintf("%stp_id=%d, uni_tag_match=%d",
236 str, s.TechnologyProfileID, s.UniTagMatch)
237 str = fmt.Sprintf("%s]", str)
238 }
239 str = fmt.Sprintf("%s]", str)
240 return str
241}
242
243var (
Elia Battistonb7bea222022-02-18 16:25:00 +0100244 Config *GlobalConfig
245 Services []ServiceYaml
246 PonsConfig *PonPortsConfig
Matteo Scandolo4a036262020-08-17 15:56:13 -0700247)
248
249// Load the BBSim configuration. This is a combination of CLI parameters and YAML files
250// We proceed in this order:
251// - Read CLI parameters
252// - Using those we read the yaml files (config and services)
253// - we merge the configuration (CLI has priority over yaml files)
254func LoadConfig() {
255
256 Config = getDefaultOps()
257
258 cliConf := readCliParams()
259
260 yamlConf, err := loadBBSimConf(cliConf.BBSim.ConfigFile)
261
262 if err != nil {
263 log.WithFields(log.Fields{
264 "file": cliConf.BBSim.ConfigFile,
265 "err": err,
266 }).Fatal("Can't read config file")
267 }
268
269 // merging Yaml and Default Values
270 if err := mergo.Merge(Config, yamlConf, mergo.WithOverride); err != nil {
271 log.WithFields(log.Fields{
272 "err": err,
273 }).Fatal("Can't merge YAML and Config")
274 }
275
276 // merging CLI values on top of the yaml ones
277 if err := mergo.Merge(Config, cliConf, mergo.WithOverride); err != nil {
278 log.WithFields(log.Fields{
279 "err": err,
280 }).Fatal("Can't merge CLI and Config")
281 }
282
283 services, err := loadBBSimServices(Config.BBSim.ServiceConfigFile)
284
285 if err != nil {
286 log.WithFields(log.Fields{
287 "file": Config.BBSim.ServiceConfigFile,
288 "err": err,
289 }).Fatal("Can't read services file")
290 }
291
292 Services = services
293
Elia Battistonb7bea222022-02-18 16:25:00 +0100294 //A blank filename means we should fall back to bbsim defaults
295 if Config.BBSim.PonsConfigFile == "" {
296 PonsConfig, err = getDefaultPonsConfig()
297 if err != nil {
298 log.WithFields(log.Fields{
299 "err": err,
300 }).Fatal("Can't load Pon interfaces defaults.")
301 }
302 } else {
303 PonsConfig, err = loadBBSimPons(Config.BBSim.PonsConfigFile)
304
305 if err != nil {
306 log.WithFields(log.Fields{
307 "file": Config.BBSim.PonsConfigFile,
308 "err": err,
309 }).Fatal("Can't read services file")
310 }
311 }
312
313 if err := validatePonsConfig(PonsConfig); err != nil {
314 log.WithFields(log.Fields{
315 "file": Config.BBSim.PonsConfigFile,
316 "err": err,
317 }).Fatal("Invalid Pon interfaces configuration")
318 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700319}
320
321func readCliParams() *GlobalConfig {
322
Matteo Scandoloc11074d2020-09-14 14:59:24 -0700323 conf := getDefaultOps()
Matteo Scandolo4a036262020-08-17 15:56:13 -0700324
325 configFile := flag.String("config", conf.BBSim.ConfigFile, "Configuration file path")
326 servicesFile := flag.String("services", conf.BBSim.ServiceConfigFile, "Service Configuration file path")
Elia Battistonb7bea222022-02-18 16:25:00 +0100327 ponsFile := flag.String("pon_port_config_file", conf.BBSim.PonsConfigFile, "Pon Interfaces Configuration file path")
Matteo Scandolo94967142021-05-28 11:37:06 -0700328 sadisBpFormat := flag.String("bp_format", conf.BBSim.BandwidthProfileFormat, "Bandwidth profile format, 'mef' or 'ietf'")
Matteo Scandolo4a036262020-08-17 15:56:13 -0700329
330 olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID")
331 nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated")
Elia Battiston420c9092022-02-02 12:17:54 +0100332 nni_speed := flag.Uint("nni_speed", uint(conf.Olt.NniSpeed), "Reported speed of the NNI ports in Mbps")
Matteo Scandolo4a036262020-08-17 15:56:13 -0700333 pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated")
334 onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated")
Elia Battistonac63b112022-01-12 18:40:49 +0100335 uni := flag.Int("uni", int(conf.Olt.UniPorts), "Number of Ethernet UNI Ports per ONU device to be emulated")
336 pots := flag.Int("pots", int(conf.Olt.PotsPorts), "Number of POTS UNI Ports per ONU device to be emulated")
Andrea Campanella6f5f3552022-03-10 17:14:25 +0100337 NniDchpTrapVid := flag.Int("nni_dhcp_trap_vid", int(conf.Olt.NniDhcpTrapVid), "Vlan to trap the DHCP packets on")
Mahir Gunyela1753ae2021-06-23 00:24:56 -0700338
Matteo Scandolo93566702020-09-30 15:19:27 -0700339 oltRebootDelay := flag.Int("oltRebootDelay", conf.Olt.OltRebootDelay, "Time that BBSim should before restarting after a reboot")
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000340 omci_response_rate := flag.Int("omci_response_rate", int(conf.Olt.OmciResponseRate), "Amount of OMCI messages to respond to")
Matteo Scandolo4a036262020-08-17 15:56:13 -0700341
342 openolt_address := flag.String("openolt_address", conf.BBSim.OpenOltAddress, "IP address:port")
343 api_address := flag.String("api_address", conf.BBSim.ApiAddress, "IP address:port")
344 rest_api_address := flag.String("rest_api_address", conf.BBSim.RestApiAddress, "IP address:port")
amit.ghosh258d14c2020-10-02 15:13:38 +0200345 dmi_server_address := flag.String("dmi_server_address", conf.BBSim.DmiServerAddress, "IP address:port")
Matteo Scandolo4a036262020-08-17 15:56:13 -0700346
347 profileCpu := flag.String("cpuprofile", "", "write cpu profile to file")
348
349 logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)")
350 logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not")
351
352 delay := flag.Int("delay", conf.BBSim.Delay, "The delay between ONU DISCOVERY batches in milliseconds (1 ONU per each PON PORT at a time")
353
354 controlledActivation := flag.String("ca", conf.BBSim.ControlledActivation, "Set the mode for controlled activation of PON ports and ONUs")
355 enablePerf := flag.Bool("enableperf", conf.BBSim.EnablePerf, "Setting this flag will cause BBSim to not store data like traffic schedulers, flows of ONUs etc..")
356 enableEvents := flag.Bool("enableEvents", conf.BBSim.Events, "Enable sending BBSim events on configured kafka server")
357 kafkaAddress := flag.String("kafkaAddress", conf.BBSim.KafkaAddress, "IP:Port for kafka")
358 kafkaEventTopic := flag.String("kafkaEventTopic", conf.BBSim.KafkaEventTopic, "Ability to configure the topic on which BBSim publishes events on Kafka")
359 dhcpRetry := flag.Bool("dhcpRetry", conf.BBSim.DhcpRetry, "Set this flag if BBSim should retry DHCP upon failure until success")
360 authRetry := flag.Bool("authRetry", conf.BBSim.AuthRetry, "Set this flag if BBSim should retry EAPOL (Authentication) upon failure until success")
Matteo Scandolocfedba42022-02-14 16:08:54 -0800361 injectOmciUnknownAttributes := flag.Bool("injectOmciUnknownAttributes", conf.BBSim.InjectOmciUnknownAttributes, "Generate a MibDB packet with Unknown Attributes")
Matteo Scandolo93566702020-09-30 15:19:27 -0700362
Matteo Scandolo4a036262020-08-17 15:56:13 -0700363 flag.Parse()
364
365 conf.Olt.ID = int(*olt_id)
366 conf.Olt.NniPorts = uint32(*nni)
Elia Battiston420c9092022-02-02 12:17:54 +0100367 conf.Olt.NniSpeed = uint32(*nni_speed)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700368 conf.Olt.PonPorts = uint32(*pon)
Mahir Gunyela1753ae2021-06-23 00:24:56 -0700369 conf.Olt.UniPorts = uint32(*uni)
Elia Battistonac63b112022-01-12 18:40:49 +0100370 conf.Olt.PotsPorts = uint32(*pots)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700371 conf.Olt.OnusPonPort = uint32(*onu)
Andrea Campanella6f5f3552022-03-10 17:14:25 +0100372 conf.Olt.NniDhcpTrapVid = uint32(*NniDchpTrapVid)
Matteo Scandolo93566702020-09-30 15:19:27 -0700373 conf.Olt.OltRebootDelay = *oltRebootDelay
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000374 conf.Olt.OmciResponseRate = uint8(*omci_response_rate)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700375 conf.BBSim.ConfigFile = *configFile
376 conf.BBSim.ServiceConfigFile = *servicesFile
Elia Battistonb7bea222022-02-18 16:25:00 +0100377 conf.BBSim.PonsConfigFile = *ponsFile
Matteo Scandolo4a036262020-08-17 15:56:13 -0700378 conf.BBSim.CpuProfile = profileCpu
379 conf.BBSim.LogLevel = *logLevel
380 conf.BBSim.LogCaller = *logCaller
381 conf.BBSim.Delay = *delay
382 conf.BBSim.ControlledActivation = *controlledActivation
383 conf.BBSim.EnablePerf = *enablePerf
384 conf.BBSim.Events = *enableEvents
385 conf.BBSim.KafkaAddress = *kafkaAddress
386 conf.BBSim.OpenOltAddress = *openolt_address
387 conf.BBSim.ApiAddress = *api_address
388 conf.BBSim.RestApiAddress = *rest_api_address
389 conf.BBSim.KafkaEventTopic = *kafkaEventTopic
390 conf.BBSim.AuthRetry = *authRetry
391 conf.BBSim.DhcpRetry = *dhcpRetry
amit.ghosh258d14c2020-10-02 15:13:38 +0200392 conf.BBSim.DmiServerAddress = *dmi_server_address
Matteo Scandolocfedba42022-02-14 16:08:54 -0800393 conf.BBSim.InjectOmciUnknownAttributes = *injectOmciUnknownAttributes
Matteo Scandolo4a036262020-08-17 15:56:13 -0700394
395 // update device id if not set
396 if conf.Olt.DeviceId == "" {
397 conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String()
398 }
399
Matteo Scandolo94967142021-05-28 11:37:06 -0700400 // check that the BP format is valid
401 if (*sadisBpFormat != BP_FORMAT_MEF) && (*sadisBpFormat != BP_FORMAT_IETF) {
402 log.Fatalf("Invalid parameter 'bp_format', supported values are %s and %s, you provided %s", BP_FORMAT_MEF, BP_FORMAT_IETF, *sadisBpFormat)
403 }
404 conf.BBSim.BandwidthProfileFormat = *sadisBpFormat
405
Matteo Scandoloc11074d2020-09-14 14:59:24 -0700406 return conf
Matteo Scandolo4a036262020-08-17 15:56:13 -0700407}
408
409func getDefaultOps() *GlobalConfig {
410
411 c := &GlobalConfig{
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100412 BBSimConfig{
Elia Battistonb7bea222022-02-18 16:25:00 +0100413 ConfigFile: "configs/bbsim.yaml",
414 ServiceConfigFile: "configs/att-services.yaml",
415 // PonsConfigFile is left intentionally blank here
416 // to use the default values computed at runtime depending
417 // on the loaded Services
Matteo Scandolocfedba42022-02-14 16:08:54 -0800418 PonsConfigFile: "",
419 LogLevel: "debug",
420 LogCaller: false,
421 Delay: 200,
422 OpenOltAddress: ":50060",
423 ApiAddress: ":50070",
424 RestApiAddress: ":50071",
425 LegacyApiAddress: ":50072",
426 LegacyRestApiAddress: ":50073",
427 SadisRestAddress: ":50074",
428 SadisServer: true,
429 KafkaAddress: ":9092",
430 Events: false,
431 ControlledActivation: "default",
432 EnablePerf: false,
433 KafkaEventTopic: "",
434 DhcpRetry: false,
435 AuthRetry: false,
436 DmiServerAddress: ":50075",
437 BandwidthProfileFormat: BP_FORMAT_MEF,
438 InjectOmciUnknownAttributes: false,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100439 },
440 OltConfig{
441 Vendor: "BBSim",
442 Model: "asfvolt16",
443 HardwareVersion: "emulated",
444 FirmwareVersion: "",
445 DeviceSerialNumber: "BBSM00000001",
446 PonPorts: 1,
447 NniPorts: 1,
Elia Battiston420c9092022-02-02 12:17:54 +0100448 NniSpeed: 10000, //Mbps
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100449 OnusPonPort: 1,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100450 ID: 0,
Matteo Scandolo93566702020-09-30 15:19:27 -0700451 OltRebootDelay: 60,
Pragya Arya996a0892020-03-09 21:47:52 +0530452 PortStatsInterval: 20,
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000453 OmciResponseRate: 10,
Mahir Gunyela1753ae2021-06-23 00:24:56 -0700454 UniPorts: 4,
Elia Battistonac63b112022-01-12 18:40:49 +0100455 PotsPorts: 0,
Andrea Campanella6f5f3552022-03-10 17:14:25 +0100456 NniDhcpTrapVid: 0,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100457 },
458 BBRConfig{
459 LogLevel: "debug",
460 LogCaller: false,
461 },
462 }
463 return c
464}
465
Elia Battistonb7bea222022-02-18 16:25:00 +0100466func getDefaultPonsConfig() (*PonPortsConfig, error) {
467
468 if Config == nil {
469 return nil, fmt.Errorf("Config is nil")
470 }
471 if Services == nil {
472 return nil, fmt.Errorf("Services is nil")
473 }
474
475 //The default should replicate the old way bbsim used to compute resource ranges based on the configuration
476 // 1 allocId per Service * UNI
477 allocIdPerOnu := uint32(Config.Olt.UniPorts * uint32(len(Services)))
478 return &PonPortsConfig{
479 Number: Config.Olt.PonPorts,
480 Ranges: []PonRangeConfig{
481 {
482 PonRange: IdRange{0, Config.Olt.PonPorts - 1},
483 Technology: XGSPON.String(),
484 // we need one ONU ID available per ONU, but the smaller the range the smaller the pool created in the openolt adapter
485 OnuRange: IdRange{defaultOnuIdStart, defaultOnuIdStart + (Config.Olt.OnusPonPort - 1)},
486 // 1 allocId per Service * UNI * ONU
487 AllocIdRange: IdRange{defaultAllocIdStart, defaultAllocIdStart + (Config.Olt.OnusPonPort * allocIdPerOnu)},
488 // up to 8 gemport-id per tcont/alloc-id
489 GemportRange: IdRange{defaultGemportIdStart, defaultGemportIdStart + Config.Olt.OnusPonPort*allocIdPerOnu*defaultGemPortIdPerAllocId},
490 },
491 },
492 }, nil
493}
494
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100495// LoadBBSimConf loads the BBSim configuration from a YAML file
Matteo Scandolo4a036262020-08-17 15:56:13 -0700496func loadBBSimConf(filename string) (*GlobalConfig, error) {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100497 yamlConfig := getDefaultOps()
498
499 yamlFile, err := ioutil.ReadFile(filename)
500 if err != nil {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700501 log.WithFields(log.Fields{
502 "err": err,
503 "filename": filename,
504 }).Error("Cannot load BBSim configuration file. Using defaults.")
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100505 return yamlConfig, nil
506 }
507
508 err = yaml.Unmarshal(yamlFile, yamlConfig)
509 if err != nil {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700510 return nil, err
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100511 }
512
513 return yamlConfig, nil
514}
515
Elia Battistonb7bea222022-02-18 16:25:00 +0100516// loadBBSimPons loads the configuration of PON interfaces from a YAML file
517func loadBBSimPons(filename string) (*PonPortsConfig, error) {
518 yamlPonsConfig, err := getDefaultPonsConfig()
519 if err != nil {
520 log.WithFields(log.Fields{
521 "err": err,
522 }).Error("Can't load Pon interfaces defaults.")
523 return nil, err
524 }
525
526 yamlFile, err := ioutil.ReadFile(filename)
527 if err != nil {
528 log.WithFields(log.Fields{
529 "err": err,
530 "filename": filename,
531 }).Error("Cannot load Pon interfaces configuration file. Using defaults.")
532 return yamlPonsConfig, nil
533 }
534
535 err = yaml.Unmarshal(yamlFile, yamlPonsConfig)
536 if err != nil {
537 return nil, err
538 }
539
540 return yamlPonsConfig, nil
541}
542
543// validatePonsConfig checks if the configuration to use for the definition of Pon interfaces is valid
544func validatePonsConfig(pons *PonPortsConfig) error {
545
546 if pons.Number == 0 {
547 return fmt.Errorf("no-pon-ports")
548 }
549
550 definedPorts := make([]int, pons.Number)
551
552 for rIndex, resRange := range pons.Ranges {
553 if _, err := PonTechnologyFromString(resRange.Technology); err != nil {
554 return err
555 }
556
557 if resRange.PonRange.EndId < resRange.PonRange.StartId {
558 return fmt.Errorf("invalid-pon-ports-limits-in-range-%d", rIndex)
559 }
560
561 //Keep track of the defined pons
562 for p := resRange.PonRange.StartId; p <= resRange.PonRange.EndId; p++ {
563 if p > uint32(len(definedPorts)-1) {
564 return fmt.Errorf("pon-port-%d-in-range-%d-but-max-is-%d", p, rIndex, pons.Number-1)
565 }
566 definedPorts[p]++
567
568 if definedPorts[p] > 1 {
569 return fmt.Errorf("pon-port-%d-has-duplicate-definition-in-range-%d", p, rIndex)
570 }
571 }
572
573 if resRange.OnuRange.EndId < resRange.OnuRange.StartId {
574 return fmt.Errorf("invalid-onus-limits-in-range-%d", rIndex)
575 }
576 if resRange.AllocIdRange.EndId < resRange.AllocIdRange.StartId {
577 return fmt.Errorf("invalid-allocid-limits-in-range-%d", rIndex)
578 }
579 if resRange.GemportRange.EndId < resRange.GemportRange.StartId {
580 return fmt.Errorf("invalid-gemport-limits-in-range-%d", rIndex)
581 }
582 }
583
584 //Check if the ranges define all the pons
585 for i, num := range definedPorts {
586 if num < 1 {
587 return fmt.Errorf("pon-port-%d-is-not-defined-in-ranges", i)
588 }
589 }
590
591 return nil
592}
593
Matteo Scandolo4a036262020-08-17 15:56:13 -0700594// LoadBBSimServices parses a file describing the services that need to be created for each UNI
595func loadBBSimServices(filename string) ([]ServiceYaml, error) {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100596
Matteo Scandolo4a036262020-08-17 15:56:13 -0700597 yamlServiceCfg := YamlServiceConfig{}
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100598
Matteo Scandolo4a036262020-08-17 15:56:13 -0700599 yamlFile, err := ioutil.ReadFile(filename)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700600 if err != nil {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700601 return nil, err
Matteo Scandolof65e6872020-04-15 15:18:43 -0700602 }
603
Matteo Scandolo4a036262020-08-17 15:56:13 -0700604 err = yaml.Unmarshal([]byte(yamlFile), &yamlServiceCfg)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700605 if err != nil {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700606 return nil, err
Matteo Scandolof65e6872020-04-15 15:18:43 -0700607 }
608
Matteo Scandolo4a036262020-08-17 15:56:13 -0700609 for _, service := range yamlServiceCfg.Services {
610
611 if service.CTagAllocation == "" || service.STagAllocation == "" {
612 log.Fatal("c_tag_allocation and s_tag_allocation are mandatory fields")
613 }
614
615 if _, err := tagAllocationFromString(string(service.CTagAllocation)); err != nil {
616 log.WithFields(log.Fields{
617 "err": err,
618 }).Fatal("c_tag_allocation is not valid")
619 }
Matteo Scandolof65e6872020-04-15 15:18:43 -0700620 }
621
Matteo Scandolo4a036262020-08-17 15:56:13 -0700622 log.WithFields(log.Fields{
623 "services": yamlServiceCfg.String(),
624 }).Debug("BBSim services description correctly loaded")
625 return yamlServiceCfg.Services, nil
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700626}
627
Matteo Scandolo4a036262020-08-17 15:56:13 -0700628// This is only used by BBR
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700629func GetBBROpts() BBRCliOptions {
630
631 bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP")
632 bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port")
633 bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port")
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700634 logFile := flag.String("logfile", "", "Log to a file")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700635
Matteo Scandoloc11074d2020-09-14 14:59:24 -0700636 LoadConfig()
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700637
638 bbrOptions := BBRCliOptions{
Matteo Scandolo4a036262020-08-17 15:56:13 -0700639 Config,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700640 *bbsimIp,
641 *bbsimPort,
642 *bbsimApiPort,
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700643 *logFile,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700644 }
645
646 return bbrOptions
647}