blob: 6c728fbc494d629d3093747edfd1a3de21400cf2 [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 (
Matteo Scandolof65e6872020-04-15 15:18:43 -070020 "errors"
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010021 "flag"
22 "fmt"
23 "io/ioutil"
24 "net"
Matteo Scandolof65e6872020-04-15 15:18:43 -070025 "strings"
Shrey Baid64cda472020-04-24 18:58:18 +053026
27 "github.com/ghodss/yaml"
28 log "github.com/sirupsen/logrus"
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
37type TagAllocation int
38
39func (t TagAllocation) String() string {
40 return tagAllocationValues[t]
41}
42
43func tagAllocationFromString(s string) (TagAllocation, error) {
44 for i, v := range tagAllocationValues {
45 if v == s {
46 return TagAllocation(i), nil
47 }
48 }
49 log.WithFields(log.Fields{
50 "ValidValues": strings.Join(tagAllocationValues[1:], ", "),
51 }).Errorf("%s-is-not-a-valid-tag-allocation", s)
52 return TagAllocation(0), errors.New(fmt.Sprintf("%s-is-not-a-valid-tag-allocation", s))
53}
54
55const (
56 _ TagAllocation = iota
57 TagAllocationShared
58 TagAllocationUnique
59)
60
Matteo Scandolof65e6872020-04-15 15:18:43 -070061var sadisFormatValues = []string{
62 "unknown",
63 "att",
64 "dt",
65 "tt",
66}
67
68type SadisFormat int
69
70func (s SadisFormat) String() string {
71 return sadisFormatValues[s]
72}
73
74func sadisFormatFromString(s string) (SadisFormat, error) {
75 for i, v := range sadisFormatValues {
76 if v == s {
77 return SadisFormat(i), nil
78 }
79 }
80 log.WithFields(log.Fields{
81 "ValidValues": strings.Join(sadisFormatValues[1:], ", "),
82 }).Errorf("%s-is-not-a-valid-sadis-format", s)
83 return SadisFormat(0), errors.New(fmt.Sprintf("%s-is-not-a-valid-sadis-format", s))
84}
85
86const (
87 _ SadisFormat = iota
88 SadisFormatAtt
89 SadisFormatDt
90 SadisFormatTt
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010091)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070092
93type BBRCliOptions struct {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010094 *BBSimYamlConfig
Matteo Scandolo40e067f2019-10-16 16:59:41 -070095 BBSimIp string
96 BBSimPort string
97 BBSimApiPort string
Matteo Scandolof5c537e2019-10-28 16:45:57 -070098 LogFile string
Matteo Scandolo40e067f2019-10-16 16:59:41 -070099}
100
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100101type BBSimYamlConfig struct {
102 BBSim BBSimConfig
103 Olt OltConfig
104 BBR BBRConfig
105}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700106
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100107type OltConfig struct {
108 Model string `yaml:"model"`
109 Vendor string `yaml:"vendor"`
110 HardwareVersion string `yaml:"hardware_version"`
111 FirmwareVersion string `yaml:"firmware_version"`
112 DeviceId string `yaml:"device_id"`
113 DeviceSerialNumber string `yaml:"device_serial_number"`
114 PonPorts uint32 `yaml:"pon_ports"`
115 NniPorts uint32 `yaml:"nni_ports"`
116 OnusPonPort uint32 `yaml:"onus_per_port"`
117 Technology string `yaml:"technology"`
118 ID int `yaml:"id"`
119 OltRebootDelay int `yaml:"reboot_delay"`
Pragya Arya996a0892020-03-09 21:47:52 +0530120 PortStatsInterval int `yaml: "port_stats_interval"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100121}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700122
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100123type BBSimConfig struct {
Shrey Baidf8abccc2020-06-15 19:41:22 +0530124 DhcpRetry bool `yaml:"dhcp_retry"`
125 AuthRetry bool `yaml:"auth_retry"`
Shrey Baide72b3cc2020-05-12 00:03:06 +0530126 EnableIgmp bool `yaml:"enable_igmp"`
Matteo Scandolof65e6872020-04-15 15:18:43 -0700127 EnableDhcp bool `yaml:"enable_dhcp"`
128 EnableAuth bool `yaml:"enable_auth"`
129 LogLevel string `yaml:"log_level"`
130 LogCaller bool `yaml:"log_caller"`
131 Delay int `yaml:"delay"`
132 CpuProfile *string `yaml:"cpu_profile"`
133 CTagAllocation TagAllocation `yaml:"c_tag_allocation"`
134 CTag int `yaml:"c_tag"`
135 STagAllocation TagAllocation `yaml:"s_tag_allocation"`
136 STag int `yaml:"s_tag"`
137 OpenOltAddress string `yaml:"openolt_address"`
138 ApiAddress string `yaml:"api_address"`
139 RestApiAddress string `yaml:"rest_api_address"`
140 LegacyApiAddress string `yaml:"legacy_api_address"`
141 LegacyRestApiAddress string `yaml:"legacy_rest_api_address"`
142 SadisRestAddress string `yaml:"sadis_rest_address"`
143 SadisServer bool `yaml:"sadis_server"`
144 SadisFormat SadisFormat `yaml:"sadis_format"`
145 KafkaAddress string `yaml:"kafka_address"`
146 Events bool `yaml:"enable_events"`
147 ControlledActivation string `yaml:"controlled_activation"`
148 EnablePerf bool `yaml:"enable_perf"`
Shrey Baid64cda472020-04-24 18:58:18 +0530149 KafkaEventTopic string `yaml:"kafka_event_topic`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100150}
Matteo Scandoloc1147092019-10-29 09:38:33 -0700151
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100152type BBRConfig struct {
153 Log string `yaml:"log"`
154 LogLevel string `yaml:"log_level"`
155 LogCaller bool `yaml:"log_caller"`
156}
157
158var Options *BBSimYamlConfig
159
160func init() {
161 // load settings from config file first
162 Options, _ = LoadBBSimConf("configs/bbsim.yaml")
163}
164
165func getDefaultOps() *BBSimYamlConfig {
166
167 c := &BBSimYamlConfig{
168 BBSimConfig{
Matteo Scandolof65e6872020-04-15 15:18:43 -0700169 STagAllocation: TagAllocationShared,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100170 STag: 900,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700171 CTagAllocation: TagAllocationUnique,
172 CTag: 900,
Shrey Baide72b3cc2020-05-12 00:03:06 +0530173 EnableIgmp: false,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100174 EnableDhcp: false,
175 EnableAuth: false,
176 LogLevel: "debug",
177 LogCaller: false,
178 Delay: 200,
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100179 OpenOltAddress: ":50060",
180 ApiAddress: ":50070",
181 RestApiAddress: ":50071",
182 LegacyApiAddress: ":50072",
183 LegacyRestApiAddress: ":50073",
184 SadisRestAddress: ":50074",
185 SadisServer: true,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700186 SadisFormat: SadisFormatAtt,
Pragya Arya324337e2020-02-20 14:35:08 +0530187 KafkaAddress: ":9092",
188 Events: false,
Pragya Arya2225f202020-01-29 18:05:01 +0530189 ControlledActivation: "default",
Anand S Katti09541352020-01-29 15:54:01 +0530190 EnablePerf: false,
Shrey Baid64cda472020-04-24 18:58:18 +0530191 KafkaEventTopic: "",
Shrey Baidf8abccc2020-06-15 19:41:22 +0530192 DhcpRetry: false,
193 AuthRetry: false,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100194 },
195 OltConfig{
196 Vendor: "BBSim",
197 Model: "asfvolt16",
198 HardwareVersion: "emulated",
199 FirmwareVersion: "",
200 DeviceSerialNumber: "BBSM00000001",
201 PonPorts: 1,
202 NniPorts: 1,
203 OnusPonPort: 1,
204 Technology: "XGS-PON",
205 ID: 0,
206 OltRebootDelay: 10,
Pragya Arya996a0892020-03-09 21:47:52 +0530207 PortStatsInterval: 20,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100208 },
209 BBRConfig{
210 LogLevel: "debug",
211 LogCaller: false,
212 },
213 }
214 return c
215}
216
217// LoadBBSimConf loads the BBSim configuration from a YAML file
218func LoadBBSimConf(filename string) (*BBSimYamlConfig, error) {
219 yamlConfig := getDefaultOps()
220
221 yamlFile, err := ioutil.ReadFile(filename)
222 if err != nil {
223 fmt.Printf("Cannot load BBSim configuration file: %s. Using defaults.\n", err)
224 return yamlConfig, nil
225 }
226
227 err = yaml.Unmarshal(yamlFile, yamlConfig)
228 if err != nil {
229 fmt.Printf("Error parsing YAML file: %s\n", err)
230 }
231
Matteo Scandolof65e6872020-04-15 15:18:43 -0700232 // TODO convert from string to TagAllocation
233
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100234 return yamlConfig, nil
235}
236
Anand S Katti09541352020-01-29 15:54:01 +0530237// GetBBSimOpts loads the BBSim configuration file and over-rides options with corresponding CLI flags if set
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100238func GetBBSimOpts() *BBSimYamlConfig {
239 conf := Options
240
241 olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID")
242 nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated")
243 pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated")
244 onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated")
245
rajeshf921f882020-03-06 18:24:28 +0530246 openolt_address := flag.String("openolt_address", conf.BBSim.OpenOltAddress, "IP address:port")
247 api_address := flag.String("api_address", conf.BBSim.ApiAddress, "IP address:port")
248 rest_api_address := flag.String("rest_api_address", conf.BBSim.RestApiAddress, "IP address:port")
249
Matteo Scandolof65e6872020-04-15 15:18:43 -0700250 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 +0100251 s_tag := flag.Int("s_tag", conf.BBSim.STag, "S-Tag initial value")
Matteo Scandolof65e6872020-04-15 15:18:43 -0700252
253 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")
254 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)")
255
256 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 +0100257
258 auth := flag.Bool("auth", conf.BBSim.EnableAuth, "Set this flag if you want authentication to start automatically")
259 dhcp := flag.Bool("dhcp", conf.BBSim.EnableDhcp, "Set this flag if you want DHCP to start automatically")
Shrey Baide72b3cc2020-05-12 00:03:06 +0530260 igmp := flag.Bool("igmp", conf.BBSim.EnableIgmp, "Set this flag if you want IGMP to start automatically")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700261 profileCpu := flag.String("cpuprofile", "", "write cpu profile to file")
262
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100263 logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)")
264 logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700265
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100266 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 -0700267
Pragya Arya2225f202020-01-29 18:05:01 +0530268 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 +0530269 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 +0530270 enableEvents := flag.Bool("enableEvents", conf.BBSim.Events, "Enable sending BBSim events on configured kafka server")
271 kafkaAddress := flag.String("kafkaAddress", conf.BBSim.KafkaAddress, "IP:Port for kafka")
Shrey Baid64cda472020-04-24 18:58:18 +0530272 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 +0530273 dhcpRetry := flag.Bool("dhcpRetry", conf.BBSim.DhcpRetry, "Set this flag if BBSim should retry DHCP upon failure until success")
274 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 -0700275 flag.Parse()
276
Matteo Scandolof65e6872020-04-15 15:18:43 -0700277 sTagAlloc, err := tagAllocationFromString(*s_tag_allocation)
278 if err != nil {
279 log.Fatal(err)
280 }
281
282 cTagAlloc, err := tagAllocationFromString(*c_tag_allocation)
283 if err != nil {
284 log.Fatal(err)
285 }
286
287 sf, err := sadisFormatFromString(*sadisFormat)
288 if err != nil {
289 log.Fatal(err)
290 }
291
292 if sf == SadisFormatTt {
293 log.Fatalf("Sadis format %s is not yet supported", sf.String())
294 }
295
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100296 conf.Olt.ID = int(*olt_id)
297 conf.Olt.NniPorts = uint32(*nni)
298 conf.Olt.PonPorts = uint32(*pon)
299 conf.Olt.OnusPonPort = uint32(*onu)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700300 conf.BBSim.STagAllocation = sTagAlloc
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100301 conf.BBSim.STag = int(*s_tag)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700302 conf.BBSim.CTagAllocation = cTagAlloc
303 conf.BBSim.CTag = int(*c_tag)
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100304 conf.BBSim.CpuProfile = profileCpu
305 conf.BBSim.LogLevel = *logLevel
306 conf.BBSim.LogCaller = *logCaller
307 conf.BBSim.EnableAuth = *auth
308 conf.BBSim.EnableDhcp = *dhcp
Shrey Baide72b3cc2020-05-12 00:03:06 +0530309 conf.BBSim.EnableIgmp = *igmp
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100310 conf.BBSim.Delay = *delay
Pragya Arya2225f202020-01-29 18:05:01 +0530311 conf.BBSim.ControlledActivation = *controlledActivation
Anand S Katti09541352020-01-29 15:54:01 +0530312 conf.BBSim.EnablePerf = *enablePerf
Pragya Arya324337e2020-02-20 14:35:08 +0530313 conf.BBSim.Events = *enableEvents
314 conf.BBSim.KafkaAddress = *kafkaAddress
rajeshf921f882020-03-06 18:24:28 +0530315 conf.BBSim.OpenOltAddress = *openolt_address
316 conf.BBSim.ApiAddress = *api_address
317 conf.BBSim.RestApiAddress = *rest_api_address
Matteo Scandolof65e6872020-04-15 15:18:43 -0700318 conf.BBSim.SadisFormat = sf
Shrey Baid64cda472020-04-24 18:58:18 +0530319 conf.BBSim.KafkaEventTopic = *kafkaEventTopic
Shrey Baidf8abccc2020-06-15 19:41:22 +0530320 conf.BBSim.AuthRetry = *authRetry
321 conf.BBSim.DhcpRetry = *dhcpRetry
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700322
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100323 // update device id if not set
324 if conf.Olt.DeviceId == "" {
325 conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String()
326 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700327
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100328 Options = conf
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100329 return conf
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700330}
331
332func GetBBROpts() BBRCliOptions {
333
334 bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP")
335 bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port")
336 bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port")
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700337 logFile := flag.String("logfile", "", "Log to a file")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700338
339 options := GetBBSimOpts()
340
341 bbrOptions := BBRCliOptions{
342 options,
343 *bbsimIp,
344 *bbsimPort,
345 *bbsimApiPort,
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700346 *logFile,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700347 }
348
349 return bbrOptions
350}