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 | |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 37 | const ( |
| 38 | BP_FORMAT_MEF = "mef" |
| 39 | BP_FORMAT_IETF = "ietf" |
| 40 | ) |
| 41 | |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 42 | type TagAllocation int |
| 43 | |
| 44 | func (t TagAllocation) String() string { |
| 45 | return tagAllocationValues[t] |
| 46 | } |
| 47 | |
| 48 | func tagAllocationFromString(s string) (TagAllocation, error) { |
| 49 | for i, v := range tagAllocationValues { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 50 | if v == strings.TrimSpace(s) { |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 51 | return TagAllocation(i), nil |
| 52 | } |
| 53 | } |
| 54 | log.WithFields(log.Fields{ |
| 55 | "ValidValues": strings.Join(tagAllocationValues[1:], ", "), |
| 56 | }).Errorf("%s-is-not-a-valid-tag-allocation", s) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 57 | 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] | 58 | } |
| 59 | |
| 60 | const ( |
| 61 | _ TagAllocation = iota |
| 62 | TagAllocationShared |
| 63 | TagAllocationUnique |
| 64 | ) |
| 65 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 66 | type BBRCliOptions struct { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 67 | *GlobalConfig |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 68 | BBSimIp string |
| 69 | BBSimPort string |
| 70 | BBSimApiPort string |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 71 | LogFile string |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 74 | type GlobalConfig struct { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 75 | BBSim BBSimConfig |
| 76 | Olt OltConfig |
| 77 | BBR BBRConfig |
| 78 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 79 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 80 | type OltConfig struct { |
| 81 | Model string `yaml:"model"` |
| 82 | Vendor string `yaml:"vendor"` |
| 83 | HardwareVersion string `yaml:"hardware_version"` |
| 84 | FirmwareVersion string `yaml:"firmware_version"` |
| 85 | DeviceId string `yaml:"device_id"` |
| 86 | DeviceSerialNumber string `yaml:"device_serial_number"` |
| 87 | PonPorts uint32 `yaml:"pon_ports"` |
| 88 | NniPorts uint32 `yaml:"nni_ports"` |
Elia Battiston | 420c909 | 2022-02-02 12:17:54 +0100 | [diff] [blame] | 89 | NniSpeed uint32 `yaml:"nni_speed"` |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 90 | OnusPonPort uint32 `yaml:"onus_per_port"` |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 91 | ID int `yaml:"id"` |
| 92 | OltRebootDelay int `yaml:"reboot_delay"` |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 93 | PortStatsInterval int `yaml:"port_stats_interval"` |
Holger Hildebrandt | c10bab1 | 2021-04-27 09:23:48 +0000 | [diff] [blame] | 94 | OmciResponseRate uint8 `yaml:"omci_response_rate"` |
Mahir Gunyel | a1753ae | 2021-06-23 00:24:56 -0700 | [diff] [blame] | 95 | UniPorts uint32 `yaml:"uni_ports"` |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 96 | PotsPorts uint32 `yaml:"pots_ports"` |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 97 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 98 | |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 99 | type PonPortsConfig struct { |
| 100 | Number uint32 `yaml:"num_pon_ports"` |
| 101 | Ranges []PonRangeConfig `yaml:"ranges"` |
| 102 | } |
| 103 | |
| 104 | type IdRange struct { |
| 105 | StartId uint32 `yaml:"start"` |
| 106 | EndId uint32 `yaml:"end"` |
| 107 | } |
| 108 | |
| 109 | type PonTechnology int |
| 110 | |
| 111 | var ponTechnologyValues = []string{ |
| 112 | "GPON", "XGS-PON", |
| 113 | } |
| 114 | |
| 115 | func (t PonTechnology) String() string { |
| 116 | return ponTechnologyValues[t] |
| 117 | } |
| 118 | |
| 119 | const ( |
| 120 | GPON PonTechnology = iota |
| 121 | XGSPON |
| 122 | ) |
| 123 | |
| 124 | func PonTechnologyFromString(s string) (PonTechnology, error) { |
| 125 | for i, val := range ponTechnologyValues { |
| 126 | if val == s { |
| 127 | return PonTechnology(i), nil |
| 128 | } |
| 129 | } |
| 130 | log.WithFields(log.Fields{ |
| 131 | "ValidValues": strings.Join(ponTechnologyValues[:], ", "), |
| 132 | }).Errorf("%s-is-not-a-valid-pon-technology", s) |
| 133 | return -1, fmt.Errorf("%s-is-not-a-valid-pon-technology", s) |
| 134 | } |
| 135 | |
| 136 | //Constants for default allocation ranges |
| 137 | const ( |
| 138 | defaultOnuIdStart = 1 |
| 139 | defaultAllocIdStart = 1024 |
| 140 | defaultGemPortIdPerAllocId = 8 |
| 141 | defaultGemportIdStart = 1024 |
| 142 | ) |
| 143 | |
| 144 | type PonRangeConfig struct { |
| 145 | PonRange IdRange `yaml:"pon_id_range"` |
| 146 | Technology string `yaml:"tech"` |
| 147 | OnuRange IdRange `yaml:"onu_id_range"` |
| 148 | AllocIdRange IdRange `yaml:"alloc_id_range"` |
| 149 | GemportRange IdRange `yaml:"gemport_id_range"` |
| 150 | } |
| 151 | |
| 152 | func GetPonConfigById(id uint32) (*PonRangeConfig, error) { |
| 153 | if PonsConfig == nil { |
| 154 | return nil, fmt.Errorf("pons-config-nil") |
| 155 | } |
| 156 | |
| 157 | for _, r := range PonsConfig.Ranges { |
| 158 | if id >= r.PonRange.StartId && id <= r.PonRange.EndId { |
| 159 | return &r, nil |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return nil, fmt.Errorf("pon-config-for-id-%d-not-found", id) |
| 164 | } |
| 165 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 166 | type BBSimConfig struct { |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 167 | ConfigFile string |
| 168 | ServiceConfigFile string |
| 169 | PonsConfigFile string |
| 170 | DhcpRetry bool `yaml:"dhcp_retry"` |
| 171 | AuthRetry bool `yaml:"auth_retry"` |
| 172 | LogLevel string `yaml:"log_level"` |
| 173 | LogCaller bool `yaml:"log_caller"` |
| 174 | Delay int `yaml:"delay"` |
| 175 | CpuProfile *string `yaml:"cpu_profile"` |
| 176 | OpenOltAddress string `yaml:"openolt_address"` |
| 177 | ApiAddress string `yaml:"api_address"` |
| 178 | RestApiAddress string `yaml:"rest_api_address"` |
| 179 | LegacyApiAddress string `yaml:"legacy_api_address"` |
| 180 | LegacyRestApiAddress string `yaml:"legacy_rest_api_address"` |
| 181 | SadisRestAddress string `yaml:"sadis_rest_address"` |
| 182 | SadisServer bool `yaml:"sadis_server"` |
| 183 | KafkaAddress string `yaml:"kafka_address"` |
| 184 | Events bool `yaml:"enable_events"` |
| 185 | ControlledActivation string `yaml:"controlled_activation"` |
| 186 | EnablePerf bool `yaml:"enable_perf"` |
| 187 | KafkaEventTopic string `yaml:"kafka_event_topic"` |
| 188 | DmiServerAddress string `yaml:"dmi_server_address"` |
| 189 | BandwidthProfileFormat string `yaml:"bp_format"` |
| 190 | InjectOmciUnknownAttributes bool `yaml:"inject_omci_unknown_attributes"` |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 191 | } |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 192 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 193 | type BBRConfig struct { |
| 194 | Log string `yaml:"log"` |
| 195 | LogLevel string `yaml:"log_level"` |
| 196 | LogCaller bool `yaml:"log_caller"` |
| 197 | } |
| 198 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 199 | type ServiceYaml struct { |
| 200 | Name string |
| 201 | CTag int `yaml:"c_tag"` |
| 202 | STag int `yaml:"s_tag"` |
| 203 | NeedsEapol bool `yaml:"needs_eapol"` |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 204 | NeedsDhcp bool `yaml:"needs_dhcp"` |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 205 | NeedsIgmp bool `yaml:"needs_igmp"` |
Andrea Campanella | 2989045 | 2022-02-03 16:00:19 +0100 | [diff] [blame] | 206 | NeedsPPPoE bool `yaml:"needs_pppoe"` |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 207 | CTagAllocation string `yaml:"c_tag_allocation"` |
| 208 | STagAllocation string `yaml:"s_tag_allocation"` |
| 209 | TechnologyProfileID int `yaml:"tp_id"` |
| 210 | UniTagMatch int `yaml:"uni_tag_match"` |
| 211 | ConfigureMacAddress bool `yaml:"configure_mac_address"` |
Andrea Campanella | 2989045 | 2022-02-03 16:00:19 +0100 | [diff] [blame] | 212 | EnableMacLearning bool `yaml:"enable_mac_learning"` |
Matteo Scandolo | 8d28137 | 2020-09-03 16:23:37 -0700 | [diff] [blame] | 213 | UsPonCTagPriority uint8 `yaml:"us_pon_c_tag_priority"` |
| 214 | UsPonSTagPriority uint8 `yaml:"us_pon_s_tag_priority"` |
| 215 | DsPonCTagPriority uint8 `yaml:"ds_pon_c_tag_priority"` |
| 216 | DsPonSTagPriority uint8 `yaml:"ds_pon_s_tag_priority"` |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 217 | } |
| 218 | type YamlServiceConfig struct { |
| 219 | Workflow string |
| 220 | Services []ServiceYaml `yaml:"services,flow"` |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 223 | func (cfg *YamlServiceConfig) String() string { |
| 224 | str := fmt.Sprintf("[workflow: %s, Services: ", cfg.Workflow) |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 225 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 226 | for _, s := range cfg.Services { |
| 227 | str = fmt.Sprintf("%s[", str) |
| 228 | str = fmt.Sprintf("%sname=%s, c_tag=%d, s_tag=%d, ", |
| 229 | str, s.Name, s.CTag, s.STag) |
| 230 | str = fmt.Sprintf("%sc_tag_allocation=%s, s_tag_allocation=%s, ", |
| 231 | str, s.CTagAllocation, s.STagAllocation) |
| 232 | str = fmt.Sprintf("%sneeds_eapol=%t, needs_dhcp=%t, needs_igmp=%t", |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 233 | str, s.NeedsEapol, s.NeedsDhcp, s.NeedsIgmp) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 234 | str = fmt.Sprintf("%stp_id=%d, uni_tag_match=%d", |
| 235 | str, s.TechnologyProfileID, s.UniTagMatch) |
| 236 | str = fmt.Sprintf("%s]", str) |
| 237 | } |
| 238 | str = fmt.Sprintf("%s]", str) |
| 239 | return str |
| 240 | } |
| 241 | |
| 242 | var ( |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 243 | Config *GlobalConfig |
| 244 | Services []ServiceYaml |
| 245 | PonsConfig *PonPortsConfig |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 246 | ) |
| 247 | |
| 248 | // Load the BBSim configuration. This is a combination of CLI parameters and YAML files |
| 249 | // We proceed in this order: |
| 250 | // - Read CLI parameters |
| 251 | // - Using those we read the yaml files (config and services) |
| 252 | // - we merge the configuration (CLI has priority over yaml files) |
| 253 | func LoadConfig() { |
| 254 | |
| 255 | Config = getDefaultOps() |
| 256 | |
| 257 | cliConf := readCliParams() |
| 258 | |
| 259 | yamlConf, err := loadBBSimConf(cliConf.BBSim.ConfigFile) |
| 260 | |
| 261 | if err != nil { |
| 262 | log.WithFields(log.Fields{ |
| 263 | "file": cliConf.BBSim.ConfigFile, |
| 264 | "err": err, |
| 265 | }).Fatal("Can't read config file") |
| 266 | } |
| 267 | |
| 268 | // merging Yaml and Default Values |
| 269 | if err := mergo.Merge(Config, yamlConf, mergo.WithOverride); err != nil { |
| 270 | log.WithFields(log.Fields{ |
| 271 | "err": err, |
| 272 | }).Fatal("Can't merge YAML and Config") |
| 273 | } |
| 274 | |
| 275 | // merging CLI values on top of the yaml ones |
| 276 | if err := mergo.Merge(Config, cliConf, mergo.WithOverride); err != nil { |
| 277 | log.WithFields(log.Fields{ |
| 278 | "err": err, |
| 279 | }).Fatal("Can't merge CLI and Config") |
| 280 | } |
| 281 | |
| 282 | services, err := loadBBSimServices(Config.BBSim.ServiceConfigFile) |
| 283 | |
| 284 | if err != nil { |
| 285 | log.WithFields(log.Fields{ |
| 286 | "file": Config.BBSim.ServiceConfigFile, |
| 287 | "err": err, |
| 288 | }).Fatal("Can't read services file") |
| 289 | } |
| 290 | |
| 291 | Services = services |
| 292 | |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 293 | //A blank filename means we should fall back to bbsim defaults |
| 294 | if Config.BBSim.PonsConfigFile == "" { |
| 295 | PonsConfig, err = getDefaultPonsConfig() |
| 296 | if err != nil { |
| 297 | log.WithFields(log.Fields{ |
| 298 | "err": err, |
| 299 | }).Fatal("Can't load Pon interfaces defaults.") |
| 300 | } |
| 301 | } else { |
| 302 | PonsConfig, err = loadBBSimPons(Config.BBSim.PonsConfigFile) |
| 303 | |
| 304 | if err != nil { |
| 305 | log.WithFields(log.Fields{ |
| 306 | "file": Config.BBSim.PonsConfigFile, |
| 307 | "err": err, |
| 308 | }).Fatal("Can't read services file") |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if err := validatePonsConfig(PonsConfig); err != nil { |
| 313 | log.WithFields(log.Fields{ |
| 314 | "file": Config.BBSim.PonsConfigFile, |
| 315 | "err": err, |
| 316 | }).Fatal("Invalid Pon interfaces configuration") |
| 317 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | func readCliParams() *GlobalConfig { |
| 321 | |
Matteo Scandolo | c11074d | 2020-09-14 14:59:24 -0700 | [diff] [blame] | 322 | conf := getDefaultOps() |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 323 | |
| 324 | configFile := flag.String("config", conf.BBSim.ConfigFile, "Configuration file path") |
| 325 | servicesFile := flag.String("services", conf.BBSim.ServiceConfigFile, "Service Configuration file path") |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 326 | ponsFile := flag.String("pon_port_config_file", conf.BBSim.PonsConfigFile, "Pon Interfaces Configuration file path") |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 327 | sadisBpFormat := flag.String("bp_format", conf.BBSim.BandwidthProfileFormat, "Bandwidth profile format, 'mef' or 'ietf'") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 328 | |
| 329 | olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID") |
| 330 | nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated") |
Elia Battiston | 420c909 | 2022-02-02 12:17:54 +0100 | [diff] [blame] | 331 | nni_speed := flag.Uint("nni_speed", uint(conf.Olt.NniSpeed), "Reported speed of the NNI ports in Mbps") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 332 | pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated") |
| 333 | onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated") |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 334 | uni := flag.Int("uni", int(conf.Olt.UniPorts), "Number of Ethernet UNI Ports per ONU device to be emulated") |
| 335 | pots := flag.Int("pots", int(conf.Olt.PotsPorts), "Number of POTS UNI Ports per ONU device to be emulated") |
Mahir Gunyel | a1753ae | 2021-06-23 00:24:56 -0700 | [diff] [blame] | 336 | |
Matteo Scandolo | 9356670 | 2020-09-30 15:19:27 -0700 | [diff] [blame] | 337 | 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] | 338 | 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] | 339 | |
| 340 | openolt_address := flag.String("openolt_address", conf.BBSim.OpenOltAddress, "IP address:port") |
| 341 | api_address := flag.String("api_address", conf.BBSim.ApiAddress, "IP address:port") |
| 342 | 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] | 343 | 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] | 344 | |
| 345 | profileCpu := flag.String("cpuprofile", "", "write cpu profile to file") |
| 346 | |
| 347 | logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)") |
| 348 | logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not") |
| 349 | |
| 350 | delay := flag.Int("delay", conf.BBSim.Delay, "The delay between ONU DISCOVERY batches in milliseconds (1 ONU per each PON PORT at a time") |
| 351 | |
| 352 | controlledActivation := flag.String("ca", conf.BBSim.ControlledActivation, "Set the mode for controlled activation of PON ports and ONUs") |
| 353 | enablePerf := flag.Bool("enableperf", conf.BBSim.EnablePerf, "Setting this flag will cause BBSim to not store data like traffic schedulers, flows of ONUs etc..") |
| 354 | enableEvents := flag.Bool("enableEvents", conf.BBSim.Events, "Enable sending BBSim events on configured kafka server") |
| 355 | kafkaAddress := flag.String("kafkaAddress", conf.BBSim.KafkaAddress, "IP:Port for kafka") |
| 356 | kafkaEventTopic := flag.String("kafkaEventTopic", conf.BBSim.KafkaEventTopic, "Ability to configure the topic on which BBSim publishes events on Kafka") |
| 357 | dhcpRetry := flag.Bool("dhcpRetry", conf.BBSim.DhcpRetry, "Set this flag if BBSim should retry DHCP upon failure until success") |
| 358 | authRetry := flag.Bool("authRetry", conf.BBSim.AuthRetry, "Set this flag if BBSim should retry EAPOL (Authentication) upon failure until success") |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 359 | injectOmciUnknownAttributes := flag.Bool("injectOmciUnknownAttributes", conf.BBSim.InjectOmciUnknownAttributes, "Generate a MibDB packet with Unknown Attributes") |
Matteo Scandolo | 9356670 | 2020-09-30 15:19:27 -0700 | [diff] [blame] | 360 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 361 | flag.Parse() |
| 362 | |
| 363 | conf.Olt.ID = int(*olt_id) |
| 364 | conf.Olt.NniPorts = uint32(*nni) |
Elia Battiston | 420c909 | 2022-02-02 12:17:54 +0100 | [diff] [blame] | 365 | conf.Olt.NniSpeed = uint32(*nni_speed) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 366 | conf.Olt.PonPorts = uint32(*pon) |
Mahir Gunyel | a1753ae | 2021-06-23 00:24:56 -0700 | [diff] [blame] | 367 | conf.Olt.UniPorts = uint32(*uni) |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 368 | conf.Olt.PotsPorts = uint32(*pots) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 369 | conf.Olt.OnusPonPort = uint32(*onu) |
Matteo Scandolo | 9356670 | 2020-09-30 15:19:27 -0700 | [diff] [blame] | 370 | conf.Olt.OltRebootDelay = *oltRebootDelay |
Holger Hildebrandt | c10bab1 | 2021-04-27 09:23:48 +0000 | [diff] [blame] | 371 | conf.Olt.OmciResponseRate = uint8(*omci_response_rate) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 372 | conf.BBSim.ConfigFile = *configFile |
| 373 | conf.BBSim.ServiceConfigFile = *servicesFile |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 374 | conf.BBSim.PonsConfigFile = *ponsFile |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 375 | conf.BBSim.CpuProfile = profileCpu |
| 376 | conf.BBSim.LogLevel = *logLevel |
| 377 | conf.BBSim.LogCaller = *logCaller |
| 378 | conf.BBSim.Delay = *delay |
| 379 | conf.BBSim.ControlledActivation = *controlledActivation |
| 380 | conf.BBSim.EnablePerf = *enablePerf |
| 381 | conf.BBSim.Events = *enableEvents |
| 382 | conf.BBSim.KafkaAddress = *kafkaAddress |
| 383 | conf.BBSim.OpenOltAddress = *openolt_address |
| 384 | conf.BBSim.ApiAddress = *api_address |
| 385 | conf.BBSim.RestApiAddress = *rest_api_address |
| 386 | conf.BBSim.KafkaEventTopic = *kafkaEventTopic |
| 387 | conf.BBSim.AuthRetry = *authRetry |
| 388 | conf.BBSim.DhcpRetry = *dhcpRetry |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 389 | conf.BBSim.DmiServerAddress = *dmi_server_address |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 390 | conf.BBSim.InjectOmciUnknownAttributes = *injectOmciUnknownAttributes |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 391 | |
| 392 | // update device id if not set |
| 393 | if conf.Olt.DeviceId == "" { |
| 394 | conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String() |
| 395 | } |
| 396 | |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 397 | // check that the BP format is valid |
| 398 | if (*sadisBpFormat != BP_FORMAT_MEF) && (*sadisBpFormat != BP_FORMAT_IETF) { |
| 399 | log.Fatalf("Invalid parameter 'bp_format', supported values are %s and %s, you provided %s", BP_FORMAT_MEF, BP_FORMAT_IETF, *sadisBpFormat) |
| 400 | } |
| 401 | conf.BBSim.BandwidthProfileFormat = *sadisBpFormat |
| 402 | |
Matteo Scandolo | c11074d | 2020-09-14 14:59:24 -0700 | [diff] [blame] | 403 | return conf |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | func getDefaultOps() *GlobalConfig { |
| 407 | |
| 408 | c := &GlobalConfig{ |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 409 | BBSimConfig{ |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 410 | ConfigFile: "configs/bbsim.yaml", |
| 411 | ServiceConfigFile: "configs/att-services.yaml", |
| 412 | // PonsConfigFile is left intentionally blank here |
| 413 | // to use the default values computed at runtime depending |
| 414 | // on the loaded Services |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 415 | PonsConfigFile: "", |
| 416 | LogLevel: "debug", |
| 417 | LogCaller: false, |
| 418 | Delay: 200, |
| 419 | OpenOltAddress: ":50060", |
| 420 | ApiAddress: ":50070", |
| 421 | RestApiAddress: ":50071", |
| 422 | LegacyApiAddress: ":50072", |
| 423 | LegacyRestApiAddress: ":50073", |
| 424 | SadisRestAddress: ":50074", |
| 425 | SadisServer: true, |
| 426 | KafkaAddress: ":9092", |
| 427 | Events: false, |
| 428 | ControlledActivation: "default", |
| 429 | EnablePerf: false, |
| 430 | KafkaEventTopic: "", |
| 431 | DhcpRetry: false, |
| 432 | AuthRetry: false, |
| 433 | DmiServerAddress: ":50075", |
| 434 | BandwidthProfileFormat: BP_FORMAT_MEF, |
| 435 | InjectOmciUnknownAttributes: false, |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 436 | }, |
| 437 | OltConfig{ |
| 438 | Vendor: "BBSim", |
| 439 | Model: "asfvolt16", |
| 440 | HardwareVersion: "emulated", |
| 441 | FirmwareVersion: "", |
| 442 | DeviceSerialNumber: "BBSM00000001", |
| 443 | PonPorts: 1, |
| 444 | NniPorts: 1, |
Elia Battiston | 420c909 | 2022-02-02 12:17:54 +0100 | [diff] [blame] | 445 | NniSpeed: 10000, //Mbps |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 446 | OnusPonPort: 1, |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 447 | ID: 0, |
Matteo Scandolo | 9356670 | 2020-09-30 15:19:27 -0700 | [diff] [blame] | 448 | OltRebootDelay: 60, |
Pragya Arya | 996a089 | 2020-03-09 21:47:52 +0530 | [diff] [blame] | 449 | PortStatsInterval: 20, |
Holger Hildebrandt | c10bab1 | 2021-04-27 09:23:48 +0000 | [diff] [blame] | 450 | OmciResponseRate: 10, |
Mahir Gunyel | a1753ae | 2021-06-23 00:24:56 -0700 | [diff] [blame] | 451 | UniPorts: 4, |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 452 | PotsPorts: 0, |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 453 | }, |
| 454 | BBRConfig{ |
| 455 | LogLevel: "debug", |
| 456 | LogCaller: false, |
| 457 | }, |
| 458 | } |
| 459 | return c |
| 460 | } |
| 461 | |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 462 | func getDefaultPonsConfig() (*PonPortsConfig, error) { |
| 463 | |
| 464 | if Config == nil { |
| 465 | return nil, fmt.Errorf("Config is nil") |
| 466 | } |
| 467 | if Services == nil { |
| 468 | return nil, fmt.Errorf("Services is nil") |
| 469 | } |
| 470 | |
| 471 | //The default should replicate the old way bbsim used to compute resource ranges based on the configuration |
| 472 | // 1 allocId per Service * UNI |
| 473 | allocIdPerOnu := uint32(Config.Olt.UniPorts * uint32(len(Services))) |
| 474 | return &PonPortsConfig{ |
| 475 | Number: Config.Olt.PonPorts, |
| 476 | Ranges: []PonRangeConfig{ |
| 477 | { |
| 478 | PonRange: IdRange{0, Config.Olt.PonPorts - 1}, |
| 479 | Technology: XGSPON.String(), |
| 480 | // we need one ONU ID available per ONU, but the smaller the range the smaller the pool created in the openolt adapter |
| 481 | OnuRange: IdRange{defaultOnuIdStart, defaultOnuIdStart + (Config.Olt.OnusPonPort - 1)}, |
| 482 | // 1 allocId per Service * UNI * ONU |
| 483 | AllocIdRange: IdRange{defaultAllocIdStart, defaultAllocIdStart + (Config.Olt.OnusPonPort * allocIdPerOnu)}, |
| 484 | // up to 8 gemport-id per tcont/alloc-id |
| 485 | GemportRange: IdRange{defaultGemportIdStart, defaultGemportIdStart + Config.Olt.OnusPonPort*allocIdPerOnu*defaultGemPortIdPerAllocId}, |
| 486 | }, |
| 487 | }, |
| 488 | }, nil |
| 489 | } |
| 490 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 491 | // LoadBBSimConf loads the BBSim configuration from a YAML file |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 492 | func loadBBSimConf(filename string) (*GlobalConfig, error) { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 493 | yamlConfig := getDefaultOps() |
| 494 | |
| 495 | yamlFile, err := ioutil.ReadFile(filename) |
| 496 | if err != nil { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 497 | log.WithFields(log.Fields{ |
| 498 | "err": err, |
| 499 | "filename": filename, |
| 500 | }).Error("Cannot load BBSim configuration file. Using defaults.") |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 501 | return yamlConfig, nil |
| 502 | } |
| 503 | |
| 504 | err = yaml.Unmarshal(yamlFile, yamlConfig) |
| 505 | if err != nil { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 506 | return nil, err |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | return yamlConfig, nil |
| 510 | } |
| 511 | |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 512 | // loadBBSimPons loads the configuration of PON interfaces from a YAML file |
| 513 | func loadBBSimPons(filename string) (*PonPortsConfig, error) { |
| 514 | yamlPonsConfig, err := getDefaultPonsConfig() |
| 515 | if err != nil { |
| 516 | log.WithFields(log.Fields{ |
| 517 | "err": err, |
| 518 | }).Error("Can't load Pon interfaces defaults.") |
| 519 | return nil, err |
| 520 | } |
| 521 | |
| 522 | yamlFile, err := ioutil.ReadFile(filename) |
| 523 | if err != nil { |
| 524 | log.WithFields(log.Fields{ |
| 525 | "err": err, |
| 526 | "filename": filename, |
| 527 | }).Error("Cannot load Pon interfaces configuration file. Using defaults.") |
| 528 | return yamlPonsConfig, nil |
| 529 | } |
| 530 | |
| 531 | err = yaml.Unmarshal(yamlFile, yamlPonsConfig) |
| 532 | if err != nil { |
| 533 | return nil, err |
| 534 | } |
| 535 | |
| 536 | return yamlPonsConfig, nil |
| 537 | } |
| 538 | |
| 539 | // validatePonsConfig checks if the configuration to use for the definition of Pon interfaces is valid |
| 540 | func validatePonsConfig(pons *PonPortsConfig) error { |
| 541 | |
| 542 | if pons.Number == 0 { |
| 543 | return fmt.Errorf("no-pon-ports") |
| 544 | } |
| 545 | |
| 546 | definedPorts := make([]int, pons.Number) |
| 547 | |
| 548 | for rIndex, resRange := range pons.Ranges { |
| 549 | if _, err := PonTechnologyFromString(resRange.Technology); err != nil { |
| 550 | return err |
| 551 | } |
| 552 | |
| 553 | if resRange.PonRange.EndId < resRange.PonRange.StartId { |
| 554 | return fmt.Errorf("invalid-pon-ports-limits-in-range-%d", rIndex) |
| 555 | } |
| 556 | |
| 557 | //Keep track of the defined pons |
| 558 | for p := resRange.PonRange.StartId; p <= resRange.PonRange.EndId; p++ { |
| 559 | if p > uint32(len(definedPorts)-1) { |
| 560 | return fmt.Errorf("pon-port-%d-in-range-%d-but-max-is-%d", p, rIndex, pons.Number-1) |
| 561 | } |
| 562 | definedPorts[p]++ |
| 563 | |
| 564 | if definedPorts[p] > 1 { |
| 565 | return fmt.Errorf("pon-port-%d-has-duplicate-definition-in-range-%d", p, rIndex) |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if resRange.OnuRange.EndId < resRange.OnuRange.StartId { |
| 570 | return fmt.Errorf("invalid-onus-limits-in-range-%d", rIndex) |
| 571 | } |
| 572 | if resRange.AllocIdRange.EndId < resRange.AllocIdRange.StartId { |
| 573 | return fmt.Errorf("invalid-allocid-limits-in-range-%d", rIndex) |
| 574 | } |
| 575 | if resRange.GemportRange.EndId < resRange.GemportRange.StartId { |
| 576 | return fmt.Errorf("invalid-gemport-limits-in-range-%d", rIndex) |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | //Check if the ranges define all the pons |
| 581 | for i, num := range definedPorts { |
| 582 | if num < 1 { |
| 583 | return fmt.Errorf("pon-port-%d-is-not-defined-in-ranges", i) |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | return nil |
| 588 | } |
| 589 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 590 | // LoadBBSimServices parses a file describing the services that need to be created for each UNI |
| 591 | func loadBBSimServices(filename string) ([]ServiceYaml, error) { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 592 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 593 | yamlServiceCfg := YamlServiceConfig{} |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 594 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 595 | yamlFile, err := ioutil.ReadFile(filename) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 596 | if err != nil { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 597 | return nil, err |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 600 | err = yaml.Unmarshal([]byte(yamlFile), &yamlServiceCfg) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 601 | if err != nil { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 602 | return nil, err |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 605 | for _, service := range yamlServiceCfg.Services { |
| 606 | |
| 607 | if service.CTagAllocation == "" || service.STagAllocation == "" { |
| 608 | log.Fatal("c_tag_allocation and s_tag_allocation are mandatory fields") |
| 609 | } |
| 610 | |
| 611 | if _, err := tagAllocationFromString(string(service.CTagAllocation)); err != nil { |
| 612 | log.WithFields(log.Fields{ |
| 613 | "err": err, |
| 614 | }).Fatal("c_tag_allocation is not valid") |
| 615 | } |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 618 | log.WithFields(log.Fields{ |
| 619 | "services": yamlServiceCfg.String(), |
| 620 | }).Debug("BBSim services description correctly loaded") |
| 621 | return yamlServiceCfg.Services, nil |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 624 | // This is only used by BBR |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 625 | func GetBBROpts() BBRCliOptions { |
| 626 | |
| 627 | bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP") |
| 628 | bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port") |
| 629 | bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port") |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 630 | logFile := flag.String("logfile", "", "Log to a file") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 631 | |
Matteo Scandolo | c11074d | 2020-09-14 14:59:24 -0700 | [diff] [blame] | 632 | LoadConfig() |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 633 | |
| 634 | bbrOptions := BBRCliOptions{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 635 | Config, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 636 | *bbsimIp, |
| 637 | *bbsimPort, |
| 638 | *bbsimApiPort, |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 639 | *logFile, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | return bbrOptions |
| 643 | } |