blob: 318ca7fc2687606891df7c81f78db29cbd2defcf [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"
divyadesaia37f78b2020-02-07 12:41:22 +000034 conf "github.com/opencord/voltha-lib-go/v3/pkg/config"
Esin Karamanccb714b2019-11-29 15:02:06 +000035 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
36 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
37 "github.com/opencord/voltha-lib-go/v3/pkg/log"
38 "github.com/opencord/voltha-lib-go/v3/pkg/probe"
Scott Bakerdbd960e2020-02-28 08:57:51 -080039 "github.com/opencord/voltha-lib-go/v3/pkg/version"
40 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
41 ac "github.com/opencord/voltha-openolt-adapter/internal/pkg/core"
Esin Karamanccb714b2019-11-29 15:02:06 +000042 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
43 "github.com/opencord/voltha-protos/v3/go/voltha"
cuilin20187b2a8c32019-03-26 19:52:28 -070044)
45
46type adapter struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070047 instanceID string
cuilin20187b2a8c32019-03-26 19:52:28 -070048 config *config.AdapterFlags
49 iAdapter adapters.IAdapter
50 kafkaClient kafka.Client
51 kvClient kvstore.Client
npujarec5762e2020-01-01 14:08:48 +053052 kip kafka.InterContainerProxy
kdarapu381c6902019-07-31 18:23:16 +053053 coreProxy adapterif.CoreProxy
54 adapterProxy adapterif.AdapterProxy
55 eventProxy adapterif.EventProxy
cuilin20187b2a8c32019-03-26 19:52:28 -070056 halted bool
57 exitChannel chan int
58 receiverChannels []<-chan *ic.InterContainerMessage
59}
60
61func init() {
Matteo Scandolo945e4012019-12-12 14:16:11 -080062 _, _ = log.AddPackage(log.CONSOLE, log.DebugLevel, nil)
cuilin20187b2a8c32019-03-26 19:52:28 -070063}
64
65func newAdapter(cf *config.AdapterFlags) *adapter {
66 var a adapter
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070067 a.instanceID = cf.InstanceID
cuilin20187b2a8c32019-03-26 19:52:28 -070068 a.config = cf
69 a.halted = false
70 a.exitChannel = make(chan int, 1)
71 a.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
72 return &a
73}
74
75func (a *adapter) start(ctx context.Context) {
76 log.Info("Starting Core Adapter components")
77 var err error
78
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000079 var p *probe.Probe
80 if value := ctx.Value(probe.ProbeContextKey); value != nil {
81 if _, ok := value.(*probe.Probe); ok {
82 p = value.(*probe.Probe)
83 p.RegisterService(
84 "message-bus",
85 "kv-store",
86 "container-proxy",
87 "core-request-handler",
88 "register-with-core",
89 )
90 }
91 }
92
cuilin20187b2a8c32019-03-26 19:52:28 -070093 // Setup KV Client
94 log.Debugw("create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070095 if err = a.setKVClient(); err != nil {
Girish Kumarbeadc112020-02-26 18:41:02 +000096 log.Fatalw("error-setting-kv-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -070097 }
98
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000099 if p != nil {
100 p.UpdateStatus("kv-store", probe.ServiceStatusRunning)
101 }
102
divyadesaia37f78b2020-02-07 12:41:22 +0000103 // Setup Log Config
104 cm := conf.NewConfigManager(a.kvClient, a.config.KVStoreType, a.config.KVStoreHost, a.config.KVStorePort, a.config.KVStoreTimeout)
divyadesaid26f6b12020-03-19 06:30:28 +0000105 go conf.StartLogLevelConfigProcessing(cm, ctx)
divyadesaia37f78b2020-02-07 12:41:22 +0000106
cuilin20187b2a8c32019-03-26 19:52:28 -0700107 // Setup Kafka Client
108 if a.kafkaClient, err = newKafkaClient("sarama", a.config.KafkaAdapterHost, a.config.KafkaAdapterPort); err != nil {
Girish Kumarbeadc112020-02-26 18:41:02 +0000109 log.Fatalw("Unsupported-common-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700110 }
111
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000112 if p != nil {
113 p.UpdateStatus("message-bus", probe.ServiceStatusRunning)
114 }
115
cuilin20187b2a8c32019-03-26 19:52:28 -0700116 // Start the common InterContainer Proxy - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000117 if a.kip, err = a.startInterContainerProxy(ctx, -1); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700118 log.Fatal("error-starting-inter-container-proxy")
119 }
120
121 // Create the core proxy to handle requests to the Core
122 a.coreProxy = com.NewCoreProxy(a.kip, a.config.Topic, a.config.CoreTopic)
123
124 // Create the adaptor proxy to handle request between olt and onu
125 a.adapterProxy = com.NewAdapterProxy(a.kip, "brcm_openomci_onu", a.config.CoreTopic)
126
Devmalya Paulfb990a52019-07-09 10:01:49 -0400127 // Create the event proxy to post events to KAFKA
128 a.eventProxy = com.NewEventProxy(com.MsgClient(a.kafkaClient), com.MsgTopic(kafka.Topic{Name: a.config.EventTopic}))
129
cuilin20187b2a8c32019-03-26 19:52:28 -0700130 // Create the open OLT adapter
Girish Kumarf26e4882020-03-05 06:49:10 +0000131 if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy, a.config); err != nil {
132 log.Fatalw("error-starting-openolt", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700133 }
134
135 // Register the core request handler
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000136 if err = a.setupRequestHandler(ctx, a.instanceID, a.iAdapter); err != nil {
Girish Kumarf26e4882020-03-05 06:49:10 +0000137 log.Fatalw("error-setting-core-request-handler", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700138 }
139
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700140 // Register this adapter to the Core - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000141 if err = a.registerWithCore(ctx, -1); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700142 log.Fatal("error-registering-with-core")
143 }
cbabu95f21522019-11-13 14:25:18 +0100144
cbabu116b73f2019-12-10 17:56:32 +0530145 // check the readiness and liveliness and update the probe status
146 a.checkServicesReadiness(ctx)
cbabu95f21522019-11-13 14:25:18 +0100147}
148
149/**
150This function checks the liveliness and readiness of the kakfa and kv-client services
151and update the status in the probe.
152*/
cbabu116b73f2019-12-10 17:56:32 +0530153func (a *adapter) checkServicesReadiness(ctx context.Context) {
154 // checks the kafka readiness
155 go a.checkKafkaReadiness(ctx)
156
157 // checks the kv-store readiness
158 go a.checkKvStoreReadiness(ctx)
159}
160
161/**
162This function checks the liveliness and readiness of the kv-store service
163and update the status in the probe.
164*/
165func (a *adapter) checkKvStoreReadiness(ctx context.Context) {
166 // dividing the live probe interval by 2 to get updated status every 30s
167 timeout := a.config.LiveProbeInterval / 2
168 kvStoreChannel := make(chan bool, 1)
169
170 // Default false to check the liveliness.
171 kvStoreChannel <- false
cbabu95f21522019-11-13 14:25:18 +0100172 for {
cbabu116b73f2019-12-10 17:56:32 +0530173 timeoutTimer := time.NewTimer(timeout)
174 select {
175 case liveliness := <-kvStoreChannel:
176 if !liveliness {
177 // kv-store not reachable or down, updating the status to not ready state
178 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady)
179 timeout = a.config.NotLiveProbeInterval
180 } else {
181 // kv-store is reachable , updating the status to running state
182 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning)
183 timeout = a.config.LiveProbeInterval / 2
184 }
185 // Check if the timer has expired or not
186 if !timeoutTimer.Stop() {
187 <-timeoutTimer.C
188 }
189 case <-timeoutTimer.C:
Girish Kumarbeadc112020-02-26 18:41:02 +0000190 // Check the status of the kv-store. Use timeout of 2 seconds to avoid forever blocking
cbabu116b73f2019-12-10 17:56:32 +0530191 log.Info("kv-store liveliness-recheck")
Girish Kumarbeadc112020-02-26 18:41:02 +0000192 timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second)
193
194 kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx)
195 // Cleanup cancel func resources
196 cancelFunc()
cbabu95f21522019-11-13 14:25:18 +0100197 }
cbabu116b73f2019-12-10 17:56:32 +0530198 }
199}
200
201/**
202This function checks the liveliness and readiness of the kafka service
203and update the status in the probe.
204*/
205func (a *adapter) checkKafkaReadiness(ctx context.Context) {
206 livelinessChannel := a.kafkaClient.EnableLivenessChannel(true)
Scott Baker86fce9a2019-12-12 09:47:17 -0800207 healthinessChannel := a.kafkaClient.EnableHealthinessChannel(true)
cbabu116b73f2019-12-10 17:56:32 +0530208 timeout := a.config.LiveProbeInterval
Scott Bakere701b862020-02-20 16:19:16 -0800209 failed := false
cbabu116b73f2019-12-10 17:56:32 +0530210 for {
211 timeoutTimer := time.NewTimer(timeout)
212
213 select {
Scott Baker86fce9a2019-12-12 09:47:17 -0800214 case healthiness := <-healthinessChannel:
215 if !healthiness {
Scott Bakere701b862020-02-20 16:19:16 -0800216 // This will eventually cause K8s to restart the container, and will do
217 // so in a way that allows cleanup to continue, rather than an immediate
218 // panic and exit here.
219 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusFailed)
220 failed = true
221 }
222 // Check if the timer has expired or not
223 if !timeoutTimer.Stop() {
224 <-timeoutTimer.C
Scott Baker86fce9a2019-12-12 09:47:17 -0800225 }
cbabu116b73f2019-12-10 17:56:32 +0530226 case liveliness := <-livelinessChannel:
Scott Bakere701b862020-02-20 16:19:16 -0800227 if failed {
228 // Failures of the message bus are permanent and can't ever be recovered from,
229 // so make sure we never inadvertently reset a failed state back to unready.
230 } else if !liveliness {
cbabu116b73f2019-12-10 17:56:32 +0530231 // kafka not reachable or down, updating the status to not ready state
232 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady)
233 timeout = a.config.NotLiveProbeInterval
234 } else {
235 // kafka is reachable , updating the status to running state
236 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning)
237 timeout = a.config.LiveProbeInterval
238 }
239 // Check if the timer has expired or not
240 if !timeoutTimer.Stop() {
241 <-timeoutTimer.C
242 }
243 case <-timeoutTimer.C:
244 log.Info("kafka-proxy-liveness-recheck")
245 // send the liveness probe in a goroutine; we don't want to deadlock ourselves as
246 // the liveness probe may wait (and block) writing to our channel.
247 err := a.kafkaClient.SendLiveness()
248 if err != nil {
249 // Catch possible error case if sending liveness after Sarama has been stopped.
250 log.Warnw("error-kafka-send-liveness", log.Fields{"error": err})
251 }
cbabu95f21522019-11-13 14:25:18 +0100252 }
cbabu95f21522019-11-13 14:25:18 +0100253 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700254}
255
npujarec5762e2020-01-01 14:08:48 +0530256func (a *adapter) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700257 // Stop leadership tracking
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700258 a.halted = true
cuilin20187b2a8c32019-03-26 19:52:28 -0700259
260 // send exit signal
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700261 a.exitChannel <- 0
cuilin20187b2a8c32019-03-26 19:52:28 -0700262
263 // Cleanup - applies only if we had a kvClient
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700264 if a.kvClient != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700265 // Release all reservations
npujarec5762e2020-01-01 14:08:48 +0530266 if err := a.kvClient.ReleaseAllReservations(ctx); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700267 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
268 }
269 // Close the DB connection
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700270 a.kvClient.Close()
cuilin20187b2a8c32019-03-26 19:52:28 -0700271 }
272
Scott Bakere701b862020-02-20 16:19:16 -0800273 if a.kip != nil {
274 a.kip.Stop()
275 }
276
cuilin20187b2a8c32019-03-26 19:52:28 -0700277 // TODO: More cleanup
278}
279
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700280func newKVClient(storeType, address string, timeout int) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700281
282 log.Infow("kv-store-type", log.Fields{"store": storeType})
283 switch storeType {
284 case "consul":
285 return kvstore.NewConsulClient(address, timeout)
286 case "etcd":
287 return kvstore.NewEtcdClient(address, timeout)
288 }
289 return nil, errors.New("unsupported-kv-store")
290}
291
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700292func newKafkaClient(clientType, host string, port int) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700293
294 log.Infow("common-client-type", log.Fields{"client": clientType})
295 switch clientType {
296 case "sarama":
297 return kafka.NewSaramaClient(
298 kafka.Host(host),
299 kafka.Port(port),
300 kafka.ProducerReturnOnErrors(true),
301 kafka.ProducerReturnOnSuccess(true),
302 kafka.ProducerMaxRetries(6),
Abhilash S.L3b494632019-07-16 15:51:09 +0530303 kafka.ProducerRetryBackoff(time.Millisecond*30),
304 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700305 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700306
cuilin20187b2a8c32019-03-26 19:52:28 -0700307 return nil, errors.New("unsupported-client-type")
308}
309
310func (a *adapter) setKVClient() error {
311 addr := a.config.KVStoreHost + ":" + strconv.Itoa(a.config.KVStorePort)
312 client, err := newKVClient(a.config.KVStoreType, addr, a.config.KVStoreTimeout)
313 if err != nil {
314 a.kvClient = nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700315 return err
316 }
317 a.kvClient = client
divyadesaia37f78b2020-02-07 12:41:22 +0000318
cuilin20187b2a8c32019-03-26 19:52:28 -0700319 return nil
320}
321
npujarec5762e2020-01-01 14:08:48 +0530322func (a *adapter) startInterContainerProxy(ctx context.Context, retries int) (kafka.InterContainerProxy, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700323 log.Infow("starting-intercontainer-messaging-proxy", log.Fields{"host": a.config.KafkaAdapterHost,
324 "port": a.config.KafkaAdapterPort, "topic": a.config.Topic})
325 var err error
npujarec5762e2020-01-01 14:08:48 +0530326 kip := kafka.NewInterContainerProxy(
cuilin20187b2a8c32019-03-26 19:52:28 -0700327 kafka.InterContainerHost(a.config.KafkaAdapterHost),
328 kafka.InterContainerPort(a.config.KafkaAdapterPort),
329 kafka.MsgClient(a.kafkaClient),
npujarec5762e2020-01-01 14:08:48 +0530330 kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic}))
cuilin20187b2a8c32019-03-26 19:52:28 -0700331 count := 0
332 for {
333 if err = kip.Start(); err != nil {
334 log.Warnw("error-starting-messaging-proxy", log.Fields{"error": err})
335 if retries == count {
336 return nil, err
337 }
338 count = +1
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700339 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700340 time.Sleep(2 * time.Second)
341 } else {
342 break
343 }
344 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000345 probe.UpdateStatusFromContext(ctx, "container-proxy", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700346 log.Info("common-messaging-proxy-created")
347 return kip, nil
348}
349
npujarec5762e2020-01-01 14:08:48 +0530350func (a *adapter) startOpenOLT(ctx context.Context, kip kafka.InterContainerProxy,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530351 cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy,
352 cfg *config.AdapterFlags) (*ac.OpenOLT, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700353 log.Info("starting-open-olt")
354 var err error
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530355 sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, ep, cfg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700356
357 if err = sOLT.Start(ctx); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700358 return nil, err
359 }
360
361 log.Info("open-olt-started")
362 return sOLT, nil
363}
364
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000365func (a *adapter) setupRequestHandler(ctx context.Context, coreInstanceID string, iadapter adapters.IAdapter) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700366 log.Info("setting-request-handler")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700367 requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy)
cuilin20187b2a8c32019-03-26 19:52:28 -0700368 if err := a.kip.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700369 return err
370
371 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000372 probe.UpdateStatusFromContext(ctx, "core-request-handler", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700373 log.Info("request-handler-setup-done")
374 return nil
375}
376
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000377func (a *adapter) registerWithCore(ctx context.Context, retries int) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700378 log.Info("registering-with-core")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400379 adapterDescription := &voltha.Adapter{Id: "openolt", // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400380 Vendor: "VOLTHA OpenOLT",
381 Version: version.VersionInfo.Version}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400382 types := []*voltha.DeviceType{{Id: "openolt",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700383 Adapter: "openolt", // Name of the adapter that handles device type
Girish Gowdru0c588b22019-04-23 23:24:56 -0400384 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
385 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700386 deviceTypes := &voltha.DeviceTypes{Items: types}
387 count := 0
388 for {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700389 if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700390 log.Warnw("registering-with-core-failed", log.Fields{"error": err})
391 if retries == count {
392 return err
393 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700394 count++
395 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700396 time.Sleep(2 * time.Second)
397 } else {
398 break
399 }
400 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000401 probe.UpdateStatusFromContext(ctx, "register-with-core", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700402 log.Info("registered-with-core")
403 return nil
404}
405
406func waitForExit() int {
407 signalChannel := make(chan os.Signal, 1)
408 signal.Notify(signalChannel,
409 syscall.SIGHUP,
410 syscall.SIGINT,
411 syscall.SIGTERM,
412 syscall.SIGQUIT)
413
414 exitChannel := make(chan int)
415
416 go func() {
417 s := <-signalChannel
418 switch s {
419 case syscall.SIGHUP,
420 syscall.SIGINT,
421 syscall.SIGTERM,
422 syscall.SIGQUIT:
423 log.Infow("closing-signal-received", log.Fields{"signal": s})
424 exitChannel <- 0
425 default:
426 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
427 exitChannel <- 1
428 }
429 }()
430
431 code := <-exitChannel
432 return code
433}
434
435func printBanner() {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800436 fmt.Println(` ____ ____ _ _______ `)
437 fmt.Println(` / _ \ / __ \| | |__ __|`)
438 fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `)
439 fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `)
440 fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `)
441 fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `)
442 fmt.Println(` | | `)
443 fmt.Println(` |_| `)
444 fmt.Println(` `)
cuilin20187b2a8c32019-03-26 19:52:28 -0700445}
446
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400447func printVersion() {
448 fmt.Println("VOLTHA OpenOLT Adapter")
449 fmt.Println(version.VersionInfo.String(" "))
450}
451
cuilin20187b2a8c32019-03-26 19:52:28 -0700452func main() {
453 start := time.Now()
454
455 cf := config.NewAdapterFlags()
456 cf.ParseCommandArguments()
457
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700458 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700459
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000460 logLevel, err := log.StringToLogLevel(cf.LogLevel)
461 if err != nil {
462 log.Fatalf("Cannot setup logging, %s", err)
463 }
Rohan Agrawal2488f192020-01-31 09:26:55 +0000464
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700465 // Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000466 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700467 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
468 }
469
470 // Update all loggers (provisionned via init) with a common field
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000471 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700472 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
473 }
474
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000475 log.SetAllLogLevel(logLevel)
Rohan Agrawal93bced32020-02-11 10:16:01 +0000476
Matteo Scandolo8f2b9572020-02-28 15:35:23 -0800477 realMain()
478
cuilin20187b2a8c32019-03-26 19:52:28 -0700479 defer log.CleanUp()
480
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400481 // Print version / build information and exit
482 if cf.DisplayVersionOnly {
483 printVersion()
484 return
485 }
486
cuilin20187b2a8c32019-03-26 19:52:28 -0700487 // Print banner if specified
488 if cf.Banner {
489 printBanner()
490 }
491
492 log.Infow("config", log.Fields{"config": *cf})
493
494 ctx, cancel := context.WithCancel(context.Background())
495 defer cancel()
496
497 ad := newAdapter(cf)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000498
499 p := &probe.Probe{}
500 go p.ListenAndServe(fmt.Sprintf("%s:%d", ad.config.ProbeHost, ad.config.ProbePort))
501
502 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
503
504 go ad.start(probeCtx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700505
506 code := waitForExit()
507 log.Infow("received-a-closing-signal", log.Fields{"code": code})
508
509 // Cleanup before leaving
npujarec5762e2020-01-01 14:08:48 +0530510 ad.stop(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700511
512 elapsed := time.Since(start)
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000513 log.Infow("run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700514}