blob: d5317e19e56c0fc31b8377d2eef09b987f359818 [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"`
55}
Matteo Scandolo40e067f2019-10-16 16:59:41 -070056
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010057type 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 Bozakov958d81c2019-12-13 22:09:48 +010071 SadisRestAddress string `yaml:"sadis_rest_address"`
72 SadisServer bool `yaml:"sadis_server"`
Pragya Arya2225f202020-01-29 18:05:01 +053073 ControlledActivation string `yaml:"controlled_activation"`
Anand S Katti09541352020-01-29 15:54:01 +053074 EnablePerf bool `yaml:"enable_perf"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010075}
Matteo Scandoloc1147092019-10-29 09:38:33 -070076
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010077type BBRConfig struct {
78 Log string `yaml:"log"`
79 LogLevel string `yaml:"log_level"`
80 LogCaller bool `yaml:"log_caller"`
81}
82
83var Options *BBSimYamlConfig
84
85func init() {
86 // load settings from config file first
87 Options, _ = LoadBBSimConf("configs/bbsim.yaml")
88}
89
90func getDefaultOps() *BBSimYamlConfig {
91
92 c := &BBSimYamlConfig{
93 BBSimConfig{
94 STag: 900,
95 CTagInit: 900,
96 EnableDhcp: false,
97 EnableAuth: false,
98 LogLevel: "debug",
99 LogCaller: false,
100 Delay: 200,
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100101 OpenOltAddress: ":50060",
102 ApiAddress: ":50070",
103 RestApiAddress: ":50071",
104 LegacyApiAddress: ":50072",
105 LegacyRestApiAddress: ":50073",
106 SadisRestAddress: ":50074",
107 SadisServer: true,
Pragya Arya2225f202020-01-29 18:05:01 +0530108 ControlledActivation: "default",
Anand S Katti09541352020-01-29 15:54:01 +0530109 EnablePerf: false,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100110 },
111 OltConfig{
112 Vendor: "BBSim",
113 Model: "asfvolt16",
114 HardwareVersion: "emulated",
115 FirmwareVersion: "",
116 DeviceSerialNumber: "BBSM00000001",
117 PonPorts: 1,
118 NniPorts: 1,
119 OnusPonPort: 1,
120 Technology: "XGS-PON",
121 ID: 0,
122 OltRebootDelay: 10,
123 },
124 BBRConfig{
125 LogLevel: "debug",
126 LogCaller: false,
127 },
128 }
129 return c
130}
131
132// LoadBBSimConf loads the BBSim configuration from a YAML file
133func LoadBBSimConf(filename string) (*BBSimYamlConfig, error) {
134 yamlConfig := getDefaultOps()
135
136 yamlFile, err := ioutil.ReadFile(filename)
137 if err != nil {
138 fmt.Printf("Cannot load BBSim configuration file: %s. Using defaults.\n", err)
139 return yamlConfig, nil
140 }
141
142 err = yaml.Unmarshal(yamlFile, yamlConfig)
143 if err != nil {
144 fmt.Printf("Error parsing YAML file: %s\n", err)
145 }
146
147 return yamlConfig, nil
148}
149
Anand S Katti09541352020-01-29 15:54:01 +0530150// GetBBSimOpts loads the BBSim configuration file and over-rides options with corresponding CLI flags if set
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100151func GetBBSimOpts() *BBSimYamlConfig {
152 conf := Options
153
154 olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID")
155 nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated")
156 pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated")
157 onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated")
158
159 s_tag := flag.Int("s_tag", conf.BBSim.STag, "S-Tag initial value")
160 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)")
161
162 auth := flag.Bool("auth", conf.BBSim.EnableAuth, "Set this flag if you want authentication to start automatically")
163 dhcp := flag.Bool("dhcp", conf.BBSim.EnableDhcp, "Set this flag if you want DHCP to start automatically")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700164
165 profileCpu := flag.String("cpuprofile", "", "write cpu profile to file")
166
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100167 logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)")
168 logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700169
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100170 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 -0700171
Pragya Arya2225f202020-01-29 18:05:01 +0530172 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 +0530173 enablePerf := flag.Bool("enableperf", conf.BBSim.EnablePerf, "Setting this flag will cause BBSim to not store data like traffic schedulers, flows of ONUs etc..")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700174 flag.Parse()
175
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100176 conf.Olt.ID = int(*olt_id)
177 conf.Olt.NniPorts = uint32(*nni)
178 conf.Olt.PonPorts = uint32(*pon)
179 conf.Olt.OnusPonPort = uint32(*onu)
180 conf.BBSim.STag = int(*s_tag)
181 conf.BBSim.CTagInit = int(*c_tag_init)
182 conf.BBSim.CpuProfile = profileCpu
183 conf.BBSim.LogLevel = *logLevel
184 conf.BBSim.LogCaller = *logCaller
185 conf.BBSim.EnableAuth = *auth
186 conf.BBSim.EnableDhcp = *dhcp
187 conf.BBSim.Delay = *delay
Pragya Arya2225f202020-01-29 18:05:01 +0530188 conf.BBSim.ControlledActivation = *controlledActivation
Anand S Katti09541352020-01-29 15:54:01 +0530189 conf.BBSim.EnablePerf = *enablePerf
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700190
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100191 // update device id if not set
192 if conf.Olt.DeviceId == "" {
193 conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String()
194 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700195
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100196 Options = conf
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100197 return conf
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700198}
199
200func GetBBROpts() BBRCliOptions {
201
202 bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP")
203 bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port")
204 bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port")
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700205 logFile := flag.String("logfile", "", "Log to a file")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700206
207 options := GetBBSimOpts()
208
209 bbrOptions := BBRCliOptions{
210 options,
211 *bbsimIp,
212 *bbsimPort,
213 *bbsimApiPort,
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700214 *logFile,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700215 }
216
217 return bbrOptions
218}