Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | */ |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 16 | package main |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "flag" |
| 21 | "fmt" |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 22 | "log" |
| 23 | "os" |
| 24 | "os/signal" |
| 25 | "path" |
Jeff | 70e8b2d | 2018-07-24 13:37:29 -0700 | [diff] [blame] | 26 | |
| 27 | "github.com/opencord/voltha/ponsim/v2/common" |
| 28 | "github.com/opencord/voltha/ponsim/v2/core" |
| 29 | "github.com/opencord/voltha/ponsim/v2/grpc" |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | // TODO: Cleanup logs |
| 33 | |
| 34 | const ( |
| 35 | default_name = "PON" |
| 36 | default_grpc_port = 50060 |
| 37 | default_grpc_addr = "" |
| 38 | default_device_type = "OLT" |
| 39 | default_api_type = "PONSIM" |
| 40 | default_internal_if = "eth0" |
| 41 | default_external_if = "eth1" |
| 42 | default_onus = 1 |
| 43 | default_alarm_sim = false |
| 44 | default_alarm_freq = 60 |
| 45 | default_quiet = false |
| 46 | default_verbose = false |
| 47 | default_no_banner = false |
| 48 | default_parent_addr = "olt" |
| 49 | default_parent_port = 50060 |
| 50 | default_vcore_endpoint = "vcore" |
| 51 | default_fluentd_host = "" |
Andy Bavier | ea82b46 | 2018-07-27 16:48:13 -0700 | [diff] [blame] | 52 | default_serial_number = "PSMO12345678" |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 53 | |
| 54 | default_snapshot_len = 65535 |
| 55 | default_promiscuous = false |
| 56 | |
| 57 | default_voltha_key = "pki/voltha.key" |
| 58 | default_voltha_cert = "pki/voltha.crt" |
| 59 | default_voltha_ca = "pki/voltha-CA.pem" |
| 60 | ) |
| 61 | |
| 62 | var ( |
| 63 | voltha_base = os.Getenv("VOLTHA_BASE") |
| 64 | certs *grpc.GrpcSecurity |
| 65 | |
| 66 | name string = default_name + "_" + device_type |
| 67 | grpc_port int = default_grpc_port |
| 68 | grpc_addr string = default_grpc_addr |
| 69 | device_type string = default_device_type |
| 70 | api_type string = default_api_type |
| 71 | internal_if string = default_internal_if |
| 72 | external_if string = default_external_if |
| 73 | onus int = default_onus |
| 74 | alarm_sim bool = default_alarm_sim |
| 75 | alarm_freq int = default_alarm_freq |
| 76 | quiet bool = default_quiet |
| 77 | verbose bool = default_verbose |
| 78 | no_banner bool = default_no_banner |
| 79 | voltha_key string = default_voltha_key |
| 80 | voltha_cert string = default_voltha_cert |
| 81 | voltha_ca string = default_voltha_ca |
| 82 | parent_addr string = default_parent_addr |
| 83 | parent_port int = default_parent_port |
| 84 | vcore_endpoint string = default_vcore_endpoint |
| 85 | fluentd_host string = default_fluentd_host |
Andy Bavier | ea82b46 | 2018-07-27 16:48:13 -0700 | [diff] [blame] | 86 | serial_number string = default_serial_number |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 87 | |
| 88 | snapshot_len int32 = default_snapshot_len |
| 89 | promiscuous bool = default_promiscuous |
| 90 | ) |
| 91 | |
| 92 | func init() { |
| 93 | parseArgs() |
| 94 | |
| 95 | // Enable fluentd support |
| 96 | if fluentd_host != "" { |
| 97 | common.Logger().SetFluentd(fluentd_host) |
| 98 | } |
| 99 | |
| 100 | // Print banner unless no_banner is specified |
| 101 | if !no_banner { |
| 102 | printBanner() |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func parseArgs() { |
| 107 | var help string |
| 108 | |
| 109 | help = fmt.Sprintf("Name of the PON device") |
| 110 | flag.StringVar(&grpc_addr, "name", default_name, help) |
| 111 | |
| 112 | help = fmt.Sprintf("Address used to establish GRPC server connection") |
| 113 | flag.StringVar(&grpc_addr, "grpc_addr", default_grpc_addr, help) |
| 114 | |
| 115 | help = fmt.Sprintf("Port used to establish GRPC server connection") |
| 116 | flag.IntVar(&grpc_port, "grpc_port", default_grpc_port, help) |
| 117 | |
| 118 | help = fmt.Sprintf("Type of device to simulate (OLT or ONU)") |
| 119 | flag.StringVar(&device_type, "device_type", default_device_type, help) |
| 120 | |
| 121 | help = fmt.Sprintf("Type of API used to communicate with devices (PONSIM or BAL)") |
| 122 | flag.StringVar(&api_type, "api_type", default_api_type, help) |
| 123 | |
| 124 | help = fmt.Sprintf("Internal Communication Interface for read/write network traffic") |
| 125 | flag.StringVar(&internal_if, "internal_if", default_internal_if, help) |
| 126 | |
| 127 | help = fmt.Sprintf("External Communication Interface for read/write network traffic") |
| 128 | flag.StringVar(&external_if, "external_if", default_external_if, help) |
| 129 | |
| 130 | help = fmt.Sprintf("Enable promiscuous mode on network interfaces") |
| 131 | flag.BoolVar(&promiscuous, "promiscuous", default_promiscuous, help) |
| 132 | |
| 133 | help = fmt.Sprintf("Number of ONUs to simulate") |
| 134 | flag.IntVar(&onus, "onus", default_onus, help) |
| 135 | |
| 136 | help = fmt.Sprintf("Suppress debug and info logs") |
| 137 | flag.BoolVar(&quiet, "quiet", default_quiet, help) |
| 138 | |
| 139 | help = fmt.Sprintf("Enable verbose logging") |
| 140 | flag.BoolVar(&verbose, "verbose", default_verbose, help) |
| 141 | |
| 142 | help = fmt.Sprintf("Omit startup banner log lines") |
| 143 | flag.BoolVar(&no_banner, "no_banner", default_no_banner, help) |
| 144 | |
| 145 | help = fmt.Sprintf("Enable generation of simulated alarms") |
| 146 | flag.BoolVar(&alarm_sim, "alarm_sim", default_alarm_sim, help) |
| 147 | |
| 148 | help = fmt.Sprintf("Frequency of simulated alarms (in seconds)") |
| 149 | flag.IntVar(&alarm_freq, "alarm_freq", default_alarm_freq, help) |
| 150 | |
| 151 | help = fmt.Sprintf("Address of OLT to connect to") |
| 152 | flag.StringVar(&parent_addr, "parent_addr", default_parent_addr, help) |
| 153 | |
| 154 | help = fmt.Sprintf("Port of OLT to connect to") |
| 155 | flag.IntVar(&parent_port, "parent_port", default_parent_port, help) |
| 156 | |
| 157 | help = fmt.Sprintf("Voltha core endpoint address") |
| 158 | flag.StringVar(&vcore_endpoint, "vcore_endpoint", default_vcore_endpoint, help) |
| 159 | |
| 160 | help = fmt.Sprintf("Fluentd host address") |
| 161 | flag.StringVar(&fluentd_host, "fluentd", default_fluentd_host, help) |
| 162 | |
Andy Bavier | ea82b46 | 2018-07-27 16:48:13 -0700 | [diff] [blame] | 163 | help = fmt.Sprintf("Serial number of ONU device") |
| 164 | flag.StringVar(&serial_number, "serial_number", default_serial_number, help) |
| 165 | |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 166 | flag.Parse() |
| 167 | } |
| 168 | |
| 169 | func printBanner() { |
| 170 | log.Println(" ____ ____ _ _______ ______ ___") |
| 171 | log.Println(" / __ \\/ __ \\/ | / / ___// _/ |/ /") |
| 172 | log.Println(" / /_/ / / / / |/ /\\__ \\ / // /|_/ / ") |
| 173 | log.Println(" / ____/ /_/ / /| /___/ // // / / / ") |
| 174 | log.Println("/_/ \\____/_/ |_//____/___/_/ /_/ ") |
| 175 | |
| 176 | switch device_type { |
| 177 | case "OLT": |
| 178 | printOltBanner() |
| 179 | case "ONU": |
| 180 | printOnuBanner() |
| 181 | } |
| 182 | |
| 183 | log.Println("(to stop: press Ctrl-C)") |
| 184 | } |
| 185 | func printOltBanner() { |
| 186 | log.Println(" ____ __ ______") |
| 187 | log.Println(" / __ \\/ / /_ __/") |
| 188 | log.Println(" / / / / / / / ") |
| 189 | log.Println("/ /_/ / /___/ / ") |
| 190 | log.Println("\\____/_____/_/ ") |
| 191 | } |
| 192 | func printOnuBanner() { |
| 193 | log.Println(" ____ _ ____ __") |
| 194 | log.Println(" / __ \\/ | / / / / /") |
| 195 | log.Println(" / / / / |/ / / / / ") |
| 196 | log.Println("/ /_/ / /| / /_/ / ") |
| 197 | log.Println("\\____/_/ |_/\\____/ ") |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | ----------------------------------------------------------------- |
| 202 | */ |
| 203 | type PonSimService struct { |
| 204 | device core.PonSimInterface |
| 205 | server *grpc.GrpcServer |
| 206 | } |
| 207 | |
| 208 | func (s *PonSimService) Start(ctx context.Context) { |
| 209 | // GRPC server needs to be secure. |
| 210 | // Otherwise communication between adapter and simulator does not occur |
Jeff | 70e8b2d | 2018-07-24 13:37:29 -0700 | [diff] [blame] | 211 | s.server = grpc.NewGrpcServer(s.device.GetAddress(), s.device.GetPort(), certs, false) |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 212 | |
| 213 | // Add GRPC services |
| 214 | s.server.AddCommonService(s.device) |
| 215 | s.server.AddPonSimService(s.device) |
| 216 | |
| 217 | // Add OLT specific services |
| 218 | if device_type == core.OLT.String() { |
| 219 | s.server.AddOltService(s.device) |
| 220 | } |
| 221 | |
| 222 | // Add XPON services unless using BAL |
| 223 | if api_type == core.PONSIM.String() { |
| 224 | s.server.AddXPonService() |
| 225 | } else { |
| 226 | s.server.AddBalService() |
| 227 | } |
| 228 | |
| 229 | // Start the GRPC server |
| 230 | go s.server.Start(ctx) |
| 231 | |
| 232 | // Start the PON device |
| 233 | go s.device.Start(ctx) |
| 234 | } |
| 235 | |
| 236 | func (s *PonSimService) Stop(ctx context.Context) { |
| 237 | // Stop PON device |
| 238 | s.device.Stop(ctx) |
| 239 | |
| 240 | // Stop GRPC server |
| 241 | s.server.Stop() |
| 242 | } |
| 243 | |
| 244 | func main() { |
| 245 | var device core.PonSimInterface |
| 246 | |
| 247 | // Init based on type of device |
| 248 | // Construct OLT/ONU object and pass it down |
| 249 | certs = &grpc.GrpcSecurity{ |
| 250 | CertFile: path.Join(voltha_base, voltha_cert), |
| 251 | KeyFile: path.Join(voltha_base, voltha_key), |
| 252 | CaFile: path.Join(voltha_base, voltha_ca), |
| 253 | } |
| 254 | |
| 255 | // Initialize device with common parameters |
| 256 | pon := core.PonSimDevice{ |
| 257 | Name: name, |
| 258 | ExternalIf: external_if, |
| 259 | InternalIf: internal_if, |
| 260 | Promiscuous: promiscuous, |
| 261 | SnapshotLen: snapshot_len, |
| 262 | Address: grpc_addr, |
| 263 | Port: int32(grpc_port), |
| 264 | AlarmsOn: alarm_sim, |
| 265 | AlarmsFreq: alarm_freq, |
| 266 | Counter: core.NewPonSimMetricCounter(name), |
| 267 | |
| 268 | // TODO: pass certificates |
| 269 | //GrpcSecurity: certs, |
| 270 | } |
| 271 | |
| 272 | switch device_type { |
| 273 | case core.OLT.String(): |
| 274 | device = core.NewPonSimOltDevice(pon) |
| 275 | device.(*core.PonSimOltDevice).MaxOnuCount = onus |
| 276 | device.(*core.PonSimOltDevice).VCoreEndpoint = vcore_endpoint |
| 277 | |
| 278 | case core.ONU.String(): |
| 279 | device = core.NewPonSimOnuDevice(pon) |
| 280 | device.(*core.PonSimOnuDevice).ParentAddress = parent_addr |
| 281 | device.(*core.PonSimOnuDevice).ParentPort = int32(parent_port) |
Andy Bavier | ea82b46 | 2018-07-27 16:48:13 -0700 | [diff] [blame] | 282 | device.(*core.PonSimOnuDevice).SerialNumber = serial_number |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 283 | |
| 284 | default: |
| 285 | log.Println("Unknown device type") |
| 286 | } |
| 287 | |
| 288 | ps := PonSimService{device: device} |
| 289 | |
| 290 | ctx, cancel := context.WithCancel(context.Background()) |
| 291 | defer cancel() |
| 292 | |
| 293 | ps.Start(ctx) |
| 294 | |
| 295 | signals := make(chan os.Signal, 1) |
| 296 | signal.Notify(signals, os.Interrupt) |
| 297 | |
| 298 | doneCh := make(chan struct{}) |
| 299 | |
| 300 | go func() { |
| 301 | for { |
| 302 | select { |
| 303 | case <-signals: |
| 304 | log.Println("Interrupt was detected") |
| 305 | doneCh <- struct{}{} |
| 306 | } |
| 307 | } |
| 308 | }() |
| 309 | |
| 310 | <-doneCh |
| 311 | } |