blob: 14c47d25daf16081857f00f0cc2fe49e6ede8f2a [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"
kdarapu381c6902019-07-31 18:23:16 +053026 "syscall"
27 "time"
28
Esin Karamanccb714b2019-11-29 15:02:06 +000029 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
kdarapu381c6902019-07-31 18:23:16 +053030
Esin Karamanccb714b2019-11-29 15:02:06 +000031 "github.com/opencord/voltha-lib-go/v3/pkg/adapters"
32 com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common"
divyadesaia37f78b2020-02-07 12:41:22 +000033 conf "github.com/opencord/voltha-lib-go/v3/pkg/config"
Esin Karamanccb714b2019-11-29 15:02:06 +000034 "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"
Scott Bakerdbd960e2020-02-28 08:57:51 -080038 "github.com/opencord/voltha-lib-go/v3/pkg/version"
39 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
40 ac "github.com/opencord/voltha-openolt-adapter/internal/pkg/core"
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
cuilin20187b2a8c32019-03-26 19:52:28 -070060func newAdapter(cf *config.AdapterFlags) *adapter {
61 var a adapter
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070062 a.instanceID = cf.InstanceID
cuilin20187b2a8c32019-03-26 19:52:28 -070063 a.config = cf
64 a.halted = false
65 a.exitChannel = make(chan int, 1)
66 a.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
67 return &a
68}
69
70func (a *adapter) start(ctx context.Context) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000071 logger.Info(ctx, "Starting Core Adapter components")
cuilin20187b2a8c32019-03-26 19:52:28 -070072 var err error
73
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000074 var p *probe.Probe
75 if value := ctx.Value(probe.ProbeContextKey); value != nil {
76 if _, ok := value.(*probe.Probe); ok {
77 p = value.(*probe.Probe)
78 p.RegisterService(
Neha Sharma96b7bf22020-06-15 10:37:32 +000079 ctx,
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000080 "message-bus",
81 "kv-store",
82 "container-proxy",
83 "core-request-handler",
84 "register-with-core",
85 )
86 }
87 }
88
cuilin20187b2a8c32019-03-26 19:52:28 -070089 // Setup KV Client
Neha Sharma96b7bf22020-06-15 10:37:32 +000090 logger.Debugw(ctx, "create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
91 if err = a.setKVClient(ctx); err != nil {
92 logger.Fatalw(ctx, "error-setting-kv-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -070093 }
94
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000095 if p != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +000096 p.UpdateStatus(ctx, "kv-store", probe.ServiceStatusRunning)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000097 }
98
divyadesaia37f78b2020-02-07 12:41:22 +000099 // Setup Log Config
Neha Sharma96b7bf22020-06-15 10:37:32 +0000100 cm := conf.NewConfigManager(ctx, a.kvClient, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout)
divyadesaid26f6b12020-03-19 06:30:28 +0000101 go conf.StartLogLevelConfigProcessing(cm, ctx)
divyadesaia37f78b2020-02-07 12:41:22 +0000102
cuilin20187b2a8c32019-03-26 19:52:28 -0700103 // Setup Kafka Client
Neha Sharma96b7bf22020-06-15 10:37:32 +0000104 if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaAdapterAddress); err != nil {
105 logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700106 }
107
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000108 if p != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000109 p.UpdateStatus(ctx, "message-bus", probe.ServiceStatusRunning)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000110 }
111
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700112 // setup endpointManager
113
cuilin20187b2a8c32019-03-26 19:52:28 -0700114 // Start the common InterContainer Proxy - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000115 if a.kip, err = a.startInterContainerProxy(ctx, -1); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000116 logger.Fatal(ctx, "error-starting-inter-container-proxy")
cuilin20187b2a8c32019-03-26 19:52:28 -0700117 }
118
119 // Create the core proxy to handle requests to the Core
Neha Sharma96b7bf22020-06-15 10:37:32 +0000120 a.coreProxy = com.NewCoreProxy(ctx, a.kip, a.config.Topic, a.config.CoreTopic)
cuilin20187b2a8c32019-03-26 19:52:28 -0700121
122 // Create the adaptor proxy to handle request between olt and onu
Matteo Scandolo46654682020-08-05 11:46:37 -0700123 a.adapterProxy = com.NewAdapterProxy(ctx, a.kip, a.config.CoreTopic, cm.Backend)
cuilin20187b2a8c32019-03-26 19:52:28 -0700124
Devmalya Paulfb990a52019-07-09 10:01:49 -0400125 // Create the event proxy to post events to KAFKA
126 a.eventProxy = com.NewEventProxy(com.MsgClient(a.kafkaClient), com.MsgTopic(kafka.Topic{Name: a.config.EventTopic}))
127
cuilin20187b2a8c32019-03-26 19:52:28 -0700128 // Create the open OLT adapter
Girish Kumarf26e4882020-03-05 06:49:10 +0000129 if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy, a.config); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000130 logger.Fatalw(ctx, "error-starting-openolt", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700131 }
132
133 // Register the core request handler
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000134 if err = a.setupRequestHandler(ctx, a.instanceID, a.iAdapter); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000135 logger.Fatalw(ctx, "error-setting-core-request-handler", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700136 }
137
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700138 // Register this adapter to the Core - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000139 if err = a.registerWithCore(ctx, -1); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000140 logger.Fatal(ctx, "error-registering-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700141 }
cbabu95f21522019-11-13 14:25:18 +0100142
cbabu116b73f2019-12-10 17:56:32 +0530143 // check the readiness and liveliness and update the probe status
144 a.checkServicesReadiness(ctx)
cbabu95f21522019-11-13 14:25:18 +0100145}
146
147/**
148This function checks the liveliness and readiness of the kakfa and kv-client services
149and update the status in the probe.
150*/
cbabu116b73f2019-12-10 17:56:32 +0530151func (a *adapter) checkServicesReadiness(ctx context.Context) {
152 // checks the kafka readiness
153 go a.checkKafkaReadiness(ctx)
154
155 // checks the kv-store readiness
156 go a.checkKvStoreReadiness(ctx)
157}
158
159/**
160This function checks the liveliness and readiness of the kv-store service
161and update the status in the probe.
162*/
163func (a *adapter) checkKvStoreReadiness(ctx context.Context) {
164 // dividing the live probe interval by 2 to get updated status every 30s
165 timeout := a.config.LiveProbeInterval / 2
166 kvStoreChannel := make(chan bool, 1)
167
168 // Default false to check the liveliness.
169 kvStoreChannel <- false
cbabu95f21522019-11-13 14:25:18 +0100170 for {
cbabu116b73f2019-12-10 17:56:32 +0530171 timeoutTimer := time.NewTimer(timeout)
172 select {
173 case liveliness := <-kvStoreChannel:
174 if !liveliness {
175 // kv-store not reachable or down, updating the status to not ready state
176 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady)
177 timeout = a.config.NotLiveProbeInterval
178 } else {
179 // kv-store is reachable , updating the status to running state
180 probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning)
181 timeout = a.config.LiveProbeInterval / 2
182 }
183 // Check if the timer has expired or not
184 if !timeoutTimer.Stop() {
185 <-timeoutTimer.C
186 }
187 case <-timeoutTimer.C:
Girish Kumarbeadc112020-02-26 18:41:02 +0000188 // Check the status of the kv-store. Use timeout of 2 seconds to avoid forever blocking
Neha Sharma96b7bf22020-06-15 10:37:32 +0000189 logger.Info(ctx, "kv-store liveliness-recheck")
Girish Kumarbeadc112020-02-26 18:41:02 +0000190 timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second)
191
192 kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx)
193 // Cleanup cancel func resources
194 cancelFunc()
cbabu95f21522019-11-13 14:25:18 +0100195 }
cbabu116b73f2019-12-10 17:56:32 +0530196 }
197}
198
199/**
200This function checks the liveliness and readiness of the kafka service
201and update the status in the probe.
202*/
203func (a *adapter) checkKafkaReadiness(ctx context.Context) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000204 livelinessChannel := a.kafkaClient.EnableLivenessChannel(ctx, true)
205 healthinessChannel := a.kafkaClient.EnableHealthinessChannel(ctx, true)
cbabu116b73f2019-12-10 17:56:32 +0530206 timeout := a.config.LiveProbeInterval
Scott Bakere701b862020-02-20 16:19:16 -0800207 failed := false
cbabu116b73f2019-12-10 17:56:32 +0530208 for {
209 timeoutTimer := time.NewTimer(timeout)
210
211 select {
Scott Baker86fce9a2019-12-12 09:47:17 -0800212 case healthiness := <-healthinessChannel:
213 if !healthiness {
Scott Bakere701b862020-02-20 16:19:16 -0800214 // This will eventually cause K8s to restart the container, and will do
215 // so in a way that allows cleanup to continue, rather than an immediate
216 // panic and exit here.
217 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusFailed)
218 failed = true
219 }
220 // Check if the timer has expired or not
221 if !timeoutTimer.Stop() {
222 <-timeoutTimer.C
Scott Baker86fce9a2019-12-12 09:47:17 -0800223 }
cbabu116b73f2019-12-10 17:56:32 +0530224 case liveliness := <-livelinessChannel:
Scott Bakere701b862020-02-20 16:19:16 -0800225 if failed {
226 // Failures of the message bus are permanent and can't ever be recovered from,
227 // so make sure we never inadvertently reset a failed state back to unready.
228 } else if !liveliness {
cbabu116b73f2019-12-10 17:56:32 +0530229 // kafka not reachable or down, updating the status to not ready state
230 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady)
231 timeout = a.config.NotLiveProbeInterval
232 } else {
233 // kafka is reachable , updating the status to running state
234 probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning)
235 timeout = a.config.LiveProbeInterval
236 }
237 // Check if the timer has expired or not
238 if !timeoutTimer.Stop() {
239 <-timeoutTimer.C
240 }
241 case <-timeoutTimer.C:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000242 logger.Info(ctx, "kafka-proxy-liveness-recheck")
cbabu116b73f2019-12-10 17:56:32 +0530243 // send the liveness probe in a goroutine; we don't want to deadlock ourselves as
244 // the liveness probe may wait (and block) writing to our channel.
Neha Sharma96b7bf22020-06-15 10:37:32 +0000245 err := a.kafkaClient.SendLiveness(ctx)
cbabu116b73f2019-12-10 17:56:32 +0530246 if err != nil {
247 // Catch possible error case if sending liveness after Sarama has been stopped.
Neha Sharma96b7bf22020-06-15 10:37:32 +0000248 logger.Warnw(ctx, "error-kafka-send-liveness", log.Fields{"error": err})
cbabu116b73f2019-12-10 17:56:32 +0530249 }
cbabu95f21522019-11-13 14:25:18 +0100250 }
cbabu95f21522019-11-13 14:25:18 +0100251 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700252}
253
npujarec5762e2020-01-01 14:08:48 +0530254func (a *adapter) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700255 // Stop leadership tracking
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700256 a.halted = true
cuilin20187b2a8c32019-03-26 19:52:28 -0700257
258 // send exit signal
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700259 a.exitChannel <- 0
cuilin20187b2a8c32019-03-26 19:52:28 -0700260
261 // Cleanup - applies only if we had a kvClient
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700262 if a.kvClient != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700263 // Release all reservations
npujarec5762e2020-01-01 14:08:48 +0530264 if err := a.kvClient.ReleaseAllReservations(ctx); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000265 logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700266 }
267 // Close the DB connection
Neha Sharma96b7bf22020-06-15 10:37:32 +0000268 a.kvClient.Close(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700269 }
270
Scott Bakere701b862020-02-20 16:19:16 -0800271 if a.kip != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000272 a.kip.Stop(ctx)
Scott Bakere701b862020-02-20 16:19:16 -0800273 }
274
cuilin20187b2a8c32019-03-26 19:52:28 -0700275 // TODO: More cleanup
276}
277
Neha Sharma96b7bf22020-06-15 10:37:32 +0000278func newKVClient(ctx context.Context, storeType, address string, timeout time.Duration) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700279
Neha Sharma96b7bf22020-06-15 10:37:32 +0000280 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700281 switch storeType {
282 case "consul":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000283 return kvstore.NewConsulClient(ctx, address, timeout)
cuilin20187b2a8c32019-03-26 19:52:28 -0700284 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000285 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
cuilin20187b2a8c32019-03-26 19:52:28 -0700286 }
287 return nil, errors.New("unsupported-kv-store")
288}
289
Neha Sharma96b7bf22020-06-15 10:37:32 +0000290func newKafkaClient(ctx context.Context, clientType, address string) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700291
Neha Sharma96b7bf22020-06-15 10:37:32 +0000292 logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700293 switch clientType {
294 case "sarama":
295 return kafka.NewSaramaClient(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000296 kafka.Address(address),
cuilin20187b2a8c32019-03-26 19:52:28 -0700297 kafka.ProducerReturnOnErrors(true),
298 kafka.ProducerReturnOnSuccess(true),
299 kafka.ProducerMaxRetries(6),
Abhilash S.L3b494632019-07-16 15:51:09 +0530300 kafka.ProducerRetryBackoff(time.Millisecond*30),
301 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700302 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700303
cuilin20187b2a8c32019-03-26 19:52:28 -0700304 return nil, errors.New("unsupported-client-type")
305}
306
Neha Sharma96b7bf22020-06-15 10:37:32 +0000307func (a *adapter) setKVClient(ctx context.Context) error {
308 client, err := newKVClient(ctx, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout)
cuilin20187b2a8c32019-03-26 19:52:28 -0700309 if err != nil {
310 a.kvClient = nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700311 return err
312 }
313 a.kvClient = client
divyadesaia37f78b2020-02-07 12:41:22 +0000314
cuilin20187b2a8c32019-03-26 19:52:28 -0700315 return nil
316}
317
npujarec5762e2020-01-01 14:08:48 +0530318func (a *adapter) startInterContainerProxy(ctx context.Context, retries int) (kafka.InterContainerProxy, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000319 logger.Infow(ctx, "starting-intercontainer-messaging-proxy", log.Fields{"address": a.config.KafkaAdapterAddress,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000320 "topic": a.config.Topic})
cuilin20187b2a8c32019-03-26 19:52:28 -0700321 var err error
npujarec5762e2020-01-01 14:08:48 +0530322 kip := kafka.NewInterContainerProxy(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000323 kafka.InterContainerAddress(a.config.KafkaAdapterAddress),
cuilin20187b2a8c32019-03-26 19:52:28 -0700324 kafka.MsgClient(a.kafkaClient),
npujarec5762e2020-01-01 14:08:48 +0530325 kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic}))
cuilin20187b2a8c32019-03-26 19:52:28 -0700326 count := 0
327 for {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000328 if err = kip.Start(ctx); err != nil {
329 logger.Warnw(ctx, "error-starting-messaging-proxy", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700330 if retries == count {
331 return nil, err
332 }
333 count = +1
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700334 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700335 time.Sleep(2 * time.Second)
336 } else {
337 break
338 }
339 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000340 probe.UpdateStatusFromContext(ctx, "container-proxy", probe.ServiceStatusRunning)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000341 logger.Info(ctx, "common-messaging-proxy-created")
cuilin20187b2a8c32019-03-26 19:52:28 -0700342 return kip, nil
343}
344
npujarec5762e2020-01-01 14:08:48 +0530345func (a *adapter) startOpenOLT(ctx context.Context, kip kafka.InterContainerProxy,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530346 cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy,
347 cfg *config.AdapterFlags) (*ac.OpenOLT, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000348 logger.Info(ctx, "starting-open-olt")
cuilin20187b2a8c32019-03-26 19:52:28 -0700349 var err error
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530350 sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, ep, cfg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700351
352 if err = sOLT.Start(ctx); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700353 return nil, err
354 }
355
Neha Sharma96b7bf22020-06-15 10:37:32 +0000356 logger.Info(ctx, "open-olt-started")
cuilin20187b2a8c32019-03-26 19:52:28 -0700357 return sOLT, nil
358}
359
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000360func (a *adapter) setupRequestHandler(ctx context.Context, coreInstanceID string, iadapter adapters.IAdapter) error {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000361 logger.Info(ctx, "setting-request-handler")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700362 requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000363 if err := a.kip.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700364 return err
365
366 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000367 probe.UpdateStatusFromContext(ctx, "core-request-handler", probe.ServiceStatusRunning)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000368 logger.Info(ctx, "request-handler-setup-done")
cuilin20187b2a8c32019-03-26 19:52:28 -0700369 return nil
370}
371
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000372func (a *adapter) registerWithCore(ctx context.Context, retries int) error {
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700373 adapterID := fmt.Sprintf("openolt_%d", a.config.CurrentReplica)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000374 logger.Infow(ctx, "registering-with-core", log.Fields{
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700375 "adapterID": adapterID,
376 "currentReplica": a.config.CurrentReplica,
377 "totalReplicas": a.config.TotalReplicas,
378 })
379 adapterDescription := &voltha.Adapter{
380 Id: adapterID, // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400381 Vendor: "VOLTHA OpenOLT",
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700382 Version: version.VersionInfo.Version,
383 // TODO once we'll be ready to support multiple versions of the OpenOLT adapter
384 // the Endpoint will have to change to `openolt_<currentReplica`>
serkant.uluderya5e3528d2020-05-22 19:31:07 -0700385 Endpoint: a.config.Topic,
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700386 Type: "openolt",
387 CurrentReplica: int32(a.config.CurrentReplica),
388 TotalReplicas: int32(a.config.TotalReplicas),
389 }
390 types := []*voltha.DeviceType{{
391 Id: "openolt",
392 Adapter: "openolt", // Type of the adapter that handles device type
Girish Gowdru0c588b22019-04-23 23:24:56 -0400393 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
394 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700395 deviceTypes := &voltha.DeviceTypes{Items: types}
396 count := 0
397 for {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700398 if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000399 logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700400 if retries == count {
401 return err
402 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700403 count++
404 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700405 time.Sleep(2 * time.Second)
406 } else {
407 break
408 }
409 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000410 probe.UpdateStatusFromContext(ctx, "register-with-core", probe.ServiceStatusRunning)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000411 logger.Info(ctx, "registered-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700412 return nil
413}
414
Neha Sharma96b7bf22020-06-15 10:37:32 +0000415func waitForExit(ctx context.Context) int {
cuilin20187b2a8c32019-03-26 19:52:28 -0700416 signalChannel := make(chan os.Signal, 1)
417 signal.Notify(signalChannel,
418 syscall.SIGHUP,
419 syscall.SIGINT,
420 syscall.SIGTERM,
421 syscall.SIGQUIT)
422
423 exitChannel := make(chan int)
424
425 go func() {
426 s := <-signalChannel
427 switch s {
428 case syscall.SIGHUP,
429 syscall.SIGINT,
430 syscall.SIGTERM,
431 syscall.SIGQUIT:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000432 logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700433 exitChannel <- 0
434 default:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000435 logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700436 exitChannel <- 1
437 }
438 }()
439
440 code := <-exitChannel
441 return code
442}
443
444func printBanner() {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800445 fmt.Println(` ____ ____ _ _______ `)
446 fmt.Println(` / _ \ / __ \| | |__ __|`)
447 fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `)
448 fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `)
449 fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `)
450 fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `)
451 fmt.Println(` | | `)
452 fmt.Println(` |_| `)
453 fmt.Println(` `)
cuilin20187b2a8c32019-03-26 19:52:28 -0700454}
455
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400456func printVersion() {
457 fmt.Println("VOLTHA OpenOLT Adapter")
458 fmt.Println(version.VersionInfo.String(" "))
459}
460
cuilin20187b2a8c32019-03-26 19:52:28 -0700461func main() {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000462 ctx := context.Background()
cuilin20187b2a8c32019-03-26 19:52:28 -0700463 start := time.Now()
464
465 cf := config.NewAdapterFlags()
466 cf.ParseCommandArguments()
467
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700468 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700469
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000470 logLevel, err := log.StringToLogLevel(cf.LogLevel)
471 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000472 logger.Fatalf(ctx, "Cannot setup logging, %s", err)
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000473 }
Rohan Agrawal2488f192020-01-31 09:26:55 +0000474
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700475 // Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000476 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700477 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
478 }
479
480 // Update all loggers (provisionned via init) with a common field
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000481 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700482 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
483 }
484
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000485 log.SetAllLogLevel(logLevel)
Rohan Agrawal93bced32020-02-11 10:16:01 +0000486
Matteo Scandolo8f2b9572020-02-28 15:35:23 -0800487 realMain()
488
Kent Hagermane6ff1012020-07-14 15:07:53 -0400489 defer func() {
490 err := log.CleanUp()
491 if err != nil {
492 logger.Errorw(context.Background(), "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
493 }
494 }()
cuilin20187b2a8c32019-03-26 19:52:28 -0700495
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400496 // Print version / build information and exit
497 if cf.DisplayVersionOnly {
498 printVersion()
499 return
500 }
501
cuilin20187b2a8c32019-03-26 19:52:28 -0700502 // Print banner if specified
503 if cf.Banner {
504 printBanner()
505 }
506
Neha Sharma96b7bf22020-06-15 10:37:32 +0000507 logger.Infow(ctx, "config", log.Fields{"config": *cf})
cuilin20187b2a8c32019-03-26 19:52:28 -0700508
509 ctx, cancel := context.WithCancel(context.Background())
510 defer cancel()
511
512 ad := newAdapter(cf)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000513
514 p := &probe.Probe{}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000515 go p.ListenAndServe(ctx, ad.config.ProbeAddress)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000516
517 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
518
Girish Kumar11e15972020-06-15 14:51:10 +0000519 closer, err := log.InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled)
520 if err != nil {
521 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
522 } else {
523 defer log.TerminateTracing(closer)
524 }
525
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000526 go ad.start(probeCtx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700527
Neha Sharma96b7bf22020-06-15 10:37:32 +0000528 code := waitForExit(ctx)
529 logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code})
cuilin20187b2a8c32019-03-26 19:52:28 -0700530
531 // Cleanup before leaving
npujarec5762e2020-01-01 14:08:48 +0530532 ad.stop(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700533
534 elapsed := time.Since(start)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000535 logger.Infow(ctx, "run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700536}