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