Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package common |
| 18 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 19 | import ( |
| 20 | "flag" |
| 21 | "fmt" |
| 22 | "io/ioutil" |
| 23 | "net" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 24 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 25 | "gopkg.in/yaml.v2" |
| 26 | ) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 27 | |
| 28 | type BBRCliOptions struct { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 29 | *BBSimYamlConfig |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 30 | BBSimIp string |
| 31 | BBSimPort string |
| 32 | BBSimApiPort string |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 33 | LogFile string |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 36 | type BBSimYamlConfig struct { |
| 37 | BBSim BBSimConfig |
| 38 | Olt OltConfig |
| 39 | BBR BBRConfig |
| 40 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 41 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 42 | type 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"` |
| 55 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 56 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 57 | type BBSimConfig struct { |
| 58 | EnableDhcp bool `yaml:"enable_dhcp"` |
| 59 | EnableAuth bool `yaml:"enable_auth"` |
| 60 | LogLevel string `yaml:"log_level"` |
| 61 | LogCaller bool `yaml:"log_caller"` |
| 62 | Delay int `yaml:"delay"` |
| 63 | CpuProfile *string `yaml:"cpu_profile"` |
| 64 | CTagInit int `yaml:"c_tag"` |
| 65 | STag int `yaml:"s_tag"` |
| 66 | OpenOltAddress string `yaml:"openolt_address"` |
| 67 | ApiAddress string `yaml:"api_address"` |
| 68 | RestApiAddress string `yaml:"rest_api_address"` |
| 69 | LegacyApiAddress string `yaml:"legacy_api_address"` |
| 70 | LegacyRestApiAddress string `yaml:"legacy_rest_api_address"` |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 71 | SadisRestAddress string `yaml:"sadis_rest_address"` |
| 72 | SadisServer bool `yaml:"sadis_server"` |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 73 | KafkaAddress string `yaml:"kafka_address"` |
| 74 | Events bool `yaml:"enable_events"` |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 75 | ControlledActivation string `yaml:"controlled_activation"` |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 76 | EnablePerf bool `yaml:"enable_perf"` |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 77 | } |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 78 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 79 | type BBRConfig struct { |
| 80 | Log string `yaml:"log"` |
| 81 | LogLevel string `yaml:"log_level"` |
| 82 | LogCaller bool `yaml:"log_caller"` |
| 83 | } |
| 84 | |
| 85 | var Options *BBSimYamlConfig |
| 86 | |
| 87 | func init() { |
| 88 | // load settings from config file first |
| 89 | Options, _ = LoadBBSimConf("configs/bbsim.yaml") |
| 90 | } |
| 91 | |
| 92 | func getDefaultOps() *BBSimYamlConfig { |
| 93 | |
| 94 | c := &BBSimYamlConfig{ |
| 95 | BBSimConfig{ |
| 96 | STag: 900, |
| 97 | CTagInit: 900, |
| 98 | EnableDhcp: false, |
| 99 | EnableAuth: false, |
| 100 | LogLevel: "debug", |
| 101 | LogCaller: false, |
| 102 | Delay: 200, |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 103 | OpenOltAddress: ":50060", |
| 104 | ApiAddress: ":50070", |
| 105 | RestApiAddress: ":50071", |
| 106 | LegacyApiAddress: ":50072", |
| 107 | LegacyRestApiAddress: ":50073", |
| 108 | SadisRestAddress: ":50074", |
| 109 | SadisServer: true, |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 110 | KafkaAddress: ":9092", |
| 111 | Events: false, |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 112 | ControlledActivation: "default", |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 113 | EnablePerf: false, |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 114 | }, |
| 115 | OltConfig{ |
| 116 | Vendor: "BBSim", |
| 117 | Model: "asfvolt16", |
| 118 | HardwareVersion: "emulated", |
| 119 | FirmwareVersion: "", |
| 120 | DeviceSerialNumber: "BBSM00000001", |
| 121 | PonPorts: 1, |
| 122 | NniPorts: 1, |
| 123 | OnusPonPort: 1, |
| 124 | Technology: "XGS-PON", |
| 125 | ID: 0, |
| 126 | OltRebootDelay: 10, |
| 127 | }, |
| 128 | BBRConfig{ |
| 129 | LogLevel: "debug", |
| 130 | LogCaller: false, |
| 131 | }, |
| 132 | } |
| 133 | return c |
| 134 | } |
| 135 | |
| 136 | // LoadBBSimConf loads the BBSim configuration from a YAML file |
| 137 | func LoadBBSimConf(filename string) (*BBSimYamlConfig, error) { |
| 138 | yamlConfig := getDefaultOps() |
| 139 | |
| 140 | yamlFile, err := ioutil.ReadFile(filename) |
| 141 | if err != nil { |
| 142 | fmt.Printf("Cannot load BBSim configuration file: %s. Using defaults.\n", err) |
| 143 | return yamlConfig, nil |
| 144 | } |
| 145 | |
| 146 | err = yaml.Unmarshal(yamlFile, yamlConfig) |
| 147 | if err != nil { |
| 148 | fmt.Printf("Error parsing YAML file: %s\n", err) |
| 149 | } |
| 150 | |
| 151 | return yamlConfig, nil |
| 152 | } |
| 153 | |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 154 | // GetBBSimOpts loads the BBSim configuration file and over-rides options with corresponding CLI flags if set |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 155 | func GetBBSimOpts() *BBSimYamlConfig { |
| 156 | conf := Options |
| 157 | |
| 158 | olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID") |
| 159 | nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated") |
| 160 | pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated") |
| 161 | onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated") |
| 162 | |
| 163 | s_tag := flag.Int("s_tag", conf.BBSim.STag, "S-Tag initial value") |
| 164 | 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)") |
| 165 | |
| 166 | auth := flag.Bool("auth", conf.BBSim.EnableAuth, "Set this flag if you want authentication to start automatically") |
| 167 | dhcp := flag.Bool("dhcp", conf.BBSim.EnableDhcp, "Set this flag if you want DHCP to start automatically") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 168 | |
| 169 | profileCpu := flag.String("cpuprofile", "", "write cpu profile to file") |
| 170 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 171 | logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)") |
| 172 | logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 173 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 174 | 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 Scandolo | e33447a | 2019-10-31 12:38:23 -0700 | [diff] [blame] | 175 | |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 176 | controlledActivation := flag.String("ca", conf.BBSim.ControlledActivation, "Set the mode for controlled activation of PON ports and ONUs") |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 177 | 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 Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 178 | enableEvents := flag.Bool("enableEvents", conf.BBSim.Events, "Enable sending BBSim events on configured kafka server") |
| 179 | kafkaAddress := flag.String("kafkaAddress", conf.BBSim.KafkaAddress, "IP:Port for kafka") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 180 | flag.Parse() |
| 181 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 182 | conf.Olt.ID = int(*olt_id) |
| 183 | conf.Olt.NniPorts = uint32(*nni) |
| 184 | conf.Olt.PonPorts = uint32(*pon) |
| 185 | conf.Olt.OnusPonPort = uint32(*onu) |
| 186 | conf.BBSim.STag = int(*s_tag) |
| 187 | conf.BBSim.CTagInit = int(*c_tag_init) |
| 188 | conf.BBSim.CpuProfile = profileCpu |
| 189 | conf.BBSim.LogLevel = *logLevel |
| 190 | conf.BBSim.LogCaller = *logCaller |
| 191 | conf.BBSim.EnableAuth = *auth |
| 192 | conf.BBSim.EnableDhcp = *dhcp |
| 193 | conf.BBSim.Delay = *delay |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 194 | conf.BBSim.ControlledActivation = *controlledActivation |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 195 | conf.BBSim.EnablePerf = *enablePerf |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 196 | conf.BBSim.Events = *enableEvents |
| 197 | conf.BBSim.KafkaAddress = *kafkaAddress |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 198 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 199 | // update device id if not set |
| 200 | if conf.Olt.DeviceId == "" { |
| 201 | conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String() |
| 202 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 203 | |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 204 | Options = conf |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 205 | return conf |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | func GetBBROpts() BBRCliOptions { |
| 209 | |
| 210 | bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP") |
| 211 | bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port") |
| 212 | bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port") |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 213 | logFile := flag.String("logfile", "", "Log to a file") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 214 | |
| 215 | options := GetBBSimOpts() |
| 216 | |
| 217 | bbrOptions := BBRCliOptions{ |
| 218 | options, |
| 219 | *bbsimIp, |
| 220 | *bbsimPort, |
| 221 | *bbsimApiPort, |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 222 | *logFile, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return bbrOptions |
| 226 | } |