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