blob: 361d9b37435addc2cc6d89ec839e90c8d437ffea [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"`
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010073}
Matteo Scandoloc1147092019-10-29 09:38:33 -070074
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010075type BBRConfig struct {
76 Log string `yaml:"log"`
77 LogLevel string `yaml:"log_level"`
78 LogCaller bool `yaml:"log_caller"`
79}
80
81var Options *BBSimYamlConfig
82
83func init() {
84 // load settings from config file first
85 Options, _ = LoadBBSimConf("configs/bbsim.yaml")
86}
87
88func getDefaultOps() *BBSimYamlConfig {
89
90 c := &BBSimYamlConfig{
91 BBSimConfig{
92 STag: 900,
93 CTagInit: 900,
94 EnableDhcp: false,
95 EnableAuth: false,
96 LogLevel: "debug",
97 LogCaller: false,
98 Delay: 200,
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010099 OpenOltAddress: ":50060",
100 ApiAddress: ":50070",
101 RestApiAddress: ":50071",
102 LegacyApiAddress: ":50072",
103 LegacyRestApiAddress: ":50073",
104 SadisRestAddress: ":50074",
105 SadisServer: true,
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100106 },
107 OltConfig{
108 Vendor: "BBSim",
109 Model: "asfvolt16",
110 HardwareVersion: "emulated",
111 FirmwareVersion: "",
112 DeviceSerialNumber: "BBSM00000001",
113 PonPorts: 1,
114 NniPorts: 1,
115 OnusPonPort: 1,
116 Technology: "XGS-PON",
117 ID: 0,
118 OltRebootDelay: 10,
119 },
120 BBRConfig{
121 LogLevel: "debug",
122 LogCaller: false,
123 },
124 }
125 return c
126}
127
128// LoadBBSimConf loads the BBSim configuration from a YAML file
129func LoadBBSimConf(filename string) (*BBSimYamlConfig, error) {
130 yamlConfig := getDefaultOps()
131
132 yamlFile, err := ioutil.ReadFile(filename)
133 if err != nil {
134 fmt.Printf("Cannot load BBSim configuration file: %s. Using defaults.\n", err)
135 return yamlConfig, nil
136 }
137
138 err = yaml.Unmarshal(yamlFile, yamlConfig)
139 if err != nil {
140 fmt.Printf("Error parsing YAML file: %s\n", err)
141 }
142
143 return yamlConfig, nil
144}
145
146// GetBBSimOpts loads the BBSim configuration file and overides options with corresponding CLI flags if set
147func GetBBSimOpts() *BBSimYamlConfig {
148 conf := Options
149
150 olt_id := flag.Int("olt_id", conf.Olt.ID, "OLT device ID")
151 nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated")
152 pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated")
153 onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated")
154
155 s_tag := flag.Int("s_tag", conf.BBSim.STag, "S-Tag initial value")
156 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)")
157
158 auth := flag.Bool("auth", conf.BBSim.EnableAuth, "Set this flag if you want authentication to start automatically")
159 dhcp := flag.Bool("dhcp", conf.BBSim.EnableDhcp, "Set this flag if you want DHCP to start automatically")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700160
161 profileCpu := flag.String("cpuprofile", "", "write cpu profile to file")
162
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100163 logLevel := flag.String("logLevel", conf.BBSim.LogLevel, "Set the log level (trace, debug, info, warn, error)")
164 logCaller := flag.Bool("logCaller", conf.BBSim.LogCaller, "Whether to print the caller filename or not")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700165
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100166 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 -0700167
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700168 flag.Parse()
169
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100170 conf.Olt.ID = int(*olt_id)
171 conf.Olt.NniPorts = uint32(*nni)
172 conf.Olt.PonPorts = uint32(*pon)
173 conf.Olt.OnusPonPort = uint32(*onu)
174 conf.BBSim.STag = int(*s_tag)
175 conf.BBSim.CTagInit = int(*c_tag_init)
176 conf.BBSim.CpuProfile = profileCpu
177 conf.BBSim.LogLevel = *logLevel
178 conf.BBSim.LogCaller = *logCaller
179 conf.BBSim.EnableAuth = *auth
180 conf.BBSim.EnableDhcp = *dhcp
181 conf.BBSim.Delay = *delay
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700182
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100183 // update device id if not set
184 if conf.Olt.DeviceId == "" {
185 conf.Olt.DeviceId = net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(conf.Olt.ID)}.String()
186 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700187
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100188 Options = conf
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100189 return conf
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700190}
191
192func GetBBROpts() BBRCliOptions {
193
194 bbsimIp := flag.String("bbsimIp", "127.0.0.1", "BBSim IP")
195 bbsimPort := flag.String("bbsimPort", "50060", "BBSim Port")
196 bbsimApiPort := flag.String("bbsimApiPort", "50070", "BBSim API Port")
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700197 logFile := flag.String("logfile", "", "Log to a file")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700198
199 options := GetBBSimOpts()
200
201 bbrOptions := BBRCliOptions{
202 options,
203 *bbsimIp,
204 *bbsimPort,
205 *bbsimApiPort,
Matteo Scandolof5c537e2019-10-28 16:45:57 -0700206 *logFile,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700207 }
208
209 return bbrOptions
210}