blob: 5dd155330ea5457038692d5c6510106a08c04599 [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
26 "github.com/ghodss/yaml"
27 log "github.com/sirupsen/logrus"
Matteo Scandolof65e6872020-04-15 15:18:43 -070028)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070029
Matteo Scandolof65e6872020-04-15 15:18:43 -070030var tagAllocationValues = []string{
31 "unknown",
32 "shared",
33 "unique",
34}
35
36type TagAllocation int
37
38func (t TagAllocation) String() string {
39 return tagAllocationValues[t]
40}
41
42func tagAllocationFromString(s string) (TagAllocation, error) {
43 for i, v := range tagAllocationValues {
44 if v == s {
45 return TagAllocation(i), nil
46 }
47 }
48 log.WithFields(log.Fields{
49 "ValidValues": strings.Join(tagAllocationValues[1:], ", "),
50 }).Errorf("%s-is-not-a-valid-tag-allocation", s)
Shrey Baid688b4242020-07-10 20:40:10 +053051 return TagAllocation(0), fmt.Errorf("%s-is-not-a-valid-tag-allocation", s)
Matteo Scandolof65e6872020-04-15 15:18:43 -070052}
53
54const (
55 _ TagAllocation = iota
56 TagAllocationShared
57 TagAllocationUnique
58)
59
Matteo Scandolof65e6872020-04-15 15:18:43 -070060var sadisFormatValues = []string{
61 "unknown",
62 "att",
63 "dt",
64 "tt",
65}
66
67type SadisFormat int
68
69func (s SadisFormat) String() string {
70 return sadisFormatValues[s]
71}
72
73func sadisFormatFromString(s string) (SadisFormat, error) {
74 for i, v := range sadisFormatValues {
75 if v == s {
76 return SadisFormat(i), nil
77 }
78 }
79 log.WithFields(log.Fields{
80 "ValidValues": strings.Join(sadisFormatValues[1:], ", "),
81 }).Errorf("%s-is-not-a-valid-sadis-format", s)
Shrey Baid688b4242020-07-10 20:40:10 +053082 return SadisFormat(0), fmt.Errorf("%s-is-not-a-valid-sadis-format", s)
Matteo Scandolof65e6872020-04-15 15:18:43 -070083}
84
85const (
86 _ SadisFormat = iota
87 SadisFormatAtt
88 SadisFormatDt
89 SadisFormatTt
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010090)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070091
92type BBRCliOptions struct {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010093 *BBSimYamlConfig
Matteo Scandolo40e067f2019-10-16 16:59:41 -070094 BBSimIp string
95 BBSimPort string
96 BBSimApiPort string
Matteo Scandolof5c537e2019-10-28 16:45:57 -070097 LogFile string
Matteo Scandolo40e067f2019-10-16 16:59:41 -070098}
99
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100100type BBSimYamlConfig struct {
101 BBSim BBSimConfig
102 Olt OltConfig
103 BBR BBRConfig
104}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700105
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100106type OltConfig struct {
107 Model string `yaml:"model"`
108 Vendor string `yaml:"vendor"`
109 HardwareVersion string `yaml:"hardware_version"`
110 FirmwareVersion string `yaml:"firmware_version"`
111 DeviceId string `yaml:"device_id"`
112 DeviceSerialNumber string `yaml:"device_serial_number"`
113 PonPorts uint32 `yaml:"pon_ports"`
114 NniPorts uint32 `yaml:"nni_ports"`
115 OnusPonPort uint32 `yaml:"onus_per_port"`
116 Technology string `yaml:"technology"`
117 ID int `yaml:"id"`
118 OltRebootDelay int `yaml:"reboot_delay"`
Shrey Baid688b4242020-07-10 20:40:10 +0530119 PortStatsInterval int `yaml:"port_stats_interval"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100120}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700121
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100122type BBSimConfig struct {
Shrey Baidf8abccc2020-06-15 19:41:22 +0530123 DhcpRetry bool `yaml:"dhcp_retry"`
124 AuthRetry bool `yaml:"auth_retry"`
Shrey Baide72b3cc2020-05-12 00:03:06 +0530125 EnableIgmp bool `yaml:"enable_igmp"`
Matteo Scandolof65e6872020-04-15 15:18:43 -0700126 EnableDhcp bool `yaml:"enable_dhcp"`
127 EnableAuth bool `yaml:"enable_auth"`
128 LogLevel string `yaml:"log_level"`
129 LogCaller bool `yaml:"log_caller"`
130 Delay int `yaml:"delay"`
131 CpuProfile *string `yaml:"cpu_profile"`
132 CTagAllocation TagAllocation `yaml:"c_tag_allocation"`
133 CTag int `yaml:"c_tag"`
134 STagAllocation TagAllocation `yaml:"s_tag_allocation"`
135 STag int `yaml:"s_tag"`
136 OpenOltAddress string `yaml:"openolt_address"`
137 ApiAddress string `yaml:"api_address"`
138 RestApiAddress string `yaml:"rest_api_address"`
139 LegacyApiAddress string `yaml:"legacy_api_address"`
140 LegacyRestApiAddress string `yaml:"legacy_rest_api_address"`
141 SadisRestAddress string `yaml:"sadis_rest_address"`
142 SadisServer bool `yaml:"sadis_server"`
143 SadisFormat SadisFormat `yaml:"sadis_format"`
144 KafkaAddress string `yaml:"kafka_address"`
145 Events bool `yaml:"enable_events"`
146 ControlledActivation string `yaml:"controlled_activation"`
147 EnablePerf bool `yaml:"enable_perf"`
Shrey Baid688b4242020-07-10 20:40:10 +0530148 KafkaEventTopic string `yaml:"kafka_event_topic"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100149}
Matteo Scandoloc1147092019-10-29 09:38:33 -0700150
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100151type BBRConfig struct {
152 Log string `yaml:"log"`
153 LogLevel string `yaml:"log_level"`
154 LogCaller bool `yaml:"log_caller"`
155}
156
157var Options *BBSimYamlConfig
158
159func init() {
160 // load settings from config file first
161 Options, _ = LoadBBSimConf("configs/bbsim.yaml")
162}
163
164func getDefaultOps() *BBSimYamlConfig {
165
166 c := &BBSimYamlConfig{
167 BBSimConfig{
Matteo Scandolof65e6872020-04-15 15:18:43 -0700168 STagAllocation: TagAllocationShared,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100169 STag: 900,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700170 CTagAllocation: TagAllocationUnique,
171 CTag: 900,
Shrey Baide72b3cc2020-05-12 00:03:06 +0530172 EnableIgmp: false,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100173 EnableDhcp: false,
174 EnableAuth: false,
175 LogLevel: "debug",
176 LogCaller: false,
177 Delay: 200,
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100178 OpenOltAddress: ":50060",
179 ApiAddress: ":50070",
180 RestApiAddress: ":50071",
181 LegacyApiAddress: ":50072",
182 LegacyRestApiAddress: ":50073",
183 SadisRestAddress: ":50074",
184 SadisServer: true,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700185 SadisFormat: SadisFormatAtt,
Pragya Arya324337e2020-02-20 14:35:08 +0530186 KafkaAddress: ":9092",
187 Events: false,
Pragya Arya2225f202020-01-29 18:05:01 +0530188 ControlledActivation: "default",
Anand S Katti09541352020-01-29 15:54:01 +0530189 EnablePerf: false,
Shrey Baid64cda472020-04-24 18:58:18 +0530190 KafkaEventTopic: "",
Shrey Baidf8abccc2020-06-15 19:41:22 +0530191 DhcpRetry: false,
192 AuthRetry: false,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100193 },
194 OltConfig{
195 Vendor: "BBSim",
196 Model: "asfvolt16",
197 HardwareVersion: "emulated",
198 FirmwareVersion: "",
199 DeviceSerialNumber: "BBSM00000001",
200 PonPorts: 1,
201 NniPorts: 1,
202 OnusPonPort: 1,
203 Technology: "XGS-PON",
204 ID: 0,
205 OltRebootDelay: 10,
Pragya Arya996a0892020-03-09 21:47:52 +0530206 PortStatsInterval: 20,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100207 },
208 BBRConfig{
209 LogLevel: "debug",
210 LogCaller: false,
211 },
212 }
213 return c
214}
215
216// LoadBBSimConf loads the BBSim configuration from a YAML file
217func LoadBBSimConf(filename string) (*BBSimYamlConfig, error) {
218 yamlConfig := getDefaultOps()
219
220 yamlFile, err := ioutil.ReadFile(filename)
221 if err != nil {
222 fmt.Printf("Cannot load BBSim configuration file: %s. Using defaults.\n", err)
223 return yamlConfig, nil
224 }
225
226 err = yaml.Unmarshal(yamlFile, yamlConfig)
227 if err != nil {
228 fmt.Printf("Error parsing YAML file: %s\n", err)
229 }
230
Matteo Scandolof65e6872020-04-15 15:18:43 -0700231 // TODO convert from string to TagAllocation
232
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100233 return yamlConfig, nil
234}
235
Anand S Katti09541352020-01-29 15:54:01 +0530236// GetBBSimOpts loads the BBSim configuration file and over-rides options with corresponding CLI flags if set
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100237func GetBBSimOpts() *BBSimYamlConfig {
238 conf := Options
239
240 olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID")
241 nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated")
242 pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated")
243 onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated")
244
rajeshf921f882020-03-06 18:24:28 +0530245 openolt_address := flag.String("openolt_address", conf.BBSim.OpenOltAddress, "IP address:port")
246 api_address := flag.String("api_address", conf.BBSim.ApiAddress, "IP address:port")
247 rest_api_address := flag.String("rest_api_address", conf.BBSim.RestApiAddress, "IP address:port")
248
Matteo Scandolof65e6872020-04-15 15:18:43 -0700249 s_tag_allocation := flag.String("s_tag_allocation", conf.BBSim.STagAllocation.String(), "Use 'unique' for incremental values, 'shared' to use the same value in all the ONUs")
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100250 s_tag := flag.Int("s_tag", conf.BBSim.STag, "S-Tag initial value")
Matteo Scandolof65e6872020-04-15 15:18:43 -0700251
252 c_tag_allocation := flag.String("c_tag_allocation", conf.BBSim.CTagAllocation.String(), "Use 'unique' for incremental values, 'shared' to use the same value in all the ONUs")
253 c_tag := flag.Int("c_tag", conf.BBSim.CTag, "C-Tag starting value, each ONU will get a sequential one (targeting 1024 ONUs per BBSim instance the range is big enough)")
254
255 sadisFormat := flag.String("sadisFormat", conf.BBSim.SadisFormat.String(), fmt.Sprintf("Which format should sadis expose? [%s]", strings.Join(sadisFormatValues[1:], "|")))
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100256
257 auth := flag.Bool("auth", conf.BBSim.EnableAuth, "Set this flag if you want authentication to start automatically")
258 dhcp := flag.Bool("dhcp", conf.BBSim.EnableDhcp, "Set this flag if you want DHCP to start automatically")
Shrey Baide72b3cc2020-05-12 00:03:06 +0530259 igmp := flag.Bool("igmp", conf.BBSim.EnableIgmp, "Set this flag if you want IGMP to start automatically")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700260 profileCpu := flag.String("cpuprofile", "", "write cpu profile to file")
261
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100262 logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)")
263 logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700264
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100265 delay := flag.Int("delay", conf.BBSim.Delay, "The delay between ONU DISCOVERY batches in milliseconds (1 ONU per each PON PORT at a time")
Matteo Scandoloe33447a2019-10-31 12:38:23 -0700266
Pragya Arya2225f202020-01-29 18:05:01 +0530267 controlledActivation := flag.String("ca", conf.BBSim.ControlledActivation, "Set the mode for controlled activation of PON ports and ONUs")
Anand S Katti09541352020-01-29 15:54:01 +0530268 enablePerf := flag.Bool("enableperf", conf.BBSim.EnablePerf, "Setting this flag will cause BBSim to not store data like traffic schedulers, flows of ONUs etc..")
Pragya Arya324337e2020-02-20 14:35:08 +0530269 enableEvents := flag.Bool("enableEvents", conf.BBSim.Events, "Enable sending BBSim events on configured kafka server")
270 kafkaAddress := flag.String("kafkaAddress", conf.BBSim.KafkaAddress, "IP:Port for kafka")
Shrey Baid64cda472020-04-24 18:58:18 +0530271 kafkaEventTopic := flag.String("kafkaEventTopic", conf.BBSim.KafkaEventTopic, "Ability to configure the topic on which BBSim publishes events on Kafka")
Shrey Baidf8abccc2020-06-15 19:41:22 +0530272 dhcpRetry := flag.Bool("dhcpRetry", conf.BBSim.DhcpRetry, "Set this flag if BBSim should retry DHCP upon failure until success")
273 authRetry := flag.Bool("authRetry", conf.BBSim.AuthRetry, "Set this flag if BBSim should retry EAPOL (Authentication) upon failure until success")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700274 flag.Parse()
275
Matteo Scandolof65e6872020-04-15 15:18:43 -0700276 sTagAlloc, err := tagAllocationFromString(*s_tag_allocation)
277 if err != nil {
278 log.Fatal(err)
279 }
280
281 cTagAlloc, err := tagAllocationFromString(*c_tag_allocation)
282 if err != nil {
283 log.Fatal(err)
284 }
285
286 sf, err := sadisFormatFromString(*sadisFormat)
287 if err != nil {
288 log.Fatal(err)
289 }
290
291 if sf == SadisFormatTt {
292 log.Fatalf("Sadis format %s is not yet supported", sf.String())
293 }
294
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100295 conf.Olt.ID = int(*olt_id)
296 conf.Olt.NniPorts = uint32(*nni)
297 conf.Olt.PonPorts = uint32(*pon)
298 conf.Olt.OnusPonPort = uint32(*onu)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700299 conf.BBSim.STagAllocation = sTagAlloc
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100300 conf.BBSim.STag = int(*s_tag)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700301 conf.BBSim.CTagAllocation = cTagAlloc
302 conf.BBSim.CTag = int(*c_tag)
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100303 conf.BBSim.CpuProfile = profileCpu
304 conf.BBSim.LogLevel = *logLevel
305 conf.BBSim.LogCaller = *logCaller
306 conf.BBSim.EnableAuth = *auth
307 conf.BBSim.EnableDhcp = *dhcp
Shrey Baide72b3cc2020-05-12 00:03:06 +0530308 conf.BBSim.EnableIgmp = *igmp
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100309 conf.BBSim.Delay = *delay
Pragya Arya2225f202020-01-29 18:05:01 +0530310 conf.BBSim.ControlledActivation = *controlledActivation
Anand S Katti09541352020-01-29 15:54:01 +0530311 conf.BBSim.EnablePerf = *enablePerf
Pragya Arya324337e2020-02-20 14:35:08 +0530312 conf.BBSim.Events = *enableEvents
313 conf.BBSim.KafkaAddress = *kafkaAddress
rajeshf921f882020-03-06 18:24:28 +0530314 conf.BBSim.OpenOltAddress = *openolt_address
315 conf.BBSim.ApiAddress = *api_address
316 conf.BBSim.RestApiAddress = *rest_api_address
Matteo Scandolof65e6872020-04-15 15:18:43 -0700317 conf.BBSim.SadisFormat = sf
Shrey Baid64cda472020-04-24 18:58:18 +0530318 conf.BBSim.KafkaEventTopic = *kafkaEventTopic
Shrey Baidf8abccc2020-06-15 19:41:22 +0530319 conf.BBSim.AuthRetry = *authRetry
320 conf.BBSim.DhcpRetry = *dhcpRetry
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700321
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100322 // update device id if not set
323 if conf.Olt.DeviceId == "" {
324 conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String()
325 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700326
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100327 Options = conf
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100328 return conf
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700329}
330
331func GetBBROpts() BBRCliOptions {
332
333 bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP")
334 bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port")
335 bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port")
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700336 logFile := flag.String("logfile", "", "Log to a file")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700337
338 options := GetBBSimOpts()
339
340 bbrOptions := BBRCliOptions{
341 options,
342 *bbsimIp,
343 *bbsimPort,
344 *bbsimApiPort,
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700345 *logFile,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700346 }
347
348 return bbrOptions
349}