blob: 05479c9ba7448a7e7ef6e76b8a1b1ce7b0c67c5f [file] [log] [blame]
cuilin20187b2a8c32019-03-26 19:52:28 -07001/*
cbabu116b73f2019-12-10 17:56:32 +05302* Copyright 2018-present Open Networking Foundation
cuilin20187b2a8c32019-03-26 19:52:28 -07003
cbabu116b73f2019-12-10 17:56:32 +05304* 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
cuilin20187b2a8c32019-03-26 19:52:28 -07007
cbabu116b73f2019-12-10 17:56:32 +05308* http://www.apache.org/licenses/LICENSE-2.0
cuilin20187b2a8c32019-03-26 19:52:28 -07009
cbabu116b73f2019-12-10 17:56:32 +053010* 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.
cuilin20187b2a8c32019-03-26 19:52:28 -070015 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070016
17//Package main invokes the application
cuilin20187b2a8c32019-03-26 19:52:28 -070018package main
19
20import (
21 "context"
22 "errors"
23 "fmt"
kdarapu381c6902019-07-31 18:23:16 +053024 "os"
25 "os/signal"
26 "strconv"
27 "syscall"
28 "time"
29
Esin Karamanccb714b2019-11-29 15:02:06 +000030 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
kdarapu381c6902019-07-31 18:23:16 +053031
Esin Karamanccb714b2019-11-29 15:02:06 +000032 "github.com/opencord/voltha-lib-go/v3/pkg/adapters"
33 com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common"
34 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
35 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
36 "github.com/opencord/voltha-lib-go/v3/pkg/log"
37 "github.com/opencord/voltha-lib-go/v3/pkg/probe"
Girish Gowdru0c588b22019-04-23 23:24:56 -040038 ac "github.com/opencord/voltha-openolt-adapter/adaptercore"
39 "github.com/opencord/voltha-openolt-adapter/config"
Matt Jeanneret0c9ae282019-07-18 18:14:28 -040040 "github.com/opencord/voltha-openolt-adapter/config/version"
Esin Karamanccb714b2019-11-29 15:02:06 +000041 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
42 "github.com/opencord/voltha-protos/v3/go/voltha"
cuilin20187b2a8c32019-03-26 19:52:28 -070043)
44
45type adapter struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070046 instanceID string
cuilin20187b2a8c32019-03-26 19:52:28 -070047 config *config.AdapterFlags
48 iAdapter adapters.IAdapter
49 kafkaClient kafka.Client
50 kvClient kvstore.Client
npujarec5762e2020-01-01 14:08:48 +053051 kip kafka.InterContainerProxy
kdarapu381c6902019-07-31 18:23:16 +053052 coreProxy adapterif.CoreProxy
53 adapterProxy adapterif.AdapterProxy
54 eventProxy adapterif.EventProxy
cuilin20187b2a8c32019-03-26 19:52:28 -070055 halted bool
56 exitChannel chan int
57 receiverChannels []<-chan *ic.InterContainerMessage
58}
59
60func init() {
Matteo Scandolo945e4012019-12-12 14:16:11 -080061 _, _ = log.AddPackage(log.CONSOLE, log.DebugLevel, nil)
cuilin20187b2a8c32019-03-26 19:52:28 -070062}
63
64func newAdapter(cf *config.AdapterFlags) *adapter {
65 var a adapter
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070066 a.instanceID = cf.InstanceID
cuilin20187b2a8c32019-03-26 19:52:28 -070067 a.config = cf
68 a.halted = false
69 a.exitChannel = make(chan int, 1)
70 a.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
71 return &a
72}
73
74func (a *adapter) start(ctx context.Context) {
75 log.Info("Starting Core Adapter components")
76 var err error
77
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000078 var p *probe.Probe
79 if value := ctx.Value(probe.ProbeContextKey); value != nil {
80 if _, ok := value.(*probe.Probe); ok {
81 p = value.(*probe.Probe)
82 p.RegisterService(
83 "message-bus",
84 "kv-store",
85 "container-proxy",
86 "core-request-handler",
87 "register-with-core",
88 )
89 }
90 }
91
cuilin20187b2a8c32019-03-26 19:52:28 -070092 // Setup KV Client
93 log.Debugw("create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070094 if err = a.setKVClient(); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -070095 log.Fatal("error-setting-kv-client")
96 }
97
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000098 if p != nil {
99 p.UpdateStatus("kv-store", probe.ServiceStatusRunning)
100 }
101
cuilin20187b2a8c32019-03-26 19:52:28 -0700102 // Setup Kafka Client
103 if a.kafkaClient, err = newKafkaClient("sarama", a.config.KafkaAdapterHost, a.config.KafkaAdapterPort); err != nil {
104 log.Fatal("Unsupported-common-client")
105 }
106
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000107 if p != nil {
108 p.UpdateStatus("message-bus", probe.ServiceStatusRunning)
109 }
110
cuilin20187b2a8c32019-03-26 19:52:28 -0700111 // Start the common InterContainer Proxy - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000112 if a.kip, err = a.startInterContainerProxy(ctx, -1); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700113 log.Fatal("error-starting-inter-container-proxy")
114 }
115
116 // Create the core proxy to handle requests to the Core
117 a.coreProxy = com.NewCoreProxy(a.kip, a.config.Topic, a.config.CoreTopic)
118
119 // Create the adaptor proxy to handle request between olt and onu
120 a.adapterProxy = com.NewAdapterProxy(a.kip, "brcm_openomci_onu", a.config.CoreTopic)
121
Devmalya Paulfb990a52019-07-09 10:01:49 -0400122 // Create the event proxy to post events to KAFKA
123 a.eventProxy = com.NewEventProxy(com.MsgClient(a.kafkaClient), com.MsgTopic(kafka.Topic{Name: a.config.EventTopic}))
124
cuilin20187b2a8c32019-03-26 19:52:28 -0700125 // Create the open OLT adapter
kdarapu381c6902019-07-31 18:23:16 +0530126 if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530127 a.config); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700128 log.Fatal("error-starting-inter-container-proxy")
129 }
130
131 // Register the core request handler
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000132 if err = a.setupRequestHandler(ctx, a.instanceID, a.iAdapter); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700133 log.Fatal("error-setting-core-request-handler")
134 }
135
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700136 // Register this adapter to the Core - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000137 if err = a.registerWithCore(ctx, -1); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700138 log.Fatal("error-registering-with-core")
139 }
cbabu95f21522019-11-13 14:25:18 +0100140
cbabu116b73f2019-12-10 17:56:32 +0530141 // check the readiness and liveliness and update the probe status
142 a.checkServicesReadiness(ctx)
cbabu95f21522019-11-13 14:25:18 +0100143}
144
145/**
146This function checks the liveliness and readiness of the kakfa and kv-client services
147and update the status in the probe.
148*/
cbabu116b73f2019-12-10 17:56:32 +0530149func (a *adapter) checkServicesReadiness(ctx context.Context) {
150 // checks the kafka readiness
151 go a.checkKafkaReadiness(ctx)
152
153 // checks the kv-store readiness
154 go a.checkKvStoreReadiness(ctx)
155}
156
157/**
158This function checks the liveliness and readiness of the kv-store service
159and update the status in the probe.
160*/
161func (a *adapter) checkKvStoreReadiness(ctx context.Context) {
162 // dividing the live probe interval by 2 to get updated status every 30s
163 timeout := a.config.LiveProbeInterval / 2
164 kvStoreChannel := make(chan bool, 1)
165
166 // Default false to check the liveliness.
167 kvStoreChannel <- false
cbabu95f21522019-11-13 14:25:18 +0100168 for {
cbabu116b73f2019-12-10 17:56:32 +0530169 timeoutTimer := time.NewTimer(timeout)
170 select {
171 case liveliness := <-kvStoreChannel:
172 if !liveliness {
173 // kv-store not reachable or down, updating the status to not ready state
174 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady)
175 timeout = a.config.NotLiveProbeInterval
176 } else {
177 // kv-store is reachable , updating the status to running state
178 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning)
179 timeout = a.config.LiveProbeInterval / 2
180 }
181 // Check if the timer has expired or not
182 if !timeoutTimer.Stop() {
183 <-timeoutTimer.C
184 }
185 case <-timeoutTimer.C:
186 // Check the status of the kv-store
187 log.Info("kv-store liveliness-recheck")
npujarec5762e2020-01-01 14:08:48 +0530188 if a.kvClient.IsConnectionUp(ctx) {
cbabu116b73f2019-12-10 17:56:32 +0530189 kvStoreChannel <- true
190 } else {
191 kvStoreChannel <- false
192 }
cbabu95f21522019-11-13 14:25:18 +0100193 }
cbabu116b73f2019-12-10 17:56:32 +0530194 }
195}
196
197/**
198This function checks the liveliness and readiness of the kafka service
199and update the status in the probe.
200*/
201func (a *adapter) checkKafkaReadiness(ctx context.Context) {
202 livelinessChannel := a.kafkaClient.EnableLivenessChannel(true)
Scott Baker86fce9a2019-12-12 09:47:17 -0800203 healthinessChannel := a.kafkaClient.EnableHealthinessChannel(true)
cbabu116b73f2019-12-10 17:56:32 +0530204 timeout := a.config.LiveProbeInterval
Scott Bakere701b862020-02-20 16:19:16 -0800205 failed := false
cbabu116b73f2019-12-10 17:56:32 +0530206 for {
207 timeoutTimer := time.NewTimer(timeout)
208
209 select {
Scott Baker86fce9a2019-12-12 09:47:17 -0800210 case healthiness := <-healthinessChannel:
211 if !healthiness {
Scott Bakere701b862020-02-20 16:19:16 -0800212 // This will eventually cause K8s to restart the container, and will do
213 // so in a way that allows cleanup to continue, rather than an immediate
214 // panic and exit here.
215 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusFailed)
216 failed = true
217 }
218 // Check if the timer has expired or not
219 if !timeoutTimer.Stop() {
220 <-timeoutTimer.C
Scott Baker86fce9a2019-12-12 09:47:17 -0800221 }
cbabu116b73f2019-12-10 17:56:32 +0530222 case liveliness := <-livelinessChannel:
Scott Bakere701b862020-02-20 16:19:16 -0800223 if failed {
224 // Failures of the message bus are permanent and can't ever be recovered from,
225 // so make sure we never inadvertently reset a failed state back to unready.
226 } else if !liveliness {
cbabu116b73f2019-12-10 17:56:32 +0530227 // kafka not reachable or down, updating the status to not ready state
228 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady)
229 timeout = a.config.NotLiveProbeInterval
230 } else {
231 // kafka is reachable , updating the status to running state
232 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning)
233 timeout = a.config.LiveProbeInterval
234 }
235 // Check if the timer has expired or not
236 if !timeoutTimer.Stop() {
237 <-timeoutTimer.C
238 }
239 case <-timeoutTimer.C:
240 log.Info("kafka-proxy-liveness-recheck")
241 // send the liveness probe in a goroutine; we don't want to deadlock ourselves as
242 // the liveness probe may wait (and block) writing to our channel.
243 err := a.kafkaClient.SendLiveness()
244 if err != nil {
245 // Catch possible error case if sending liveness after Sarama has been stopped.
246 log.Warnw("error-kafka-send-liveness", log.Fields{"error": err})
247 }
cbabu95f21522019-11-13 14:25:18 +0100248 }
cbabu95f21522019-11-13 14:25:18 +0100249 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700250}
251
npujarec5762e2020-01-01 14:08:48 +0530252func (a *adapter) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700253 // Stop leadership tracking
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 a.halted = true
cuilin20187b2a8c32019-03-26 19:52:28 -0700255
256 // send exit signal
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700257 a.exitChannel <- 0
cuilin20187b2a8c32019-03-26 19:52:28 -0700258
259 // Cleanup - applies only if we had a kvClient
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700260 if a.kvClient != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700261 // Release all reservations
npujarec5762e2020-01-01 14:08:48 +0530262 if err := a.kvClient.ReleaseAllReservations(ctx); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700263 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
264 }
265 // Close the DB connection
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700266 a.kvClient.Close()
cuilin20187b2a8c32019-03-26 19:52:28 -0700267 }
268
Scott Bakere701b862020-02-20 16:19:16 -0800269 if a.kip != nil {
270 a.kip.Stop()
271 }
272
cuilin20187b2a8c32019-03-26 19:52:28 -0700273 // TODO: More cleanup
274}
275
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700276func newKVClient(storeType, address string, timeout int) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700277
278 log.Infow("kv-store-type", log.Fields{"store": storeType})
279 switch storeType {
280 case "consul":
281 return kvstore.NewConsulClient(address, timeout)
282 case "etcd":
283 return kvstore.NewEtcdClient(address, timeout)
284 }
285 return nil, errors.New("unsupported-kv-store")
286}
287
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700288func newKafkaClient(clientType, host string, port int) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700289
290 log.Infow("common-client-type", log.Fields{"client": clientType})
291 switch clientType {
292 case "sarama":
293 return kafka.NewSaramaClient(
294 kafka.Host(host),
295 kafka.Port(port),
296 kafka.ProducerReturnOnErrors(true),
297 kafka.ProducerReturnOnSuccess(true),
298 kafka.ProducerMaxRetries(6),
Abhilash S.L3b494632019-07-16 15:51:09 +0530299 kafka.ProducerRetryBackoff(time.Millisecond*30),
300 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700301 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700302
cuilin20187b2a8c32019-03-26 19:52:28 -0700303 return nil, errors.New("unsupported-client-type")
304}
305
306func (a *adapter) setKVClient() error {
307 addr := a.config.KVStoreHost + ":" + strconv.Itoa(a.config.KVStorePort)
308 client, err := newKVClient(a.config.KVStoreType, addr, a.config.KVStoreTimeout)
309 if err != nil {
310 a.kvClient = nil
311 log.Error(err)
312 return err
313 }
314 a.kvClient = client
315 return nil
316}
317
npujarec5762e2020-01-01 14:08:48 +0530318func (a *adapter) startInterContainerProxy(ctx context.Context, retries int) (kafka.InterContainerProxy, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700319 log.Infow("starting-intercontainer-messaging-proxy", log.Fields{"host": a.config.KafkaAdapterHost,
320 "port": a.config.KafkaAdapterPort, "topic": a.config.Topic})
321 var err error
npujarec5762e2020-01-01 14:08:48 +0530322 kip := kafka.NewInterContainerProxy(
cuilin20187b2a8c32019-03-26 19:52:28 -0700323 kafka.InterContainerHost(a.config.KafkaAdapterHost),
324 kafka.InterContainerPort(a.config.KafkaAdapterPort),
325 kafka.MsgClient(a.kafkaClient),
npujarec5762e2020-01-01 14:08:48 +0530326 kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic}))
cuilin20187b2a8c32019-03-26 19:52:28 -0700327 count := 0
328 for {
329 if err = kip.Start(); err != nil {
330 log.Warnw("error-starting-messaging-proxy", log.Fields{"error": err})
331 if retries == count {
332 return nil, err
333 }
334 count = +1
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700335 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700336 time.Sleep(2 * time.Second)
337 } else {
338 break
339 }
340 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000341 probe.UpdateStatusFromContext(ctx, "container-proxy", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700342 log.Info("common-messaging-proxy-created")
343 return kip, nil
344}
345
npujarec5762e2020-01-01 14:08:48 +0530346func (a *adapter) startOpenOLT(ctx context.Context, kip kafka.InterContainerProxy,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530347 cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy,
348 cfg *config.AdapterFlags) (*ac.OpenOLT, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700349 log.Info("starting-open-olt")
350 var err error
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530351 sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, ep, cfg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700352
353 if err = sOLT.Start(ctx); err != nil {
354 log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err})
355 return nil, err
356 }
357
358 log.Info("open-olt-started")
359 return sOLT, nil
360}
361
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000362func (a *adapter) setupRequestHandler(ctx context.Context, coreInstanceID string, iadapter adapters.IAdapter) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700363 log.Info("setting-request-handler")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy)
cuilin20187b2a8c32019-03-26 19:52:28 -0700365 if err := a.kip.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
366 log.Errorw("request-handler-setup-failed", log.Fields{"error": err})
367 return err
368
369 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000370 probe.UpdateStatusFromContext(ctx, "core-request-handler", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700371 log.Info("request-handler-setup-done")
372 return nil
373}
374
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000375func (a *adapter) registerWithCore(ctx context.Context, retries int) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700376 log.Info("registering-with-core")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400377 adapterDescription := &voltha.Adapter{Id: "openolt", // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400378 Vendor: "VOLTHA OpenOLT",
379 Version: version.VersionInfo.Version}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400380 types := []*voltha.DeviceType{{Id: "openolt",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700381 Adapter: "openolt", // Name of the adapter that handles device type
Girish Gowdru0c588b22019-04-23 23:24:56 -0400382 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
383 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700384 deviceTypes := &voltha.DeviceTypes{Items: types}
385 count := 0
386 for {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700387 if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700388 log.Warnw("registering-with-core-failed", log.Fields{"error": err})
389 if retries == count {
390 return err
391 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700392 count++
393 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700394 time.Sleep(2 * time.Second)
395 } else {
396 break
397 }
398 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000399 probe.UpdateStatusFromContext(ctx, "register-with-core", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700400 log.Info("registered-with-core")
401 return nil
402}
403
404func waitForExit() int {
405 signalChannel := make(chan os.Signal, 1)
406 signal.Notify(signalChannel,
407 syscall.SIGHUP,
408 syscall.SIGINT,
409 syscall.SIGTERM,
410 syscall.SIGQUIT)
411
412 exitChannel := make(chan int)
413
414 go func() {
415 s := <-signalChannel
416 switch s {
417 case syscall.SIGHUP,
418 syscall.SIGINT,
419 syscall.SIGTERM,
420 syscall.SIGQUIT:
421 log.Infow("closing-signal-received", log.Fields{"signal": s})
422 exitChannel <- 0
423 default:
424 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
425 exitChannel <- 1
426 }
427 }()
428
429 code := <-exitChannel
430 return code
431}
432
433func printBanner() {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800434 fmt.Println(` ____ ____ _ _______ `)
435 fmt.Println(` / _ \ / __ \| | |__ __|`)
436 fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `)
437 fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `)
438 fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `)
439 fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `)
440 fmt.Println(` | | `)
441 fmt.Println(` |_| `)
442 fmt.Println(` `)
cuilin20187b2a8c32019-03-26 19:52:28 -0700443}
444
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400445func printVersion() {
446 fmt.Println("VOLTHA OpenOLT Adapter")
447 fmt.Println(version.VersionInfo.String(" "))
448}
449
cuilin20187b2a8c32019-03-26 19:52:28 -0700450func main() {
451 start := time.Now()
452
453 cf := config.NewAdapterFlags()
454 cf.ParseCommandArguments()
455
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700456 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700457
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000458 logLevel, err := log.StringToLogLevel(cf.LogLevel)
459 if err != nil {
460 log.Fatalf("Cannot setup logging, %s", err)
461 }
Rohan Agrawal2488f192020-01-31 09:26:55 +0000462
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700463 // Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000464 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700465 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
466 }
467
468 // Update all loggers (provisionned via init) with a common field
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000469 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700470 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
471 }
472
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000473 log.SetAllLogLevel(logLevel)
Rohan Agrawal93bced32020-02-11 10:16:01 +0000474
cuilin20187b2a8c32019-03-26 19:52:28 -0700475 defer log.CleanUp()
476
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400477 // Print version / build information and exit
478 if cf.DisplayVersionOnly {
479 printVersion()
480 return
481 }
482
cuilin20187b2a8c32019-03-26 19:52:28 -0700483 // Print banner if specified
484 if cf.Banner {
485 printBanner()
486 }
487
488 log.Infow("config", log.Fields{"config": *cf})
489
490 ctx, cancel := context.WithCancel(context.Background())
491 defer cancel()
492
493 ad := newAdapter(cf)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000494
495 p := &probe.Probe{}
496 go p.ListenAndServe(fmt.Sprintf("%s:%d", ad.config.ProbeHost, ad.config.ProbePort))
497
498 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
499
500 go ad.start(probeCtx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700501
502 code := waitForExit()
503 log.Infow("received-a-closing-signal", log.Fields{"code": code})
504
505 // Cleanup before leaving
npujarec5762e2020-01-01 14:08:48 +0530506 ad.stop(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700507
508 elapsed := time.Since(start)
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000509 log.Infow("run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700510}