blob: 0ecb0c86e3a16df1950abd22fd971b00b3bdd60b [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 {
Matteo Scandolof65e6872020-04-15 15:18:43 -0700124 EnableDhcp bool `yaml:"enable_dhcp"`
125 EnableAuth bool `yaml:"enable_auth"`
126 LogLevel string `yaml:"log_level"`
127 LogCaller bool `yaml:"log_caller"`
128 Delay int `yaml:"delay"`
129 CpuProfile *string `yaml:"cpu_profile"`
130 CTagAllocation TagAllocation `yaml:"c_tag_allocation"`
131 CTag int `yaml:"c_tag"`
132 STagAllocation TagAllocation `yaml:"s_tag_allocation"`
133 STag int `yaml:"s_tag"`
134 OpenOltAddress string `yaml:"openolt_address"`
135 ApiAddress string `yaml:"api_address"`
136 RestApiAddress string `yaml:"rest_api_address"`
137 LegacyApiAddress string `yaml:"legacy_api_address"`
138 LegacyRestApiAddress string `yaml:"legacy_rest_api_address"`
139 SadisRestAddress string `yaml:"sadis_rest_address"`
140 SadisServer bool `yaml:"sadis_server"`
141 SadisFormat SadisFormat `yaml:"sadis_format"`
142 KafkaAddress string `yaml:"kafka_address"`
143 Events bool `yaml:"enable_events"`
144 ControlledActivation string `yaml:"controlled_activation"`
145 EnablePerf bool `yaml:"enable_perf"`
Shrey Baid64cda472020-04-24 18:58:18 +0530146 KafkaEventTopic string `yaml:"kafka_event_topic`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100147}
Matteo Scandoloc1147092019-10-29 09:38:33 -0700148
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100149type BBRConfig struct {
150 Log string `yaml:"log"`
151 LogLevel string `yaml:"log_level"`
152 LogCaller bool `yaml:"log_caller"`
153}
154
155var Options *BBSimYamlConfig
156
157func init() {
158 // load settings from config file first
159 Options, _ = LoadBBSimConf("configs/bbsim.yaml")
160}
161
162func getDefaultOps() *BBSimYamlConfig {
163
164 c := &BBSimYamlConfig{
165 BBSimConfig{
Matteo Scandolof65e6872020-04-15 15:18:43 -0700166 STagAllocation: TagAllocationShared,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100167 STag: 900,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700168 CTagAllocation: TagAllocationUnique,
169 CTag: 900,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100170 EnableDhcp: false,
171 EnableAuth: false,
172 LogLevel: "debug",
173 LogCaller: false,
174 Delay: 200,
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100175 OpenOltAddress: ":50060",
176 ApiAddress: ":50070",
177 RestApiAddress: ":50071",
178 LegacyApiAddress: ":50072",
179 LegacyRestApiAddress: ":50073",
180 SadisRestAddress: ":50074",
181 SadisServer: true,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700182 SadisFormat: SadisFormatAtt,
Pragya Arya324337e2020-02-20 14:35:08 +0530183 KafkaAddress: ":9092",
184 Events: false,
Pragya Arya2225f202020-01-29 18:05:01 +0530185 ControlledActivation: "default",
Anand S Katti09541352020-01-29 15:54:01 +0530186 EnablePerf: false,
Shrey Baid64cda472020-04-24 18:58:18 +0530187 KafkaEventTopic: "",
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100188 },
189 OltConfig{
190 Vendor: "BBSim",
191 Model: "asfvolt16",
192 HardwareVersion: "emulated",
193 FirmwareVersion: "",
194 DeviceSerialNumber: "BBSM00000001",
195 PonPorts: 1,
196 NniPorts: 1,
197 OnusPonPort: 1,
198 Technology: "XGS-PON",
199 ID: 0,
200 OltRebootDelay: 10,
Pragya Arya996a0892020-03-09 21:47:52 +0530201 PortStatsInterval: 20,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100202 },
203 BBRConfig{
204 LogLevel: "debug",
205 LogCaller: false,
206 },
207 }
208 return c
209}
210
211// LoadBBSimConf loads the BBSim configuration from a YAML file
212func LoadBBSimConf(filename string) (*BBSimYamlConfig, error) {
213 yamlConfig := getDefaultOps()
214
215 yamlFile, err := ioutil.ReadFile(filename)
216 if err != nil {
217 fmt.Printf("Cannot load BBSim configuration file: %s. Using defaults.\n", err)
218 return yamlConfig, nil
219 }
220
221 err = yaml.Unmarshal(yamlFile, yamlConfig)
222 if err != nil {
223 fmt.Printf("Error parsing YAML file: %s\n", err)
224 }
225
Matteo Scandolof65e6872020-04-15 15:18:43 -0700226 // TODO convert from string to TagAllocation
227
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100228 return yamlConfig, nil
229}
230
Anand S Katti09541352020-01-29 15:54:01 +0530231// GetBBSimOpts loads the BBSim configuration file and over-rides options with corresponding CLI flags if set
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100232func GetBBSimOpts() *BBSimYamlConfig {
233 conf := Options
234
235 olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID")
236 nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated")
237 pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated")
238 onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated")
239
rajeshf921f882020-03-06 18:24:28 +0530240 openolt_address := flag.String("openolt_address", conf.BBSim.OpenOltAddress, "IP address:port")
241 api_address := flag.String("api_address", conf.BBSim.ApiAddress, "IP address:port")
242 rest_api_address := flag.String("rest_api_address", conf.BBSim.RestApiAddress, "IP address:port")
243
Matteo Scandolof65e6872020-04-15 15:18:43 -0700244 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 +0100245 s_tag := flag.Int("s_tag", conf.BBSim.STag, "S-Tag initial value")
Matteo Scandolof65e6872020-04-15 15:18:43 -0700246
247 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")
248 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)")
249
250 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 +0100251
252 auth := flag.Bool("auth", conf.BBSim.EnableAuth, "Set this flag if you want authentication to start automatically")
253 dhcp := flag.Bool("dhcp", conf.BBSim.EnableDhcp, "Set this flag if you want DHCP to start automatically")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700254
255 profileCpu := flag.String("cpuprofile", "", "write cpu profile to file")
256
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100257 logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)")
258 logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700259
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100260 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 -0700261
Pragya Arya2225f202020-01-29 18:05:01 +0530262 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 +0530263 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 +0530264 enableEvents := flag.Bool("enableEvents", conf.BBSim.Events, "Enable sending BBSim events on configured kafka server")
265 kafkaAddress := flag.String("kafkaAddress", conf.BBSim.KafkaAddress, "IP:Port for kafka")
Shrey Baid64cda472020-04-24 18:58:18 +0530266 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 -0700267 flag.Parse()
268
Matteo Scandolof65e6872020-04-15 15:18:43 -0700269 sTagAlloc, err := tagAllocationFromString(*s_tag_allocation)
270 if err != nil {
271 log.Fatal(err)
272 }
273
274 cTagAlloc, err := tagAllocationFromString(*c_tag_allocation)
275 if err != nil {
276 log.Fatal(err)
277 }
278
279 sf, err := sadisFormatFromString(*sadisFormat)
280 if err != nil {
281 log.Fatal(err)
282 }
283
284 if sf == SadisFormatTt {
285 log.Fatalf("Sadis format %s is not yet supported", sf.String())
286 }
287
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100288 conf.Olt.ID = int(*olt_id)
289 conf.Olt.NniPorts = uint32(*nni)
290 conf.Olt.PonPorts = uint32(*pon)
291 conf.Olt.OnusPonPort = uint32(*onu)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700292 conf.BBSim.STagAllocation = sTagAlloc
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100293 conf.BBSim.STag = int(*s_tag)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700294 conf.BBSim.CTagAllocation = cTagAlloc
295 conf.BBSim.CTag = int(*c_tag)
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100296 conf.BBSim.CpuProfile = profileCpu
297 conf.BBSim.LogLevel = *logLevel
298 conf.BBSim.LogCaller = *logCaller
299 conf.BBSim.EnableAuth = *auth
300 conf.BBSim.EnableDhcp = *dhcp
301 conf.BBSim.Delay = *delay
Pragya Arya2225f202020-01-29 18:05:01 +0530302 conf.BBSim.ControlledActivation = *controlledActivation
Anand S Katti09541352020-01-29 15:54:01 +0530303 conf.BBSim.EnablePerf = *enablePerf
Pragya Arya324337e2020-02-20 14:35:08 +0530304 conf.BBSim.Events = *enableEvents
305 conf.BBSim.KafkaAddress = *kafkaAddress
rajeshf921f882020-03-06 18:24:28 +0530306 conf.BBSim.OpenOltAddress = *openolt_address
307 conf.BBSim.ApiAddress = *api_address
308 conf.BBSim.RestApiAddress = *rest_api_address
Matteo Scandolof65e6872020-04-15 15:18:43 -0700309 conf.BBSim.SadisFormat = sf
Shrey Baid64cda472020-04-24 18:58:18 +0530310 conf.BBSim.KafkaEventTopic = *kafkaEventTopic
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700311
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100312 // update device id if not set
313 if conf.Olt.DeviceId == "" {
314 conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String()
315 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700316
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100317 Options = conf
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100318 return conf
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700319}
320
321func GetBBROpts() BBRCliOptions {
322
323 bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP")
324 bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port")
325 bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port")
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700326 logFile := flag.String("logfile", "", "Log to a file")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700327
328 options := GetBBSimOpts()
329
330 bbrOptions := BBRCliOptions{
331 options,
332 *bbsimIp,
333 *bbsimPort,
334 *bbsimApiPort,
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700335 *logFile,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700336 }
337
338 return bbrOptions
339}