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"` |
| 71 | } |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 72 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 73 | type BBRConfig struct { |
| 74 | Log string `yaml:"log"` |
| 75 | LogLevel string `yaml:"log_level"` |
| 76 | LogCaller bool `yaml:"log_caller"` |
| 77 | } |
| 78 | |
| 79 | var Options *BBSimYamlConfig |
| 80 | |
| 81 | func init() { |
| 82 | // load settings from config file first |
| 83 | Options, _ = LoadBBSimConf("configs/bbsim.yaml") |
| 84 | } |
| 85 | |
| 86 | func getDefaultOps() *BBSimYamlConfig { |
| 87 | |
| 88 | c := &BBSimYamlConfig{ |
| 89 | BBSimConfig{ |
| 90 | STag: 900, |
| 91 | CTagInit: 900, |
| 92 | EnableDhcp: false, |
| 93 | EnableAuth: false, |
| 94 | LogLevel: "debug", |
| 95 | LogCaller: false, |
| 96 | Delay: 200, |
| 97 | OpenOltAddress: "0.0.0.0:50060", |
| 98 | ApiAddress: "0.0.0.0:50070", |
| 99 | RestApiAddress: "0.0.0.0:50071", |
| 100 | LegacyApiAddress: "0.0.0.0:50072", |
| 101 | LegacyRestApiAddress: "0.0.0.0:50073", |
| 102 | }, |
| 103 | OltConfig{ |
| 104 | Vendor: "BBSim", |
| 105 | Model: "asfvolt16", |
| 106 | HardwareVersion: "emulated", |
| 107 | FirmwareVersion: "", |
| 108 | DeviceSerialNumber: "BBSM00000001", |
| 109 | PonPorts: 1, |
| 110 | NniPorts: 1, |
| 111 | OnusPonPort: 1, |
| 112 | Technology: "XGS-PON", |
| 113 | ID: 0, |
| 114 | OltRebootDelay: 10, |
| 115 | }, |
| 116 | BBRConfig{ |
| 117 | LogLevel: "debug", |
| 118 | LogCaller: false, |
| 119 | }, |
| 120 | } |
| 121 | return c |
| 122 | } |
| 123 | |
| 124 | // LoadBBSimConf loads the BBSim configuration from a YAML file |
| 125 | func LoadBBSimConf(filename string) (*BBSimYamlConfig, error) { |
| 126 | yamlConfig := getDefaultOps() |
| 127 | |
| 128 | yamlFile, err := ioutil.ReadFile(filename) |
| 129 | if err != nil { |
| 130 | fmt.Printf("Cannot load BBSim configuration file: %s. Using defaults.\n", err) |
| 131 | return yamlConfig, nil |
| 132 | } |
| 133 | |
| 134 | err = yaml.Unmarshal(yamlFile, yamlConfig) |
| 135 | if err != nil { |
| 136 | fmt.Printf("Error parsing YAML file: %s\n", err) |
| 137 | } |
| 138 | |
| 139 | return yamlConfig, nil |
| 140 | } |
| 141 | |
| 142 | // GetBBSimOpts loads the BBSim configuration file and overides options with corresponding CLI flags if set |
| 143 | func GetBBSimOpts() *BBSimYamlConfig { |
| 144 | conf := Options |
| 145 | |
| 146 | olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID") |
| 147 | nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated") |
| 148 | pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated") |
| 149 | onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated") |
| 150 | |
| 151 | s_tag := flag.Int("s_tag", conf.BBSim.STag, "S-Tag initial value") |
| 152 | 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)") |
| 153 | |
| 154 | auth := flag.Bool("auth", conf.BBSim.EnableAuth, "Set this flag if you want authentication to start automatically") |
| 155 | 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] | 156 | |
| 157 | profileCpu := flag.String("cpuprofile", "", "write cpu profile to file") |
| 158 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 159 | logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)") |
| 160 | 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] | 161 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 162 | 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] | 163 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 164 | flag.Parse() |
| 165 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 166 | conf.Olt.ID = int(*olt_id) |
| 167 | conf.Olt.NniPorts = uint32(*nni) |
| 168 | conf.Olt.PonPorts = uint32(*pon) |
| 169 | conf.Olt.OnusPonPort = uint32(*onu) |
| 170 | conf.BBSim.STag = int(*s_tag) |
| 171 | conf.BBSim.CTagInit = int(*c_tag_init) |
| 172 | conf.BBSim.CpuProfile = profileCpu |
| 173 | conf.BBSim.LogLevel = *logLevel |
| 174 | conf.BBSim.LogCaller = *logCaller |
| 175 | conf.BBSim.EnableAuth = *auth |
| 176 | conf.BBSim.EnableDhcp = *dhcp |
| 177 | conf.BBSim.Delay = *delay |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 178 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 179 | // update device id if not set |
| 180 | if conf.Olt.DeviceId == "" { |
| 181 | conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String() |
| 182 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 183 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame] | 184 | return conf |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | func GetBBROpts() BBRCliOptions { |
| 188 | |
| 189 | bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP") |
| 190 | bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port") |
| 191 | bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port") |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 192 | logFile := flag.String("logfile", "", "Log to a file") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 193 | |
| 194 | options := GetBBSimOpts() |
| 195 | |
| 196 | bbrOptions := BBRCliOptions{ |
| 197 | options, |
| 198 | *bbsimIp, |
| 199 | *bbsimPort, |
| 200 | *bbsimApiPort, |
Matteo Scandolo | f5c537e | 2019-10-28 16:45:57 -0700 | [diff] [blame] | 201 | *logFile, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return bbrOptions |
| 205 | } |