blob: d7bc9ebaaa109b3e9c18ec1bb0da4f1960c2e98c [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 Scandolo40e067f2019-10-16 16:59:41 -070024
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010025 "gopkg.in/yaml.v2"
26)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070027
28type BBRCliOptions struct {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010029 *BBSimYamlConfig
Matteo Scandolo40e067f2019-10-16 16:59:41 -070030 BBSimIp string
31 BBSimPort string
32 BBSimApiPort string
Matteo Scandolof5c537e2019-10-28 16:45:57 -070033 LogFile string
Matteo Scandolo40e067f2019-10-16 16:59:41 -070034}
35
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010036type BBSimYamlConfig struct {
37 BBSim BBSimConfig
38 Olt OltConfig
39 BBR BBRConfig
40}
Matteo Scandolo40e067f2019-10-16 16:59:41 -070041
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010042type OltConfig struct {
43 Model string `yaml:"model"`
44 Vendor string `yaml:"vendor"`
45 HardwareVersion string `yaml:"hardware_version"`
46 FirmwareVersion string `yaml:"firmware_version"`
47 DeviceId string `yaml:"device_id"`
48 DeviceSerialNumber string `yaml:"device_serial_number"`
49 PonPorts uint32 `yaml:"pon_ports"`
50 NniPorts uint32 `yaml:"nni_ports"`
51 OnusPonPort uint32 `yaml:"onus_per_port"`
52 Technology string `yaml:"technology"`
53 ID int `yaml:"id"`
54 OltRebootDelay int `yaml:"reboot_delay"`
Pragya Arya996a0892020-03-09 21:47:52 +053055 PortStatsInterval int `yaml: "port_stats_interval"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010056}
Matteo Scandolo40e067f2019-10-16 16:59:41 -070057
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010058type BBSimConfig struct {
59 EnableDhcp bool `yaml:"enable_dhcp"`
60 EnableAuth bool `yaml:"enable_auth"`
61 LogLevel string `yaml:"log_level"`
62 LogCaller bool `yaml:"log_caller"`
63 Delay int `yaml:"delay"`
64 CpuProfile *string `yaml:"cpu_profile"`
65 CTagInit int `yaml:"c_tag"`
66 STag int `yaml:"s_tag"`
67 OpenOltAddress string `yaml:"openolt_address"`
68 ApiAddress string `yaml:"api_address"`
69 RestApiAddress string `yaml:"rest_api_address"`
70 LegacyApiAddress string `yaml:"legacy_api_address"`
71 LegacyRestApiAddress string `yaml:"legacy_rest_api_address"`
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010072 SadisRestAddress string `yaml:"sadis_rest_address"`
73 SadisServer bool `yaml:"sadis_server"`
Pragya Arya324337e2020-02-20 14:35:08 +053074 KafkaAddress string `yaml:"kafka_address"`
75 Events bool `yaml:"enable_events"`
Pragya Arya2225f202020-01-29 18:05:01 +053076 ControlledActivation string `yaml:"controlled_activation"`
Anand S Katti09541352020-01-29 15:54:01 +053077 EnablePerf bool `yaml:"enable_perf"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010078}
Matteo Scandoloc1147092019-10-29 09:38:33 -070079
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010080type BBRConfig struct {
81 Log string `yaml:"log"`
82 LogLevel string `yaml:"log_level"`
83 LogCaller bool `yaml:"log_caller"`
84}
85
86var Options *BBSimYamlConfig
87
88func init() {
89 // load settings from config file first
90 Options, _ = LoadBBSimConf("configs/bbsim.yaml")
91}
92
93func getDefaultOps() *BBSimYamlConfig {
94
95 c := &BBSimYamlConfig{
96 BBSimConfig{
97 STag: 900,
98 CTagInit: 900,
99 EnableDhcp: false,
100 EnableAuth: false,
101 LogLevel: "debug",
102 LogCaller: false,
103 Delay: 200,
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100104 OpenOltAddress: ":50060",
105 ApiAddress: ":50070",
106 RestApiAddress: ":50071",
107 LegacyApiAddress: ":50072",
108 LegacyRestApiAddress: ":50073",
109 SadisRestAddress: ":50074",
110 SadisServer: true,
Pragya Arya324337e2020-02-20 14:35:08 +0530111 KafkaAddress: ":9092",
112 Events: false,
Pragya Arya2225f202020-01-29 18:05:01 +0530113 ControlledActivation: "default",
Anand S Katti09541352020-01-29 15:54:01 +0530114 EnablePerf: false,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100115 },
116 OltConfig{
117 Vendor: "BBSim",
118 Model: "asfvolt16",
119 HardwareVersion: "emulated",
120 FirmwareVersion: "",
121 DeviceSerialNumber: "BBSM00000001",
122 PonPorts: 1,
123 NniPorts: 1,
124 OnusPonPort: 1,
125 Technology: "XGS-PON",
126 ID: 0,
127 OltRebootDelay: 10,
Pragya Arya996a0892020-03-09 21:47:52 +0530128 PortStatsInterval: 20,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100129 },
130 BBRConfig{
131 LogLevel: "debug",
132 LogCaller: false,
133 },
134 }
135 return c
136}
137
138// LoadBBSimConf loads the BBSim configuration from a YAML file
139func LoadBBSimConf(filename string) (*BBSimYamlConfig, error) {
140 yamlConfig := getDefaultOps()
141
142 yamlFile, err := ioutil.ReadFile(filename)
143 if err != nil {
144 fmt.Printf("Cannot load BBSim configuration file: %s. Using defaults.\n", err)
145 return yamlConfig, nil
146 }
147
148 err = yaml.Unmarshal(yamlFile, yamlConfig)
149 if err != nil {
150 fmt.Printf("Error parsing YAML file: %s\n", err)
151 }
152
153 return yamlConfig, nil
154}
155
Anand S Katti09541352020-01-29 15:54:01 +0530156// GetBBSimOpts loads the BBSim configuration file and over-rides options with corresponding CLI flags if set
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100157func GetBBSimOpts() *BBSimYamlConfig {
158 conf := Options
159
160 olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID")
161 nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated")
162 pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated")
163 onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated")
164
rajeshf921f882020-03-06 18:24:28 +0530165 openolt_address := flag.String("openolt_address", conf.BBSim.OpenOltAddress, "IP address:port")
166 api_address := flag.String("api_address", conf.BBSim.ApiAddress, "IP address:port")
167 rest_api_address := flag.String("rest_api_address", conf.BBSim.RestApiAddress, "IP address:port")
168
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100169 s_tag := flag.Int("s_tag", conf.BBSim.STag, "S-Tag initial value")
170 c_tag_init := flag.Int("c_tag", conf.BBSim.CTagInit, "C-Tag starting value, each ONU will get a sequential one (targeting 1024 ONUs per BBSim instance the range is big enough)")
171
172 auth := flag.Bool("auth", conf.BBSim.EnableAuth, "Set this flag if you want authentication to start automatically")
173 dhcp := flag.Bool("dhcp", conf.BBSim.EnableDhcp, "Set this flag if you want DHCP to start automatically")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700174
175 profileCpu := flag.String("cpuprofile", "", "write cpu profile to file")
176
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100177 logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)")
178 logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700179
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100180 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 -0700181
Pragya Arya2225f202020-01-29 18:05:01 +0530182 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 +0530183 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 +0530184 enableEvents := flag.Bool("enableEvents", conf.BBSim.Events, "Enable sending BBSim events on configured kafka server")
185 kafkaAddress := flag.String("kafkaAddress", conf.BBSim.KafkaAddress, "IP:Port for kafka")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700186 flag.Parse()
187
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100188 conf.Olt.ID = int(*olt_id)
189 conf.Olt.NniPorts = uint32(*nni)
190 conf.Olt.PonPorts = uint32(*pon)
191 conf.Olt.OnusPonPort = uint32(*onu)
192 conf.BBSim.STag = int(*s_tag)
193 conf.BBSim.CTagInit = int(*c_tag_init)
194 conf.BBSim.CpuProfile = profileCpu
195 conf.BBSim.LogLevel = *logLevel
196 conf.BBSim.LogCaller = *logCaller
197 conf.BBSim.EnableAuth = *auth
198 conf.BBSim.EnableDhcp = *dhcp
199 conf.BBSim.Delay = *delay
Pragya Arya2225f202020-01-29 18:05:01 +0530200 conf.BBSim.ControlledActivation = *controlledActivation
Anand S Katti09541352020-01-29 15:54:01 +0530201 conf.BBSim.EnablePerf = *enablePerf
Pragya Arya324337e2020-02-20 14:35:08 +0530202 conf.BBSim.Events = *enableEvents
203 conf.BBSim.KafkaAddress = *kafkaAddress
rajeshf921f882020-03-06 18:24:28 +0530204 conf.BBSim.OpenOltAddress = *openolt_address
205 conf.BBSim.ApiAddress = *api_address
206 conf.BBSim.RestApiAddress = *rest_api_address
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700207
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100208 // update device id if not set
209 if conf.Olt.DeviceId == "" {
210 conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String()
211 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700212
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100213 Options = conf
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100214 return conf
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700215}
216
217func GetBBROpts() BBRCliOptions {
218
219 bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP")
220 bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port")
221 bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port")
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700222 logFile := flag.String("logfile", "", "Log to a file")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700223
224 options := GetBBSimOpts()
225
226 bbrOptions := BBRCliOptions{
227 options,
228 *bbsimIp,
229 *bbsimPort,
230 *bbsimApiPort,
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700231 *logFile,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700232 }
233
234 return bbrOptions
235}