blob: 86e9549de9361469907f73c273e95321cdb78dff [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
Girish Gowdraa09aeab2020-09-14 16:30:52 -070029 "github.com/opencord/voltha-lib-go/v4/pkg/adapters/adapterif"
kdarapu381c6902019-07-31 18:23:16 +053030
Girish Gowdraa09aeab2020-09-14 16:30:52 -070031 "github.com/opencord/voltha-lib-go/v4/pkg/adapters"
32 com "github.com/opencord/voltha-lib-go/v4/pkg/adapters/common"
33 conf "github.com/opencord/voltha-lib-go/v4/pkg/config"
34 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
35 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
36 "github.com/opencord/voltha-lib-go/v4/pkg/log"
37 "github.com/opencord/voltha-lib-go/v4/pkg/probe"
38 "github.com/opencord/voltha-lib-go/v4/pkg/version"
Scott Bakerdbd960e2020-02-28 08:57:51 -080039 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
40 ac "github.com/opencord/voltha-openolt-adapter/internal/pkg/core"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070041 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
42 "github.com/opencord/voltha-protos/v4/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)
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800101
divyadesaid26f6b12020-03-19 06:30:28 +0000102 go conf.StartLogLevelConfigProcessing(cm, ctx)
Girish Kumar935f7af2020-08-18 11:59:42 +0000103 go conf.StartLogFeaturesConfigProcessing(cm, ctx)
divyadesaia37f78b2020-02-07 12:41:22 +0000104
cuilin20187b2a8c32019-03-26 19:52:28 -0700105 // Setup Kafka Client
Neha Sharma96b7bf22020-06-15 10:37:32 +0000106 if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaAdapterAddress); err != nil {
107 logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700108 }
109
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000110 if p != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000111 p.UpdateStatus(ctx, "message-bus", probe.ServiceStatusRunning)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000112 }
113
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700114 // setup endpointManager
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 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000118 logger.Fatal(ctx, "error-starting-inter-container-proxy")
cuilin20187b2a8c32019-03-26 19:52:28 -0700119 }
120
121 // Create the core proxy to handle requests to the Core
Neha Sharma96b7bf22020-06-15 10:37:32 +0000122 a.coreProxy = com.NewCoreProxy(ctx, a.kip, a.config.Topic, a.config.CoreTopic)
cuilin20187b2a8c32019-03-26 19:52:28 -0700123
124 // Create the adaptor proxy to handle request between olt and onu
Matteo Scandolo46654682020-08-05 11:46:37 -0700125 a.adapterProxy = com.NewAdapterProxy(ctx, a.kip, a.config.CoreTopic, cm.Backend)
cuilin20187b2a8c32019-03-26 19:52:28 -0700126
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
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800131 if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy, a.config, cm); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000132 logger.Fatalw(ctx, "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 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000137 logger.Fatalw(ctx, "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 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000142 logger.Fatal(ctx, "error-registering-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700143 }
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
Neha Sharma96b7bf22020-06-15 10:37:32 +0000191 logger.Info(ctx, "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) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000206 livelinessChannel := a.kafkaClient.EnableLivenessChannel(ctx, true)
207 healthinessChannel := a.kafkaClient.EnableHealthinessChannel(ctx, 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:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000244 logger.Info(ctx, "kafka-proxy-liveness-recheck")
cbabu116b73f2019-12-10 17:56:32 +0530245 // 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.
Neha Sharma96b7bf22020-06-15 10:37:32 +0000247 err := a.kafkaClient.SendLiveness(ctx)
cbabu116b73f2019-12-10 17:56:32 +0530248 if err != nil {
249 // Catch possible error case if sending liveness after Sarama has been stopped.
Neha Sharma96b7bf22020-06-15 10:37:32 +0000250 logger.Warnw(ctx, "error-kafka-send-liveness", log.Fields{"error": err})
cbabu116b73f2019-12-10 17:56:32 +0530251 }
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 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000267 logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700268 }
269 // Close the DB connection
Neha Sharma96b7bf22020-06-15 10:37:32 +0000270 a.kvClient.Close(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700271 }
272
Scott Bakere701b862020-02-20 16:19:16 -0800273 if a.kip != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000274 a.kip.Stop(ctx)
Scott Bakere701b862020-02-20 16:19:16 -0800275 }
276
cuilin20187b2a8c32019-03-26 19:52:28 -0700277 // TODO: More cleanup
278}
279
Neha Sharma96b7bf22020-06-15 10:37:32 +0000280func newKVClient(ctx context.Context, storeType, address string, timeout time.Duration) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700281
Neha Sharma96b7bf22020-06-15 10:37:32 +0000282 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700283 switch storeType {
284 case "consul":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000285 return kvstore.NewConsulClient(ctx, address, timeout)
cuilin20187b2a8c32019-03-26 19:52:28 -0700286 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000287 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
cuilin20187b2a8c32019-03-26 19:52:28 -0700288 }
289 return nil, errors.New("unsupported-kv-store")
290}
291
Neha Sharma96b7bf22020-06-15 10:37:32 +0000292func newKafkaClient(ctx context.Context, clientType, address string) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700293
Neha Sharma96b7bf22020-06-15 10:37:32 +0000294 logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700295 switch clientType {
296 case "sarama":
297 return kafka.NewSaramaClient(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000298 kafka.Address(address),
cuilin20187b2a8c32019-03-26 19:52:28 -0700299 kafka.ProducerReturnOnErrors(true),
300 kafka.ProducerReturnOnSuccess(true),
301 kafka.ProducerMaxRetries(6),
Abhilash S.L3b494632019-07-16 15:51:09 +0530302 kafka.ProducerRetryBackoff(time.Millisecond*30),
303 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700304 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700305
cuilin20187b2a8c32019-03-26 19:52:28 -0700306 return nil, errors.New("unsupported-client-type")
307}
308
Neha Sharma96b7bf22020-06-15 10:37:32 +0000309func (a *adapter) setKVClient(ctx context.Context) error {
310 client, err := newKVClient(ctx, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout)
cuilin20187b2a8c32019-03-26 19:52:28 -0700311 if err != nil {
312 a.kvClient = nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700313 return err
314 }
315 a.kvClient = client
divyadesaia37f78b2020-02-07 12:41:22 +0000316
cuilin20187b2a8c32019-03-26 19:52:28 -0700317 return nil
318}
319
npujarec5762e2020-01-01 14:08:48 +0530320func (a *adapter) startInterContainerProxy(ctx context.Context, retries int) (kafka.InterContainerProxy, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000321 logger.Infow(ctx, "starting-intercontainer-messaging-proxy", log.Fields{"address": a.config.KafkaAdapterAddress,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000322 "topic": a.config.Topic})
cuilin20187b2a8c32019-03-26 19:52:28 -0700323 var err error
npujarec5762e2020-01-01 14:08:48 +0530324 kip := kafka.NewInterContainerProxy(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000325 kafka.InterContainerAddress(a.config.KafkaAdapterAddress),
cuilin20187b2a8c32019-03-26 19:52:28 -0700326 kafka.MsgClient(a.kafkaClient),
npujarec5762e2020-01-01 14:08:48 +0530327 kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic}))
cuilin20187b2a8c32019-03-26 19:52:28 -0700328 count := 0
329 for {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000330 if err = kip.Start(ctx); err != nil {
331 logger.Warnw(ctx, "error-starting-messaging-proxy", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700332 if retries == count {
333 return nil, err
334 }
335 count = +1
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700336 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700337 time.Sleep(2 * time.Second)
338 } else {
339 break
340 }
341 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000342 probe.UpdateStatusFromContext(ctx, "container-proxy", probe.ServiceStatusRunning)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000343 logger.Info(ctx, "common-messaging-proxy-created")
cuilin20187b2a8c32019-03-26 19:52:28 -0700344 return kip, nil
345}
346
npujarec5762e2020-01-01 14:08:48 +0530347func (a *adapter) startOpenOLT(ctx context.Context, kip kafka.InterContainerProxy,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530348 cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800349 cfg *config.AdapterFlags, cm *conf.ConfigManager) (*ac.OpenOLT, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000350 logger.Info(ctx, "starting-open-olt")
cuilin20187b2a8c32019-03-26 19:52:28 -0700351 var err error
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800352 sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, ep, cfg, cm)
cuilin20187b2a8c32019-03-26 19:52:28 -0700353
354 if err = sOLT.Start(ctx); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700355 return nil, err
356 }
357
Neha Sharma96b7bf22020-06-15 10:37:32 +0000358 logger.Info(ctx, "open-olt-started")
cuilin20187b2a8c32019-03-26 19:52:28 -0700359 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 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000363 logger.Info(ctx, "setting-request-handler")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000365 if err := a.kip.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700366 return err
367
368 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000369 probe.UpdateStatusFromContext(ctx, "core-request-handler", probe.ServiceStatusRunning)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000370 logger.Info(ctx, "request-handler-setup-done")
cuilin20187b2a8c32019-03-26 19:52:28 -0700371 return nil
372}
373
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000374func (a *adapter) registerWithCore(ctx context.Context, retries int) error {
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700375 adapterID := fmt.Sprintf("openolt_%d", a.config.CurrentReplica)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000376 logger.Infow(ctx, "registering-with-core", log.Fields{
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700377 "adapterID": adapterID,
378 "currentReplica": a.config.CurrentReplica,
379 "totalReplicas": a.config.TotalReplicas,
380 })
381 adapterDescription := &voltha.Adapter{
382 Id: adapterID, // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400383 Vendor: "VOLTHA OpenOLT",
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700384 Version: version.VersionInfo.Version,
385 // TODO once we'll be ready to support multiple versions of the OpenOLT adapter
386 // the Endpoint will have to change to `openolt_<currentReplica`>
serkant.uluderya5e3528d2020-05-22 19:31:07 -0700387 Endpoint: a.config.Topic,
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700388 Type: "openolt",
389 CurrentReplica: int32(a.config.CurrentReplica),
390 TotalReplicas: int32(a.config.TotalReplicas),
391 }
392 types := []*voltha.DeviceType{{
393 Id: "openolt",
394 Adapter: "openolt", // Type of the adapter that handles device type
Girish Gowdru0c588b22019-04-23 23:24:56 -0400395 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
396 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700397 deviceTypes := &voltha.DeviceTypes{Items: types}
398 count := 0
399 for {
Neha Sharma8f4e4322020-08-06 10:51:53 +0000400 if err := a.coreProxy.RegisterAdapter(log.WithSpanFromContext(context.TODO(), ctx), adapterDescription, deviceTypes); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000401 logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700402 if retries == count {
403 return err
404 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700405 count++
406 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700407 time.Sleep(2 * time.Second)
408 } else {
409 break
410 }
411 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000412 probe.UpdateStatusFromContext(ctx, "register-with-core", probe.ServiceStatusRunning)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000413 logger.Info(ctx, "registered-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700414 return nil
415}
416
Neha Sharma96b7bf22020-06-15 10:37:32 +0000417func waitForExit(ctx context.Context) int {
cuilin20187b2a8c32019-03-26 19:52:28 -0700418 signalChannel := make(chan os.Signal, 1)
419 signal.Notify(signalChannel,
420 syscall.SIGHUP,
421 syscall.SIGINT,
422 syscall.SIGTERM,
423 syscall.SIGQUIT)
424
425 exitChannel := make(chan int)
426
427 go func() {
428 s := <-signalChannel
429 switch s {
430 case syscall.SIGHUP,
431 syscall.SIGINT,
432 syscall.SIGTERM,
433 syscall.SIGQUIT:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000434 logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700435 exitChannel <- 0
436 default:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000437 logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700438 exitChannel <- 1
439 }
440 }()
441
442 code := <-exitChannel
443 return code
444}
445
446func printBanner() {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800447 fmt.Println(` ____ ____ _ _______ `)
448 fmt.Println(` / _ \ / __ \| | |__ __|`)
449 fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `)
450 fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `)
451 fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `)
452 fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `)
453 fmt.Println(` | | `)
454 fmt.Println(` |_| `)
455 fmt.Println(` `)
cuilin20187b2a8c32019-03-26 19:52:28 -0700456}
457
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400458func printVersion() {
459 fmt.Println("VOLTHA OpenOLT Adapter")
460 fmt.Println(version.VersionInfo.String(" "))
461}
462
cuilin20187b2a8c32019-03-26 19:52:28 -0700463func main() {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000464 ctx := context.Background()
cuilin20187b2a8c32019-03-26 19:52:28 -0700465 start := time.Now()
466
467 cf := config.NewAdapterFlags()
468 cf.ParseCommandArguments()
469
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700470 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700471
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000472 logLevel, err := log.StringToLogLevel(cf.LogLevel)
473 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000474 logger.Fatalf(ctx, "Cannot setup logging, %s", err)
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000475 }
Rohan Agrawal2488f192020-01-31 09:26:55 +0000476
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700477 // Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000478 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
Girish Kumara1ea2aa2020-08-19 18:14:22 +0000479 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
cuilin20187b2a8c32019-03-26 19:52:28 -0700480 }
481
482 // Update all loggers (provisionned via init) with a common field
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000483 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
Girish Kumara1ea2aa2020-08-19 18:14:22 +0000484 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
cuilin20187b2a8c32019-03-26 19:52:28 -0700485 }
486
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000487 log.SetAllLogLevel(logLevel)
Rohan Agrawal93bced32020-02-11 10:16:01 +0000488
Matteo Scandolo8f2b9572020-02-28 15:35:23 -0800489 realMain()
490
Kent Hagermane6ff1012020-07-14 15:07:53 -0400491 defer func() {
492 err := log.CleanUp()
493 if err != nil {
494 logger.Errorw(context.Background(), "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
495 }
496 }()
cuilin20187b2a8c32019-03-26 19:52:28 -0700497
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400498 // Print version / build information and exit
499 if cf.DisplayVersionOnly {
500 printVersion()
501 return
502 }
503
cuilin20187b2a8c32019-03-26 19:52:28 -0700504 // Print banner if specified
505 if cf.Banner {
506 printBanner()
507 }
508
Neha Sharma96b7bf22020-06-15 10:37:32 +0000509 logger.Infow(ctx, "config", log.Fields{"config": *cf})
cuilin20187b2a8c32019-03-26 19:52:28 -0700510
511 ctx, cancel := context.WithCancel(context.Background())
512 defer cancel()
513
514 ad := newAdapter(cf)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000515
516 p := &probe.Probe{}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000517 go p.ListenAndServe(ctx, ad.config.ProbeAddress)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000518
519 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
520
Girish Kumar935f7af2020-08-18 11:59:42 +0000521 closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled)
Girish Kumar11e15972020-06-15 14:51:10 +0000522 if err != nil {
523 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
524 } else {
525 defer log.TerminateTracing(closer)
526 }
527
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000528 go ad.start(probeCtx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700529
Neha Sharma96b7bf22020-06-15 10:37:32 +0000530 code := waitForExit(ctx)
531 logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code})
cuilin20187b2a8c32019-03-26 19:52:28 -0700532
533 // Cleanup before leaving
npujarec5762e2020-01-01 14:08:48 +0530534 ad.stop(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700535
536 elapsed := time.Since(start)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000537 logger.Infow(ctx, "run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700538}