blob: b7f8bf63037ce4c076e90db8d83bec2c4d020e0b [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 Baide72b3cc2020-05-12 00:03:06 +0530124 EnableIgmp bool `yaml:"enable_igmp"`
Matteo Scandolof65e6872020-04-15 15:18:43 -0700125 EnableDhcp bool `yaml:"enable_dhcp"`
126 EnableAuth bool `yaml:"enable_auth"`
127 LogLevel string `yaml:"log_level"`
128 LogCaller bool `yaml:"log_caller"`
129 Delay int `yaml:"delay"`
130 CpuProfile *string `yaml:"cpu_profile"`
131 CTagAllocation TagAllocation `yaml:"c_tag_allocation"`
132 CTag int `yaml:"c_tag"`
133 STagAllocation TagAllocation `yaml:"s_tag_allocation"`
134 STag int `yaml:"s_tag"`
135 OpenOltAddress string `yaml:"openolt_address"`
136 ApiAddress string `yaml:"api_address"`
137 RestApiAddress string `yaml:"rest_api_address"`
138 LegacyApiAddress string `yaml:"legacy_api_address"`
139 LegacyRestApiAddress string `yaml:"legacy_rest_api_address"`
140 SadisRestAddress string `yaml:"sadis_rest_address"`
141 SadisServer bool `yaml:"sadis_server"`
142 SadisFormat SadisFormat `yaml:"sadis_format"`
143 KafkaAddress string `yaml:"kafka_address"`
144 Events bool `yaml:"enable_events"`
145 ControlledActivation string `yaml:"controlled_activation"`
146 EnablePerf bool `yaml:"enable_perf"`
Shrey Baid64cda472020-04-24 18:58:18 +0530147 KafkaEventTopic string `yaml:"kafka_event_topic`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100148}
Matteo Scandoloc1147092019-10-29 09:38:33 -0700149
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100150type BBRConfig struct {
151 Log string `yaml:"log"`
152 LogLevel string `yaml:"log_level"`
153 LogCaller bool `yaml:"log_caller"`
154}
155
156var Options *BBSimYamlConfig
157
158func init() {
159 // load settings from config file first
160 Options, _ = LoadBBSimConf("configs/bbsim.yaml")
161}
162
163func getDefaultOps() *BBSimYamlConfig {
164
165 c := &BBSimYamlConfig{
166 BBSimConfig{
Matteo Scandolof65e6872020-04-15 15:18:43 -0700167 STagAllocation: TagAllocationShared,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100168 STag: 900,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700169 CTagAllocation: TagAllocationUnique,
170 CTag: 900,
Shrey Baide72b3cc2020-05-12 00:03:06 +0530171 EnableIgmp: false,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100172 EnableDhcp: false,
173 EnableAuth: false,
174 LogLevel: "debug",
175 LogCaller: false,
176 Delay: 200,
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100177 OpenOltAddress: ":50060",
178 ApiAddress: ":50070",
179 RestApiAddress: ":50071",
180 LegacyApiAddress: ":50072",
181 LegacyRestApiAddress: ":50073",
182 SadisRestAddress: ":50074",
183 SadisServer: true,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700184 SadisFormat: SadisFormatAtt,
Pragya Arya324337e2020-02-20 14:35:08 +0530185 KafkaAddress: ":9092",
186 Events: false,
Pragya Arya2225f202020-01-29 18:05:01 +0530187 ControlledActivation: "default",
Anand S Katti09541352020-01-29 15:54:01 +0530188 EnablePerf: false,
Shrey Baid64cda472020-04-24 18:58:18 +0530189 KafkaEventTopic: "",
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100190 },
191 OltConfig{
192 Vendor: "BBSim",
193 Model: "asfvolt16",
194 HardwareVersion: "emulated",
195 FirmwareVersion: "",
196 DeviceSerialNumber: "BBSM00000001",
197 PonPorts: 1,
198 NniPorts: 1,
199 OnusPonPort: 1,
200 Technology: "XGS-PON",
201 ID: 0,
202 OltRebootDelay: 10,
Pragya Arya996a0892020-03-09 21:47:52 +0530203 PortStatsInterval: 20,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100204 },
205 BBRConfig{
206 LogLevel: "debug",
207 LogCaller: false,
208 },
209 }
210 return c
211}
212
213// LoadBBSimConf loads the BBSim configuration from a YAML file
214func LoadBBSimConf(filename string) (*BBSimYamlConfig, error) {
215 yamlConfig := getDefaultOps()
216
217 yamlFile, err := ioutil.ReadFile(filename)
218 if err != nil {
219 fmt.Printf("Cannot load BBSim configuration file: %s. Using defaults.\n", err)
220 return yamlConfig, nil
221 }
222
223 err = yaml.Unmarshal(yamlFile, yamlConfig)
224 if err != nil {
225 fmt.Printf("Error parsing YAML file: %s\n", err)
226 }
227
Matteo Scandolof65e6872020-04-15 15:18:43 -0700228 // TODO convert from string to TagAllocation
229
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100230 return yamlConfig, nil
231}
232
Anand S Katti09541352020-01-29 15:54:01 +0530233// GetBBSimOpts loads the BBSim configuration file and over-rides options with corresponding CLI flags if set
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100234func GetBBSimOpts() *BBSimYamlConfig {
235 conf := Options
236
237 olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID")
238 nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated")
239 pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated")
240 onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated")
241
rajeshf921f882020-03-06 18:24:28 +0530242 openolt_address := flag.String("openolt_address", conf.BBSim.OpenOltAddress, "IP address:port")
243 api_address := flag.String("api_address", conf.BBSim.ApiAddress, "IP address:port")
244 rest_api_address := flag.String("rest_api_address", conf.BBSim.RestApiAddress, "IP address:port")
245
Matteo Scandolof65e6872020-04-15 15:18:43 -0700246 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 +0100247 s_tag := flag.Int("s_tag", conf.BBSim.STag, "S-Tag initial value")
Matteo Scandolof65e6872020-04-15 15:18:43 -0700248
249 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")
250 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)")
251
252 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 +0100253
254 auth := flag.Bool("auth", conf.BBSim.EnableAuth, "Set this flag if you want authentication to start automatically")
255 dhcp := flag.Bool("dhcp", conf.BBSim.EnableDhcp, "Set this flag if you want DHCP to start automatically")
Shrey Baide72b3cc2020-05-12 00:03:06 +0530256 igmp := flag.Bool("igmp", conf.BBSim.EnableIgmp, "Set this flag if you want IGMP to start automatically")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700257 profileCpu := flag.String("cpuprofile", "", "write cpu profile to file")
258
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100259 logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)")
260 logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700261
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100262 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 -0700263
Pragya Arya2225f202020-01-29 18:05:01 +0530264 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 +0530265 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 +0530266 enableEvents := flag.Bool("enableEvents", conf.BBSim.Events, "Enable sending BBSim events on configured kafka server")
267 kafkaAddress := flag.String("kafkaAddress", conf.BBSim.KafkaAddress, "IP:Port for kafka")
Shrey Baid64cda472020-04-24 18:58:18 +0530268 kafkaEventTopic := flag.String("kafkaEventTopic", conf.BBSim.KafkaEventTopic, "Ability to configure the topic on which BBSim publishes events on Kafka")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700269 flag.Parse()
270
Matteo Scandolof65e6872020-04-15 15:18:43 -0700271 sTagAlloc, err := tagAllocationFromString(*s_tag_allocation)
272 if err != nil {
273 log.Fatal(err)
274 }
275
276 cTagAlloc, err := tagAllocationFromString(*c_tag_allocation)
277 if err != nil {
278 log.Fatal(err)
279 }
280
281 sf, err := sadisFormatFromString(*sadisFormat)
282 if err != nil {
283 log.Fatal(err)
284 }
285
286 if sf == SadisFormatTt {
287 log.Fatalf("Sadis format %s is not yet supported", sf.String())
288 }
289
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100290 conf.Olt.ID = int(*olt_id)
291 conf.Olt.NniPorts = uint32(*nni)
292 conf.Olt.PonPorts = uint32(*pon)
293 conf.Olt.OnusPonPort = uint32(*onu)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700294 conf.BBSim.STagAllocation = sTagAlloc
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100295 conf.BBSim.STag = int(*s_tag)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700296 conf.BBSim.CTagAllocation = cTagAlloc
297 conf.BBSim.CTag = int(*c_tag)
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100298 conf.BBSim.CpuProfile = profileCpu
299 conf.BBSim.LogLevel = *logLevel
300 conf.BBSim.LogCaller = *logCaller
301 conf.BBSim.EnableAuth = *auth
302 conf.BBSim.EnableDhcp = *dhcp
Shrey Baide72b3cc2020-05-12 00:03:06 +0530303 conf.BBSim.EnableIgmp = *igmp
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100304 conf.BBSim.Delay = *delay
Pragya Arya2225f202020-01-29 18:05:01 +0530305 conf.BBSim.ControlledActivation = *controlledActivation
Anand S Katti09541352020-01-29 15:54:01 +0530306 conf.BBSim.EnablePerf = *enablePerf
Pragya Arya324337e2020-02-20 14:35:08 +0530307 conf.BBSim.Events = *enableEvents
308 conf.BBSim.KafkaAddress = *kafkaAddress
rajeshf921f882020-03-06 18:24:28 +0530309 conf.BBSim.OpenOltAddress = *openolt_address
310 conf.BBSim.ApiAddress = *api_address
311 conf.BBSim.RestApiAddress = *rest_api_address
Matteo Scandolof65e6872020-04-15 15:18:43 -0700312 conf.BBSim.SadisFormat = sf
Shrey Baid64cda472020-04-24 18:58:18 +0530313 conf.BBSim.KafkaEventTopic = *kafkaEventTopic
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700314
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100315 // update device id if not set
316 if conf.Olt.DeviceId == "" {
317 conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String()
318 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700319
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100320 Options = conf
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100321 return conf
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700322}
323
324func GetBBROpts() BBRCliOptions {
325
326 bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP")
327 bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port")
328 bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port")
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700329 logFile := flag.String("logfile", "", "Log to a file")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700330
331 options := GetBBSimOpts()
332
333 bbrOptions := BBRCliOptions{
334 options,
335 *bbsimIp,
336 *bbsimPort,
337 *bbsimApiPort,
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700338 *logFile,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700339 }
340
341 return bbrOptions
342}