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 | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 24 | "strings" |
Shrey Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 25 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 26 | "github.com/imdario/mergo" |
Shrey Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 27 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 28 | "gopkg.in/yaml.v2" |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 29 | ) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 30 | |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 31 | var tagAllocationValues = []string{ |
| 32 | "unknown", |
| 33 | "shared", |
| 34 | "unique", |
| 35 | } |
| 36 | |
| 37 | type TagAllocation int |
| 38 | |
| 39 | func (t TagAllocation) String() string { |
| 40 | return tagAllocationValues[t] |
| 41 | } |
| 42 | |
| 43 | func tagAllocationFromString(s string) (TagAllocation, error) { |
| 44 | for i, v := range tagAllocationValues { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 45 | if v == strings.TrimSpace(s) { |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 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) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 52 | return TagAllocation(0), fmt.Errorf("%s-is-not-a-valid-tag-allocation", s) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | const ( |
| 56 | _ TagAllocation = iota |
| 57 | TagAllocationShared |
| 58 | TagAllocationUnique |
| 59 | ) |
| 60 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 61 | type BBRCliOptions struct { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 62 | *GlobalConfig |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 63 | BBSimIp string |
| 64 | BBSimPort string |
| 65 | BBSimApiPort string |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 66 | LogFile string |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 69 | type GlobalConfig struct { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 70 | BBSim BBSimConfig |
| 71 | Olt OltConfig |
| 72 | BBR BBRConfig |
| 73 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 74 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 75 | type OltConfig struct { |
| 76 | Model string `yaml:"model"` |
| 77 | Vendor string `yaml:"vendor"` |
| 78 | HardwareVersion string `yaml:"hardware_version"` |
| 79 | FirmwareVersion string `yaml:"firmware_version"` |
| 80 | DeviceId string `yaml:"device_id"` |
| 81 | DeviceSerialNumber string `yaml:"device_serial_number"` |
| 82 | PonPorts uint32 `yaml:"pon_ports"` |
| 83 | NniPorts uint32 `yaml:"nni_ports"` |
| 84 | OnusPonPort uint32 `yaml:"onus_per_port"` |
| 85 | Technology string `yaml:"technology"` |
| 86 | ID int `yaml:"id"` |
| 87 | OltRebootDelay int `yaml:"reboot_delay"` |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 88 | PortStatsInterval int `yaml:"port_stats_interval"` |
Holger Hildebrandt | c10bab1 | 2021-04-27 09:23:48 +0000 | [diff] [blame] | 89 | OmciResponseRate uint8 `yaml:"omci_response_rate"` |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 90 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 91 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 92 | type BBSimConfig struct { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 93 | ConfigFile string |
| 94 | ServiceConfigFile string |
| 95 | DhcpRetry bool `yaml:"dhcp_retry"` |
| 96 | AuthRetry bool `yaml:"auth_retry"` |
| 97 | LogLevel string `yaml:"log_level"` |
| 98 | LogCaller bool `yaml:"log_caller"` |
| 99 | Delay int `yaml:"delay"` |
| 100 | CpuProfile *string `yaml:"cpu_profile"` |
| 101 | OpenOltAddress string `yaml:"openolt_address"` |
| 102 | ApiAddress string `yaml:"api_address"` |
| 103 | RestApiAddress string `yaml:"rest_api_address"` |
| 104 | LegacyApiAddress string `yaml:"legacy_api_address"` |
| 105 | LegacyRestApiAddress string `yaml:"legacy_rest_api_address"` |
| 106 | SadisRestAddress string `yaml:"sadis_rest_address"` |
| 107 | SadisServer bool `yaml:"sadis_server"` |
| 108 | KafkaAddress string `yaml:"kafka_address"` |
| 109 | Events bool `yaml:"enable_events"` |
| 110 | ControlledActivation string `yaml:"controlled_activation"` |
| 111 | EnablePerf bool `yaml:"enable_perf"` |
| 112 | KafkaEventTopic string `yaml:"kafka_event_topic"` |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 113 | DmiServerAddress string `yaml:"dmi_server_address"` |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 114 | } |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 115 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 116 | type BBRConfig struct { |
| 117 | Log string `yaml:"log"` |
| 118 | LogLevel string `yaml:"log_level"` |
| 119 | LogCaller bool `yaml:"log_caller"` |
| 120 | } |
| 121 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 122 | type ServiceYaml struct { |
| 123 | Name string |
| 124 | CTag int `yaml:"c_tag"` |
| 125 | STag int `yaml:"s_tag"` |
| 126 | NeedsEapol bool `yaml:"needs_eapol"` |
| 127 | NeedsDchp bool `yaml:"needs_dhcp"` |
| 128 | NeedsIgmp bool `yaml:"needs_igmp"` |
| 129 | CTagAllocation string `yaml:"c_tag_allocation"` |
| 130 | STagAllocation string `yaml:"s_tag_allocation"` |
| 131 | TechnologyProfileID int `yaml:"tp_id"` |
| 132 | UniTagMatch int `yaml:"uni_tag_match"` |
| 133 | ConfigureMacAddress bool `yaml:"configure_mac_address"` |
Matteo Scandolo | 8d28137 | 2020-09-03 16:23:37 -0700 | [diff] [blame] | 134 | UsPonCTagPriority uint8 `yaml:"us_pon_c_tag_priority"` |
| 135 | UsPonSTagPriority uint8 `yaml:"us_pon_s_tag_priority"` |
| 136 | DsPonCTagPriority uint8 `yaml:"ds_pon_c_tag_priority"` |
| 137 | DsPonSTagPriority uint8 `yaml:"ds_pon_s_tag_priority"` |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 138 | } |
| 139 | type YamlServiceConfig struct { |
| 140 | Workflow string |
| 141 | Services []ServiceYaml `yaml:"services,flow"` |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 144 | func (cfg *YamlServiceConfig) String() string { |
| 145 | str := fmt.Sprintf("[workflow: %s, Services: ", cfg.Workflow) |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 146 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 147 | for _, s := range cfg.Services { |
| 148 | str = fmt.Sprintf("%s[", str) |
| 149 | str = fmt.Sprintf("%sname=%s, c_tag=%d, s_tag=%d, ", |
| 150 | str, s.Name, s.CTag, s.STag) |
| 151 | str = fmt.Sprintf("%sc_tag_allocation=%s, s_tag_allocation=%s, ", |
| 152 | str, s.CTagAllocation, s.STagAllocation) |
| 153 | str = fmt.Sprintf("%sneeds_eapol=%t, needs_dhcp=%t, needs_igmp=%t", |
| 154 | str, s.NeedsEapol, s.NeedsDchp, s.NeedsIgmp) |
| 155 | str = fmt.Sprintf("%stp_id=%d, uni_tag_match=%d", |
| 156 | str, s.TechnologyProfileID, s.UniTagMatch) |
| 157 | str = fmt.Sprintf("%s]", str) |
| 158 | } |
| 159 | str = fmt.Sprintf("%s]", str) |
| 160 | return str |
| 161 | } |
| 162 | |
| 163 | var ( |
| 164 | Config *GlobalConfig |
| 165 | Services []ServiceYaml |
| 166 | ) |
| 167 | |
| 168 | // Load the BBSim configuration. This is a combination of CLI parameters and YAML files |
| 169 | // We proceed in this order: |
| 170 | // - Read CLI parameters |
| 171 | // - Using those we read the yaml files (config and services) |
| 172 | // - we merge the configuration (CLI has priority over yaml files) |
| 173 | func LoadConfig() { |
| 174 | |
| 175 | Config = getDefaultOps() |
| 176 | |
| 177 | cliConf := readCliParams() |
| 178 | |
| 179 | yamlConf, err := loadBBSimConf(cliConf.BBSim.ConfigFile) |
| 180 | |
| 181 | if err != nil { |
| 182 | log.WithFields(log.Fields{ |
| 183 | "file": cliConf.BBSim.ConfigFile, |
| 184 | "err": err, |
| 185 | }).Fatal("Can't read config file") |
| 186 | } |
| 187 | |
| 188 | // merging Yaml and Default Values |
| 189 | if err := mergo.Merge(Config, yamlConf, mergo.WithOverride); err != nil { |
| 190 | log.WithFields(log.Fields{ |
| 191 | "err": err, |
| 192 | }).Fatal("Can't merge YAML and Config") |
| 193 | } |
| 194 | |
| 195 | // merging CLI values on top of the yaml ones |
| 196 | if err := mergo.Merge(Config, cliConf, mergo.WithOverride); err != nil { |
| 197 | log.WithFields(log.Fields{ |
| 198 | "err": err, |
| 199 | }).Fatal("Can't merge CLI and Config") |
| 200 | } |
| 201 | |
| 202 | services, err := loadBBSimServices(Config.BBSim.ServiceConfigFile) |
| 203 | |
| 204 | if err != nil { |
| 205 | log.WithFields(log.Fields{ |
| 206 | "file": Config.BBSim.ServiceConfigFile, |
| 207 | "err": err, |
| 208 | }).Fatal("Can't read services file") |
| 209 | } |
| 210 | |
| 211 | Services = services |
| 212 | |
| 213 | } |
| 214 | |
| 215 | func readCliParams() *GlobalConfig { |
| 216 | |
Matteo Scandolo | c11074d | 2020-09-14 14:59:24 -0700 | [diff] [blame] | 217 | conf := getDefaultOps() |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 218 | |
| 219 | configFile := flag.String("config", conf.BBSim.ConfigFile, "Configuration file path") |
| 220 | servicesFile := flag.String("services", conf.BBSim.ServiceConfigFile, "Service Configuration file path") |
| 221 | |
| 222 | olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID") |
| 223 | nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated") |
| 224 | pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated") |
| 225 | onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated") |
Matteo Scandolo | 9356670 | 2020-09-30 15:19:27 -0700 | [diff] [blame] | 226 | oltRebootDelay := flag.Int("oltRebootDelay", conf.Olt.OltRebootDelay, "Time that BBSim should before restarting after a reboot") |
Holger Hildebrandt | c10bab1 | 2021-04-27 09:23:48 +0000 | [diff] [blame] | 227 | omci_response_rate := flag.Int("omci_response_rate", int(conf.Olt.OmciResponseRate), "Amount of OMCI messages to respond to") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 228 | |
| 229 | openolt_address := flag.String("openolt_address", conf.BBSim.OpenOltAddress, "IP address:port") |
| 230 | api_address := flag.String("api_address", conf.BBSim.ApiAddress, "IP address:port") |
| 231 | rest_api_address := flag.String("rest_api_address", conf.BBSim.RestApiAddress, "IP address:port") |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 232 | dmi_server_address := flag.String("dmi_server_address", conf.BBSim.DmiServerAddress, "IP address:port") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 233 | |
| 234 | profileCpu := flag.String("cpuprofile", "", "write cpu profile to file") |
| 235 | |
| 236 | logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)") |
| 237 | logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not") |
| 238 | |
| 239 | delay := flag.Int("delay", conf.BBSim.Delay, "The delay between ONU DISCOVERY batches in milliseconds (1 ONU per each PON PORT at a time") |
| 240 | |
| 241 | controlledActivation := flag.String("ca", conf.BBSim.ControlledActivation, "Set the mode for controlled activation of PON ports and ONUs") |
| 242 | enablePerf := flag.Bool("enableperf", conf.BBSim.EnablePerf, "Setting this flag will cause BBSim to not store data like traffic schedulers, flows of ONUs etc..") |
| 243 | enableEvents := flag.Bool("enableEvents", conf.BBSim.Events, "Enable sending BBSim events on configured kafka server") |
| 244 | kafkaAddress := flag.String("kafkaAddress", conf.BBSim.KafkaAddress, "IP:Port for kafka") |
| 245 | kafkaEventTopic := flag.String("kafkaEventTopic", conf.BBSim.KafkaEventTopic, "Ability to configure the topic on which BBSim publishes events on Kafka") |
| 246 | dhcpRetry := flag.Bool("dhcpRetry", conf.BBSim.DhcpRetry, "Set this flag if BBSim should retry DHCP upon failure until success") |
| 247 | authRetry := flag.Bool("authRetry", conf.BBSim.AuthRetry, "Set this flag if BBSim should retry EAPOL (Authentication) upon failure until success") |
Matteo Scandolo | 9356670 | 2020-09-30 15:19:27 -0700 | [diff] [blame] | 248 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 249 | flag.Parse() |
| 250 | |
| 251 | conf.Olt.ID = int(*olt_id) |
| 252 | conf.Olt.NniPorts = uint32(*nni) |
| 253 | conf.Olt.PonPorts = uint32(*pon) |
| 254 | conf.Olt.OnusPonPort = uint32(*onu) |
Matteo Scandolo | 9356670 | 2020-09-30 15:19:27 -0700 | [diff] [blame] | 255 | conf.Olt.OltRebootDelay = *oltRebootDelay |
Holger Hildebrandt | c10bab1 | 2021-04-27 09:23:48 +0000 | [diff] [blame] | 256 | conf.Olt.OmciResponseRate = uint8(*omci_response_rate) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 257 | conf.BBSim.ConfigFile = *configFile |
| 258 | conf.BBSim.ServiceConfigFile = *servicesFile |
| 259 | conf.BBSim.CpuProfile = profileCpu |
| 260 | conf.BBSim.LogLevel = *logLevel |
| 261 | conf.BBSim.LogCaller = *logCaller |
| 262 | conf.BBSim.Delay = *delay |
| 263 | conf.BBSim.ControlledActivation = *controlledActivation |
| 264 | conf.BBSim.EnablePerf = *enablePerf |
| 265 | conf.BBSim.Events = *enableEvents |
| 266 | conf.BBSim.KafkaAddress = *kafkaAddress |
| 267 | conf.BBSim.OpenOltAddress = *openolt_address |
| 268 | conf.BBSim.ApiAddress = *api_address |
| 269 | conf.BBSim.RestApiAddress = *rest_api_address |
| 270 | conf.BBSim.KafkaEventTopic = *kafkaEventTopic |
| 271 | conf.BBSim.AuthRetry = *authRetry |
| 272 | conf.BBSim.DhcpRetry = *dhcpRetry |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 273 | conf.BBSim.DmiServerAddress = *dmi_server_address |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 274 | |
| 275 | // update device id if not set |
| 276 | if conf.Olt.DeviceId == "" { |
| 277 | conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String() |
| 278 | } |
| 279 | |
Matteo Scandolo | c11074d | 2020-09-14 14:59:24 -0700 | [diff] [blame] | 280 | return conf |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | func getDefaultOps() *GlobalConfig { |
| 284 | |
| 285 | c := &GlobalConfig{ |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 286 | BBSimConfig{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 287 | ConfigFile: "configs/bbsim.yaml", |
| 288 | ServiceConfigFile: "configs/att-services.yaml", |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 289 | LogLevel: "debug", |
| 290 | LogCaller: false, |
| 291 | Delay: 200, |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 292 | OpenOltAddress: ":50060", |
| 293 | ApiAddress: ":50070", |
| 294 | RestApiAddress: ":50071", |
| 295 | LegacyApiAddress: ":50072", |
| 296 | LegacyRestApiAddress: ":50073", |
| 297 | SadisRestAddress: ":50074", |
| 298 | SadisServer: true, |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 299 | KafkaAddress: ":9092", |
| 300 | Events: false, |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 301 | ControlledActivation: "default", |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 302 | EnablePerf: false, |
Shrey Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [diff] [blame] | 303 | KafkaEventTopic: "", |
Shrey Baid | f8abccc | 2020-06-15 19:41:22 +0530 | [diff] [blame] | 304 | DhcpRetry: false, |
| 305 | AuthRetry: false, |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 306 | DmiServerAddress: ":50075", |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 307 | }, |
| 308 | OltConfig{ |
| 309 | Vendor: "BBSim", |
| 310 | Model: "asfvolt16", |
| 311 | HardwareVersion: "emulated", |
| 312 | FirmwareVersion: "", |
| 313 | DeviceSerialNumber: "BBSM00000001", |
| 314 | PonPorts: 1, |
| 315 | NniPorts: 1, |
| 316 | OnusPonPort: 1, |
| 317 | Technology: "XGS-PON", |
| 318 | ID: 0, |
Matteo Scandolo | 9356670 | 2020-09-30 15:19:27 -0700 | [diff] [blame] | 319 | OltRebootDelay: 60, |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 320 | PortStatsInterval: 20, |
Holger Hildebrandt | c10bab1 | 2021-04-27 09:23:48 +0000 | [diff] [blame] | 321 | OmciResponseRate: 10, |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 322 | }, |
| 323 | BBRConfig{ |
| 324 | LogLevel: "debug", |
| 325 | LogCaller: false, |
| 326 | }, |
| 327 | } |
| 328 | return c |
| 329 | } |
| 330 | |
| 331 | // LoadBBSimConf loads the BBSim configuration from a YAML file |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 332 | func loadBBSimConf(filename string) (*GlobalConfig, error) { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 333 | yamlConfig := getDefaultOps() |
| 334 | |
| 335 | yamlFile, err := ioutil.ReadFile(filename) |
| 336 | if err != nil { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 337 | log.WithFields(log.Fields{ |
| 338 | "err": err, |
| 339 | "filename": filename, |
| 340 | }).Error("Cannot load BBSim configuration file. Using defaults.") |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 341 | return yamlConfig, nil |
| 342 | } |
| 343 | |
| 344 | err = yaml.Unmarshal(yamlFile, yamlConfig) |
| 345 | if err != nil { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 346 | return nil, err |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | return yamlConfig, nil |
| 350 | } |
| 351 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 352 | // LoadBBSimServices parses a file describing the services that need to be created for each UNI |
| 353 | func loadBBSimServices(filename string) ([]ServiceYaml, error) { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 354 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 355 | yamlServiceCfg := YamlServiceConfig{} |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 356 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 357 | yamlFile, err := ioutil.ReadFile(filename) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 358 | if err != nil { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 359 | return nil, err |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 362 | err = yaml.Unmarshal([]byte(yamlFile), &yamlServiceCfg) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 363 | if err != nil { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 364 | return nil, err |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 367 | for _, service := range yamlServiceCfg.Services { |
| 368 | |
| 369 | if service.CTagAllocation == "" || service.STagAllocation == "" { |
| 370 | log.Fatal("c_tag_allocation and s_tag_allocation are mandatory fields") |
| 371 | } |
| 372 | |
| 373 | if _, err := tagAllocationFromString(string(service.CTagAllocation)); err != nil { |
| 374 | log.WithFields(log.Fields{ |
| 375 | "err": err, |
| 376 | }).Fatal("c_tag_allocation is not valid") |
| 377 | } |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 380 | log.WithFields(log.Fields{ |
| 381 | "services": yamlServiceCfg.String(), |
| 382 | }).Debug("BBSim services description correctly loaded") |
| 383 | return yamlServiceCfg.Services, nil |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 386 | // This is only used by BBR |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 387 | func GetBBROpts() BBRCliOptions { |
| 388 | |
| 389 | bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP") |
| 390 | bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port") |
| 391 | bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port") |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 392 | logFile := flag.String("logfile", "", "Log to a file") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 393 | |
Matteo Scandolo | c11074d | 2020-09-14 14:59:24 -0700 | [diff] [blame] | 394 | LoadConfig() |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 395 | |
| 396 | bbrOptions := BBRCliOptions{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 397 | Config, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 398 | *bbsimIp, |
| 399 | *bbsimPort, |
| 400 | *bbsimApiPort, |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 401 | *logFile, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | return bbrOptions |
| 405 | } |