blob: 6088a1d54b9ac08b51046bbe0358547feaa76725 [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"
Matteo Scandolof65e6872020-04-15 15:18:43 -070023 "github.com/ghodss/yaml"
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010024 "io/ioutil"
Matteo Scandolof65e6872020-04-15 15:18:43 -070025 log "github.com/sirupsen/logrus"
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010026 "net"
Matteo Scandolof65e6872020-04-15 15:18:43 -070027 "strings"
28)
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)
51 return TagAllocation(0), errors.New(fmt.Sprintf("%s-is-not-a-valid-tag-allocation", s))
52}
53
54const (
55 _ TagAllocation = iota
56 TagAllocationShared
57 TagAllocationUnique
58)
59
60
61var 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"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100146}
Matteo Scandoloc1147092019-10-29 09:38:33 -0700147
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100148type BBRConfig struct {
149 Log string `yaml:"log"`
150 LogLevel string `yaml:"log_level"`
151 LogCaller bool `yaml:"log_caller"`
152}
153
154var Options *BBSimYamlConfig
155
156func init() {
157 // load settings from config file first
158 Options, _ = LoadBBSimConf("configs/bbsim.yaml")
159}
160
161func getDefaultOps() *BBSimYamlConfig {
162
163 c := &BBSimYamlConfig{
164 BBSimConfig{
Matteo Scandolof65e6872020-04-15 15:18:43 -0700165 STagAllocation: TagAllocationShared,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100166 STag: 900,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700167 CTagAllocation: TagAllocationUnique,
168 CTag: 900,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100169 EnableDhcp: false,
170 EnableAuth: false,
171 LogLevel: "debug",
172 LogCaller: false,
173 Delay: 200,
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100174 OpenOltAddress: ":50060",
175 ApiAddress: ":50070",
176 RestApiAddress: ":50071",
177 LegacyApiAddress: ":50072",
178 LegacyRestApiAddress: ":50073",
179 SadisRestAddress: ":50074",
180 SadisServer: true,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700181 SadisFormat: SadisFormatAtt,
Pragya Arya324337e2020-02-20 14:35:08 +0530182 KafkaAddress: ":9092",
183 Events: false,
Pragya Arya2225f202020-01-29 18:05:01 +0530184 ControlledActivation: "default",
Anand S Katti09541352020-01-29 15:54:01 +0530185 EnablePerf: false,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100186 },
187 OltConfig{
188 Vendor: "BBSim",
189 Model: "asfvolt16",
190 HardwareVersion: "emulated",
191 FirmwareVersion: "",
192 DeviceSerialNumber: "BBSM00000001",
193 PonPorts: 1,
194 NniPorts: 1,
195 OnusPonPort: 1,
196 Technology: "XGS-PON",
197 ID: 0,
198 OltRebootDelay: 10,
Pragya Arya996a0892020-03-09 21:47:52 +0530199 PortStatsInterval: 20,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100200 },
201 BBRConfig{
202 LogLevel: "debug",
203 LogCaller: false,
204 },
205 }
206 return c
207}
208
209// LoadBBSimConf loads the BBSim configuration from a YAML file
210func LoadBBSimConf(filename string) (*BBSimYamlConfig, error) {
211 yamlConfig := getDefaultOps()
212
213 yamlFile, err := ioutil.ReadFile(filename)
214 if err != nil {
215 fmt.Printf("Cannot load BBSim configuration file: %s. Using defaults.\n", err)
216 return yamlConfig, nil
217 }
218
219 err = yaml.Unmarshal(yamlFile, yamlConfig)
220 if err != nil {
221 fmt.Printf("Error parsing YAML file: %s\n", err)
222 }
223
Matteo Scandolof65e6872020-04-15 15:18:43 -0700224 // TODO convert from string to TagAllocation
225
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100226 return yamlConfig, nil
227}
228
Anand S Katti09541352020-01-29 15:54:01 +0530229// GetBBSimOpts loads the BBSim configuration file and over-rides options with corresponding CLI flags if set
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100230func GetBBSimOpts() *BBSimYamlConfig {
231 conf := Options
232
233 olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID")
234 nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated")
235 pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated")
236 onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated")
237
rajeshf921f882020-03-06 18:24:28 +0530238 openolt_address := flag.String("openolt_address", conf.BBSim.OpenOltAddress, "IP address:port")
239 api_address := flag.String("api_address", conf.BBSim.ApiAddress, "IP address:port")
240 rest_api_address := flag.String("rest_api_address", conf.BBSim.RestApiAddress, "IP address:port")
241
Matteo Scandolof65e6872020-04-15 15:18:43 -0700242 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 +0100243 s_tag := flag.Int("s_tag", conf.BBSim.STag, "S-Tag initial value")
Matteo Scandolof65e6872020-04-15 15:18:43 -0700244
245 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")
246 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)")
247
248 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 +0100249
250 auth := flag.Bool("auth", conf.BBSim.EnableAuth, "Set this flag if you want authentication to start automatically")
251 dhcp := flag.Bool("dhcp", conf.BBSim.EnableDhcp, "Set this flag if you want DHCP to start automatically")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700252
253 profileCpu := flag.String("cpuprofile", "", "write cpu profile to file")
254
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100255 logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)")
256 logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700257
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100258 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 -0700259
Pragya Arya2225f202020-01-29 18:05:01 +0530260 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 +0530261 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 +0530262 enableEvents := flag.Bool("enableEvents", conf.BBSim.Events, "Enable sending BBSim events on configured kafka server")
263 kafkaAddress := flag.String("kafkaAddress", conf.BBSim.KafkaAddress, "IP:Port for kafka")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700264 flag.Parse()
265
Matteo Scandolof65e6872020-04-15 15:18:43 -0700266 sTagAlloc, err := tagAllocationFromString(*s_tag_allocation)
267 if err != nil {
268 log.Fatal(err)
269 }
270
271 cTagAlloc, err := tagAllocationFromString(*c_tag_allocation)
272 if err != nil {
273 log.Fatal(err)
274 }
275
276 sf, err := sadisFormatFromString(*sadisFormat)
277 if err != nil {
278 log.Fatal(err)
279 }
280
281 if sf == SadisFormatTt {
282 log.Fatalf("Sadis format %s is not yet supported", sf.String())
283 }
284
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100285 conf.Olt.ID = int(*olt_id)
286 conf.Olt.NniPorts = uint32(*nni)
287 conf.Olt.PonPorts = uint32(*pon)
288 conf.Olt.OnusPonPort = uint32(*onu)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700289 conf.BBSim.STagAllocation = sTagAlloc
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100290 conf.BBSim.STag = int(*s_tag)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700291 conf.BBSim.CTagAllocation = cTagAlloc
292 conf.BBSim.CTag = int(*c_tag)
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100293 conf.BBSim.CpuProfile = profileCpu
294 conf.BBSim.LogLevel = *logLevel
295 conf.BBSim.LogCaller = *logCaller
296 conf.BBSim.EnableAuth = *auth
297 conf.BBSim.EnableDhcp = *dhcp
298 conf.BBSim.Delay = *delay
Pragya Arya2225f202020-01-29 18:05:01 +0530299 conf.BBSim.ControlledActivation = *controlledActivation
Anand S Katti09541352020-01-29 15:54:01 +0530300 conf.BBSim.EnablePerf = *enablePerf
Pragya Arya324337e2020-02-20 14:35:08 +0530301 conf.BBSim.Events = *enableEvents
302 conf.BBSim.KafkaAddress = *kafkaAddress
rajeshf921f882020-03-06 18:24:28 +0530303 conf.BBSim.OpenOltAddress = *openolt_address
304 conf.BBSim.ApiAddress = *api_address
305 conf.BBSim.RestApiAddress = *rest_api_address
Matteo Scandolof65e6872020-04-15 15:18:43 -0700306 conf.BBSim.SadisFormat = sf
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700307
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100308 // update device id if not set
309 if conf.Olt.DeviceId == "" {
310 conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String()
311 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700312
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100313 Options = conf
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100314 return conf
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700315}
316
317func GetBBROpts() BBRCliOptions {
318
319 bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP")
320 bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port")
321 bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port")
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700322 logFile := flag.String("logfile", "", "Log to a file")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700323
324 options := GetBBSimOpts()
325
326 bbrOptions := BBRCliOptions{
327 options,
328 *bbsimIp,
329 *bbsimPort,
330 *bbsimApiPort,
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700331 *logFile,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700332 }
333
334 return bbrOptions
335}