blob: 0e2e186b8dd9337f2f0e42aae3fc3e83eae64e69 [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
Scott Baker51290152019-10-24 14:23:20 -070030 "github.com/opencord/voltha-lib-go/v2/pkg/adapters/adapterif"
kdarapu381c6902019-07-31 18:23:16 +053031
Scott Baker51290152019-10-24 14:23:20 -070032 "github.com/opencord/voltha-lib-go/v2/pkg/adapters"
33 com "github.com/opencord/voltha-lib-go/v2/pkg/adapters/common"
34 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
35 "github.com/opencord/voltha-lib-go/v2/pkg/kafka"
36 "github.com/opencord/voltha-lib-go/v2/pkg/log"
37 "github.com/opencord/voltha-lib-go/v2/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"
Scott Bakerc6e54cb2019-11-04 09:31:25 -080041 ic "github.com/opencord/voltha-protos/v2/go/inter_container"
42 "github.com/opencord/voltha-protos/v2/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
51 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() {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070061 _, _ = log.AddPackage(log.JSON, 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,
127 a.config.OnuNumber,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700128 a.config.KVStoreHost, a.config.KVStorePort, a.config.KVStoreType); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700129 log.Fatal("error-starting-inter-container-proxy")
130 }
131
132 // Register the core request handler
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000133 if err = a.setupRequestHandler(ctx, a.instanceID, a.iAdapter); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700134 log.Fatal("error-setting-core-request-handler")
135 }
136
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700137 // Register this adapter to the Core - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000138 if err = a.registerWithCore(ctx, -1); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700139 log.Fatal("error-registering-with-core")
140 }
cbabu95f21522019-11-13 14:25:18 +0100141
cbabu116b73f2019-12-10 17:56:32 +0530142 // check the readiness and liveliness and update the probe status
143 a.checkServicesReadiness(ctx)
cbabu95f21522019-11-13 14:25:18 +0100144}
145
146/**
147This function checks the liveliness and readiness of the kakfa and kv-client services
148and update the status in the probe.
149*/
cbabu116b73f2019-12-10 17:56:32 +0530150func (a *adapter) checkServicesReadiness(ctx context.Context) {
151 // checks the kafka readiness
152 go a.checkKafkaReadiness(ctx)
153
154 // checks the kv-store readiness
155 go a.checkKvStoreReadiness(ctx)
156}
157
158/**
159This function checks the liveliness and readiness of the kv-store service
160and update the status in the probe.
161*/
162func (a *adapter) checkKvStoreReadiness(ctx context.Context) {
163 // dividing the live probe interval by 2 to get updated status every 30s
164 timeout := a.config.LiveProbeInterval / 2
165 kvStoreChannel := make(chan bool, 1)
166
167 // Default false to check the liveliness.
168 kvStoreChannel <- false
cbabu95f21522019-11-13 14:25:18 +0100169 for {
cbabu116b73f2019-12-10 17:56:32 +0530170 timeoutTimer := time.NewTimer(timeout)
171 select {
172 case liveliness := <-kvStoreChannel:
173 if !liveliness {
174 // kv-store not reachable or down, updating the status to not ready state
175 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady)
176 timeout = a.config.NotLiveProbeInterval
177 } else {
178 // kv-store is reachable , updating the status to running state
179 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning)
180 timeout = a.config.LiveProbeInterval / 2
181 }
182 // Check if the timer has expired or not
183 if !timeoutTimer.Stop() {
184 <-timeoutTimer.C
185 }
186 case <-timeoutTimer.C:
187 // Check the status of the kv-store
188 log.Info("kv-store liveliness-recheck")
189 if a.kvClient.IsConnectionUp(a.config.KVStoreTimeout) {
190 kvStoreChannel <- true
191 } else {
192 kvStoreChannel <- false
193 }
cbabu95f21522019-11-13 14:25:18 +0100194 }
cbabu116b73f2019-12-10 17:56:32 +0530195 }
196}
197
198/**
199This function checks the liveliness and readiness of the kafka service
200and update the status in the probe.
201*/
202func (a *adapter) checkKafkaReadiness(ctx context.Context) {
203 livelinessChannel := a.kafkaClient.EnableLivenessChannel(true)
Scott Baker86fce9a2019-12-12 09:47:17 -0800204 healthinessChannel := a.kafkaClient.EnableHealthinessChannel(true)
cbabu116b73f2019-12-10 17:56:32 +0530205 timeout := a.config.LiveProbeInterval
206 for {
207 timeoutTimer := time.NewTimer(timeout)
208
209 select {
Scott Baker86fce9a2019-12-12 09:47:17 -0800210 case healthiness := <-healthinessChannel:
211 if !healthiness {
212 // log.Fatal will call os.Exit(1) to terminate
213 log.Fatal("Kafka service has become unhealthy")
214 }
cbabu116b73f2019-12-10 17:56:32 +0530215 case liveliness := <-livelinessChannel:
216 if !liveliness {
217 // kafka not reachable or down, updating the status to not ready state
218 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady)
219 timeout = a.config.NotLiveProbeInterval
220 } else {
221 // kafka is reachable , updating the status to running state
222 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning)
223 timeout = a.config.LiveProbeInterval
224 }
225 // Check if the timer has expired or not
226 if !timeoutTimer.Stop() {
227 <-timeoutTimer.C
228 }
229 case <-timeoutTimer.C:
230 log.Info("kafka-proxy-liveness-recheck")
231 // send the liveness probe in a goroutine; we don't want to deadlock ourselves as
232 // the liveness probe may wait (and block) writing to our channel.
233 err := a.kafkaClient.SendLiveness()
234 if err != nil {
235 // Catch possible error case if sending liveness after Sarama has been stopped.
236 log.Warnw("error-kafka-send-liveness", log.Fields{"error": err})
237 }
cbabu95f21522019-11-13 14:25:18 +0100238 }
cbabu95f21522019-11-13 14:25:18 +0100239 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700240}
241
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700242func (a *adapter) stop() {
cuilin20187b2a8c32019-03-26 19:52:28 -0700243 // Stop leadership tracking
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700244 a.halted = true
cuilin20187b2a8c32019-03-26 19:52:28 -0700245
246 // send exit signal
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700247 a.exitChannel <- 0
cuilin20187b2a8c32019-03-26 19:52:28 -0700248
249 // Cleanup - applies only if we had a kvClient
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700250 if a.kvClient != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700251 // Release all reservations
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700252 if err := a.kvClient.ReleaseAllReservations(); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700253 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
254 }
255 // Close the DB connection
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700256 a.kvClient.Close()
cuilin20187b2a8c32019-03-26 19:52:28 -0700257 }
258
259 // TODO: More cleanup
260}
261
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700262func newKVClient(storeType, address string, timeout int) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700263
264 log.Infow("kv-store-type", log.Fields{"store": storeType})
265 switch storeType {
266 case "consul":
267 return kvstore.NewConsulClient(address, timeout)
268 case "etcd":
269 return kvstore.NewEtcdClient(address, timeout)
270 }
271 return nil, errors.New("unsupported-kv-store")
272}
273
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700274func newKafkaClient(clientType, host string, port int) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700275
276 log.Infow("common-client-type", log.Fields{"client": clientType})
277 switch clientType {
278 case "sarama":
279 return kafka.NewSaramaClient(
280 kafka.Host(host),
281 kafka.Port(port),
282 kafka.ProducerReturnOnErrors(true),
283 kafka.ProducerReturnOnSuccess(true),
284 kafka.ProducerMaxRetries(6),
Abhilash S.L3b494632019-07-16 15:51:09 +0530285 kafka.ProducerRetryBackoff(time.Millisecond*30),
286 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700287 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700288
cuilin20187b2a8c32019-03-26 19:52:28 -0700289 return nil, errors.New("unsupported-client-type")
290}
291
292func (a *adapter) setKVClient() error {
293 addr := a.config.KVStoreHost + ":" + strconv.Itoa(a.config.KVStorePort)
294 client, err := newKVClient(a.config.KVStoreType, addr, a.config.KVStoreTimeout)
295 if err != nil {
296 a.kvClient = nil
297 log.Error(err)
298 return err
299 }
300 a.kvClient = client
301 return nil
302}
303
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000304func (a *adapter) startInterContainerProxy(ctx context.Context, retries int) (*kafka.InterContainerProxy, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700305 log.Infow("starting-intercontainer-messaging-proxy", log.Fields{"host": a.config.KafkaAdapterHost,
306 "port": a.config.KafkaAdapterPort, "topic": a.config.Topic})
307 var err error
308 var kip *kafka.InterContainerProxy
309 if kip, err = kafka.NewInterContainerProxy(
310 kafka.InterContainerHost(a.config.KafkaAdapterHost),
311 kafka.InterContainerPort(a.config.KafkaAdapterPort),
312 kafka.MsgClient(a.kafkaClient),
313 kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic})); err != nil {
314 log.Errorw("fail-to-create-common-proxy", log.Fields{"error": err})
315 return nil, err
316 }
317 count := 0
318 for {
319 if err = kip.Start(); err != nil {
320 log.Warnw("error-starting-messaging-proxy", log.Fields{"error": err})
321 if retries == count {
322 return nil, err
323 }
324 count = +1
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700325 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700326 time.Sleep(2 * time.Second)
327 } else {
328 break
329 }
330 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000331 probe.UpdateStatusFromContext(ctx, "container-proxy", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700332 log.Info("common-messaging-proxy-created")
333 return kip, nil
334}
335
kdarapu381c6902019-07-31 18:23:16 +0530336func (a *adapter) startOpenOLT(ctx context.Context, kip *kafka.InterContainerProxy,
337 cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy, onuNumber int, kvStoreHost string,
338 kvStorePort int, KVStoreType string) (*ac.OpenOLT, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700339 log.Info("starting-open-olt")
340 var err error
Devmalya Paulfb990a52019-07-09 10:01:49 -0400341 sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, ep, onuNumber, kvStoreHost, kvStorePort, KVStoreType)
cuilin20187b2a8c32019-03-26 19:52:28 -0700342
343 if err = sOLT.Start(ctx); err != nil {
344 log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err})
345 return nil, err
346 }
347
348 log.Info("open-olt-started")
349 return sOLT, nil
350}
351
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000352func (a *adapter) setupRequestHandler(ctx context.Context, coreInstanceID string, iadapter adapters.IAdapter) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700353 log.Info("setting-request-handler")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700354 requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy)
cuilin20187b2a8c32019-03-26 19:52:28 -0700355 if err := a.kip.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
356 log.Errorw("request-handler-setup-failed", log.Fields{"error": err})
357 return err
358
359 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000360 probe.UpdateStatusFromContext(ctx, "core-request-handler", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700361 log.Info("request-handler-setup-done")
362 return nil
363}
364
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000365func (a *adapter) registerWithCore(ctx context.Context, retries int) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700366 log.Info("registering-with-core")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400367 adapterDescription := &voltha.Adapter{Id: "openolt", // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400368 Vendor: "VOLTHA OpenOLT",
369 Version: version.VersionInfo.Version}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400370 types := []*voltha.DeviceType{{Id: "openolt",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700371 Adapter: "openolt", // Name of the adapter that handles device type
Girish Gowdru0c588b22019-04-23 23:24:56 -0400372 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
373 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700374 deviceTypes := &voltha.DeviceTypes{Items: types}
375 count := 0
376 for {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700377 if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700378 log.Warnw("registering-with-core-failed", log.Fields{"error": err})
379 if retries == count {
380 return err
381 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700382 count++
383 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700384 time.Sleep(2 * time.Second)
385 } else {
386 break
387 }
388 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000389 probe.UpdateStatusFromContext(ctx, "register-with-core", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700390 log.Info("registered-with-core")
391 return nil
392}
393
394func waitForExit() int {
395 signalChannel := make(chan os.Signal, 1)
396 signal.Notify(signalChannel,
397 syscall.SIGHUP,
398 syscall.SIGINT,
399 syscall.SIGTERM,
400 syscall.SIGQUIT)
401
402 exitChannel := make(chan int)
403
404 go func() {
405 s := <-signalChannel
406 switch s {
407 case syscall.SIGHUP,
408 syscall.SIGINT,
409 syscall.SIGTERM,
410 syscall.SIGQUIT:
411 log.Infow("closing-signal-received", log.Fields{"signal": s})
412 exitChannel <- 0
413 default:
414 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
415 exitChannel <- 1
416 }
417 }()
418
419 code := <-exitChannel
420 return code
421}
422
423func printBanner() {
424 fmt.Println(" ____ ____ _ _______ ")
425 fmt.Println(" / _ \\ / __\\| | |__ __|")
426 fmt.Println(" | | | |_ __ ___ _ __ | | | | | | | ")
427 fmt.Println(" | | | | '_\\ / _\\ '_\\ | | | | | | | ")
428 fmt.Println(" | |__| | |_) | __/ | | || |__| | |____| | ")
429 fmt.Println(" \\____/| .__/\\___|_| |_|\\____/|______|_| ")
430 fmt.Println(" | | ")
431 fmt.Println(" |_| ")
432 fmt.Println(" ")
433}
434
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400435func printVersion() {
436 fmt.Println("VOLTHA OpenOLT Adapter")
437 fmt.Println(version.VersionInfo.String(" "))
438}
439
cuilin20187b2a8c32019-03-26 19:52:28 -0700440func main() {
441 start := time.Now()
442
443 cf := config.NewAdapterFlags()
444 cf.ParseCommandArguments()
445
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700446 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700447
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700448 // Setup default logger - applies for packages that do not have specific logger set
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000449 if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700450 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
451 }
452
453 // Update all loggers (provisionned via init) with a common field
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000454 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700455 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
456 }
457
Scott Baker51290152019-10-24 14:23:20 -0700458 log.SetPackageLogLevel("github.com/opencord/voltha-lib-go/v2/pkg/adapters/common", log.DebugLevel)
cuilin20187b2a8c32019-03-26 19:52:28 -0700459
460 defer log.CleanUp()
461
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400462 // Print version / build information and exit
463 if cf.DisplayVersionOnly {
464 printVersion()
465 return
466 }
467
cuilin20187b2a8c32019-03-26 19:52:28 -0700468 // Print banner if specified
469 if cf.Banner {
470 printBanner()
471 }
472
473 log.Infow("config", log.Fields{"config": *cf})
474
475 ctx, cancel := context.WithCancel(context.Background())
476 defer cancel()
477
478 ad := newAdapter(cf)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000479
480 p := &probe.Probe{}
481 go p.ListenAndServe(fmt.Sprintf("%s:%d", ad.config.ProbeHost, ad.config.ProbePort))
482
483 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
484
485 go ad.start(probeCtx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700486
487 code := waitForExit()
488 log.Infow("received-a-closing-signal", log.Fields{"code": code})
489
490 // Cleanup before leaving
491 ad.stop()
492
493 elapsed := time.Since(start)
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000494 log.Infow("run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700495}