blob: 7d8cfe85089b22988a4ae272c541ebe09c9e8f11 [file] [log] [blame]
cuilin20187b2a8c32019-03-26 19:52:28 -07001/*
Joey Armstrongf9bffdf2022-12-27 07:05:28 -05002* Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
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
khenaidoo106c61a2021-08-11 18:05:46 -040029 conf "github.com/opencord/voltha-lib-go/v7/pkg/config"
30 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
31 "github.com/opencord/voltha-lib-go/v7/pkg/events"
32 "github.com/opencord/voltha-lib-go/v7/pkg/events/eventif"
33 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
34 "github.com/opencord/voltha-lib-go/v7/pkg/kafka"
35 "github.com/opencord/voltha-lib-go/v7/pkg/log"
36 "github.com/opencord/voltha-lib-go/v7/pkg/probe"
37 "github.com/opencord/voltha-lib-go/v7/pkg/version"
Scott Bakerdbd960e2020-02-28 08:57:51 -080038 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
39 ac "github.com/opencord/voltha-openolt-adapter/internal/pkg/core"
khenaidoodc2116e2021-10-19 17:33:19 -040040 "github.com/opencord/voltha-protos/v5/go/adapter_service"
41 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
42 "github.com/opencord/voltha-protos/v5/go/core_service"
khenaidoodc2116e2021-10-19 17:33:19 -040043 "github.com/opencord/voltha-protos/v5/go/olt_inter_adapter_service"
khenaidoo106c61a2021-08-11 18:05:46 -040044 "github.com/opencord/voltha-protos/v5/go/voltha"
45 "google.golang.org/grpc"
46)
47
48const (
49 clusterMessagingService = "cluster-message-service"
50 oltAdapterService = "olt-adapter-service"
51 kvService = "kv-service"
52 coreService = "core-service"
cuilin20187b2a8c32019-03-26 19:52:28 -070053)
54
55type adapter struct {
khenaidooefff76e2021-12-15 16:51:30 -050056 instanceID string
57 config *config.AdapterFlags
58 grpcServer *vgrpc.GrpcServer
59 oltAdapter *ac.OpenOLT
60 oltInterAdapter *ac.OpenOLTInterAdapter
61 kafkaClient kafka.Client
62 kvClient kvstore.Client
63 coreClient *vgrpc.Client
64 eventProxy eventif.EventProxy
65 halted bool
66 exitChannel chan int
cuilin20187b2a8c32019-03-26 19:52:28 -070067}
68
cuilin20187b2a8c32019-03-26 19:52:28 -070069func newAdapter(cf *config.AdapterFlags) *adapter {
70 var a adapter
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070071 a.instanceID = cf.InstanceID
cuilin20187b2a8c32019-03-26 19:52:28 -070072 a.config = cf
73 a.halted = false
74 a.exitChannel = make(chan int, 1)
cuilin20187b2a8c32019-03-26 19:52:28 -070075 return &a
76}
77
78func (a *adapter) start(ctx context.Context) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000079 logger.Info(ctx, "Starting Core Adapter components")
cuilin20187b2a8c32019-03-26 19:52:28 -070080 var err error
81
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000082 var p *probe.Probe
83 if value := ctx.Value(probe.ProbeContextKey); value != nil {
84 if _, ok := value.(*probe.Probe); ok {
85 p = value.(*probe.Probe)
86 p.RegisterService(
Neha Sharma96b7bf22020-06-15 10:37:32 +000087 ctx,
khenaidoo106c61a2021-08-11 18:05:46 -040088 clusterMessagingService,
89 kvService,
90 oltAdapterService,
91 coreService,
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000092 )
93 }
94 }
95
cuilin20187b2a8c32019-03-26 19:52:28 -070096 // Setup KV Client
Neha Sharma96b7bf22020-06-15 10:37:32 +000097 logger.Debugw(ctx, "create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
98 if err = a.setKVClient(ctx); err != nil {
99 logger.Fatalw(ctx, "error-setting-kv-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700100 }
101
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000102 if p != nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400103 p.UpdateStatus(ctx, kvService, probe.ServiceStatusRunning)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000104 }
105
divyadesaia37f78b2020-02-07 12:41:22 +0000106 // Setup Log Config
Neha Sharma96b7bf22020-06-15 10:37:32 +0000107 cm := conf.NewConfigManager(ctx, a.kvClient, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout)
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800108
divyadesaid26f6b12020-03-19 06:30:28 +0000109 go conf.StartLogLevelConfigProcessing(cm, ctx)
Girish Kumar935f7af2020-08-18 11:59:42 +0000110 go conf.StartLogFeaturesConfigProcessing(cm, ctx)
divyadesaia37f78b2020-02-07 12:41:22 +0000111
cuilin20187b2a8c32019-03-26 19:52:28 -0700112 // Setup Kafka Client
khenaidoo106c61a2021-08-11 18:05:46 -0400113 if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaClusterAddress); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000114 logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700115 }
116
khenaidoo106c61a2021-08-11 18:05:46 -0400117 // Start kafka communication with the broker
118 if err := kafka.StartAndWaitUntilKafkaConnectionIsUp(ctx, a.kafkaClient, a.config.HeartbeatCheckInterval, clusterMessagingService); err != nil {
119 logger.Fatal(ctx, "unable-to-connect-to-kafka")
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000120 }
121
Devmalya Paulfb990a52019-07-09 10:01:49 -0400122 // Create the event proxy to post events to KAFKA
Himani Chawlacd407802020-12-10 12:08:59 +0530123 a.eventProxy = events.NewEventProxy(events.MsgClient(a.kafkaClient), events.MsgTopic(kafka.Topic{Name: a.config.EventTopic}))
khenaidoo106c61a2021-08-11 18:05:46 -0400124 go func() {
125 if err := a.eventProxy.Start(); err != nil {
126 logger.Fatalw(ctx, "event-proxy-cannot-start", log.Fields{"error": err})
127 }
128 }()
129
130 // Create the Core client to handle requests to the Core. Note that the coreClient is an interface and needs to be
131 // cast to the appropriate grpc client by invoking GetCoreGrpcClient on the a.coreClient
khenaidoo27e7ac92021-12-08 14:43:09 -0500132 if a.coreClient, err = vgrpc.NewClient(
133 a.config.AdapterEndpoint,
134 a.config.CoreEndpoint,
khenaidooefff76e2021-12-15 16:51:30 -0500135 "core_service.CoreService",
khenaidoo27e7ac92021-12-08 14:43:09 -0500136 a.coreRestarted); err != nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400137 logger.Fatal(ctx, "grpc-client-not-created")
138 }
139 // Start the core grpc client
khenaidooefff76e2021-12-15 16:51:30 -0500140 go a.coreClient.Start(ctx, getCoreServiceClientHandler)
Devmalya Paulfb990a52019-07-09 10:01:49 -0400141
cuilin20187b2a8c32019-03-26 19:52:28 -0700142 // Create the open OLT adapter
khenaidoo106c61a2021-08-11 18:05:46 -0400143 if a.oltAdapter, err = a.startOpenOLT(ctx, a.coreClient, a.eventProxy, a.config, cm); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000144 logger.Fatalw(ctx, "error-starting-openolt", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700145 }
146
khenaidooefff76e2021-12-15 16:51:30 -0500147 // Create the open OLT Inter adapter adapter
148 if a.oltInterAdapter, err = a.startOpenOLTInterAdapter(ctx, a.oltAdapter); err != nil {
149 logger.Fatalw(ctx, "error-starting-openolt-inter-adapter", log.Fields{"error": err})
150 }
151
khenaidoo106c61a2021-08-11 18:05:46 -0400152 // Create and start the grpc server
153 a.grpcServer = vgrpc.NewGrpcServer(a.config.GrpcAddress, nil, false, p)
154
155 //Register the adapter service
156 a.addAdapterService(ctx, a.grpcServer, a.oltAdapter)
157
158 //Register the olt inter-adapter service
khenaidooefff76e2021-12-15 16:51:30 -0500159 a.addOltInterAdapterService(ctx, a.grpcServer, a.oltInterAdapter)
khenaidoo106c61a2021-08-11 18:05:46 -0400160
161 // Start the grpc server
162 go a.startGRPCService(ctx, a.grpcServer, oltAdapterService)
cuilin20187b2a8c32019-03-26 19:52:28 -0700163
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700164 // Register this adapter to the Core - retries indefinitely
khenaidoo106c61a2021-08-11 18:05:46 -0400165 if err = a.registerWithCore(ctx, coreService, -1); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000166 logger.Fatal(ctx, "error-registering-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700167 }
cbabu95f21522019-11-13 14:25:18 +0100168
cbabu116b73f2019-12-10 17:56:32 +0530169 // check the readiness and liveliness and update the probe status
170 a.checkServicesReadiness(ctx)
cbabu95f21522019-11-13 14:25:18 +0100171}
172
khenaidoo106c61a2021-08-11 18:05:46 -0400173// TODO: Any action the adapter needs to do following a Core restart?
174func (a *adapter) coreRestarted(ctx context.Context, endPoint string) error {
175 logger.Errorw(ctx, "core-restarted", log.Fields{"endpoint": endPoint})
176 return nil
177}
178
khenaidooefff76e2021-12-15 16:51:30 -0500179// getCoreServiceClientHandler is used to test whether the remote gRPC service is up
180func getCoreServiceClientHandler(ctx context.Context, conn *grpc.ClientConn) interface{} {
181 if conn == nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400182 return nil
183 }
khenaidooefff76e2021-12-15 16:51:30 -0500184 return core_service.NewCoreServiceClient(conn)
khenaidoo106c61a2021-08-11 18:05:46 -0400185}
186
cbabu95f21522019-11-13 14:25:18 +0100187/**
188This function checks the liveliness and readiness of the kakfa and kv-client services
189and update the status in the probe.
190*/
cbabu116b73f2019-12-10 17:56:32 +0530191func (a *adapter) checkServicesReadiness(ctx context.Context) {
192 // checks the kafka readiness
khenaidoo106c61a2021-08-11 18:05:46 -0400193 go kafka.MonitorKafkaReadiness(ctx, a.kafkaClient, a.config.LiveProbeInterval, a.config.NotLiveProbeInterval, clusterMessagingService)
cbabu116b73f2019-12-10 17:56:32 +0530194
195 // checks the kv-store readiness
196 go a.checkKvStoreReadiness(ctx)
197}
198
199/**
200This function checks the liveliness and readiness of the kv-store service
201and update the status in the probe.
202*/
203func (a *adapter) checkKvStoreReadiness(ctx context.Context) {
204 // dividing the live probe interval by 2 to get updated status every 30s
205 timeout := a.config.LiveProbeInterval / 2
206 kvStoreChannel := make(chan bool, 1)
207
Girish Gowdra4b48fa42022-06-01 18:10:08 -0700208 timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second)
209 kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx)
210 cancelFunc()
211
cbabu95f21522019-11-13 14:25:18 +0100212 for {
cbabu116b73f2019-12-10 17:56:32 +0530213 timeoutTimer := time.NewTimer(timeout)
214 select {
215 case liveliness := <-kvStoreChannel:
216 if !liveliness {
217 // kv-store not reachable or down, updating the status to not ready state
khenaidoo106c61a2021-08-11 18:05:46 -0400218 probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusNotReady)
cbabu116b73f2019-12-10 17:56:32 +0530219 timeout = a.config.NotLiveProbeInterval
220 } else {
221 // kv-store is reachable , updating the status to running state
khenaidoo106c61a2021-08-11 18:05:46 -0400222 probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusRunning)
cbabu116b73f2019-12-10 17:56:32 +0530223 timeout = a.config.LiveProbeInterval / 2
224 }
Girish Gowdra4b48fa42022-06-01 18:10:08 -0700225
cbabu116b73f2019-12-10 17:56:32 +0530226 // Check if the timer has expired or not
227 if !timeoutTimer.Stop() {
228 <-timeoutTimer.C
229 }
Girish Gowdra4b48fa42022-06-01 18:10:08 -0700230
cbabu116b73f2019-12-10 17:56:32 +0530231 case <-timeoutTimer.C:
Girish Kumarbeadc112020-02-26 18:41:02 +0000232 // Check the status of the kv-store. Use timeout of 2 seconds to avoid forever blocking
Neha Sharma96b7bf22020-06-15 10:37:32 +0000233 logger.Info(ctx, "kv-store liveliness-recheck")
Girish Kumarbeadc112020-02-26 18:41:02 +0000234 timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second)
235
236 kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx)
237 // Cleanup cancel func resources
238 cancelFunc()
cbabu95f21522019-11-13 14:25:18 +0100239 }
cbabu116b73f2019-12-10 17:56:32 +0530240 }
241}
242
npujarec5762e2020-01-01 14:08:48 +0530243func (a *adapter) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700244 // Stop leadership tracking
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700245 a.halted = true
cuilin20187b2a8c32019-03-26 19:52:28 -0700246
247 // send exit signal
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700248 a.exitChannel <- 0
cuilin20187b2a8c32019-03-26 19:52:28 -0700249
khenaidooefff76e2021-12-15 16:51:30 -0500250 // Stop all grpc processing
251 if err := a.oltAdapter.Stop(ctx); err != nil {
252 logger.Errorw(ctx, "failure-stopping-olt-adapter-service", log.Fields{"error": err, "adapter": a.config.AdapterName})
253 }
254 if err := a.oltInterAdapter.Stop(ctx); err != nil {
255 logger.Errorw(ctx, "failure-stopping-olt-inter-adapter-service", log.Fields{"error": err, "adapter": a.config.AdapterName})
256 }
257
cuilin20187b2a8c32019-03-26 19:52:28 -0700258 // Cleanup - applies only if we had a kvClient
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700259 if a.kvClient != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700260 // Release all reservations
npujarec5762e2020-01-01 14:08:48 +0530261 if err := a.kvClient.ReleaseAllReservations(ctx); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000262 logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700263 }
264 // Close the DB connection
Girish Gowdra4b48fa42022-06-01 18:10:08 -0700265 go a.kvClient.Close(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700266 }
267
khenaidoo106c61a2021-08-11 18:05:46 -0400268 if a.eventProxy != nil {
269 a.eventProxy.Stop()
Scott Bakere701b862020-02-20 16:19:16 -0800270 }
271
khenaidoo106c61a2021-08-11 18:05:46 -0400272 if a.kafkaClient != nil {
273 a.kafkaClient.Stop(ctx)
274 }
275
276 // Stop core client
277 if a.coreClient != nil {
278 a.coreClient.Stop(ctx)
279 }
280
Girish Gowdra4b48fa42022-06-01 18:10:08 -0700281 logger.Info(ctx, "main-stop-processing-complete")
282
khenaidoo106c61a2021-08-11 18:05:46 -0400283 // TODO: Stop child devices connections
284
cuilin20187b2a8c32019-03-26 19:52:28 -0700285 // TODO: More cleanup
286}
287
Neha Sharma96b7bf22020-06-15 10:37:32 +0000288func newKVClient(ctx context.Context, storeType, address string, timeout time.Duration) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700289
Neha Sharma96b7bf22020-06-15 10:37:32 +0000290 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700291 switch storeType {
cuilin20187b2a8c32019-03-26 19:52:28 -0700292 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000293 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
cuilin20187b2a8c32019-03-26 19:52:28 -0700294 }
295 return nil, errors.New("unsupported-kv-store")
296}
297
Neha Sharma96b7bf22020-06-15 10:37:32 +0000298func newKafkaClient(ctx context.Context, clientType, address string) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700299
Neha Sharma96b7bf22020-06-15 10:37:32 +0000300 logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700301 switch clientType {
302 case "sarama":
303 return kafka.NewSaramaClient(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000304 kafka.Address(address),
cuilin20187b2a8c32019-03-26 19:52:28 -0700305 kafka.ProducerReturnOnErrors(true),
306 kafka.ProducerReturnOnSuccess(true),
307 kafka.ProducerMaxRetries(6),
Abhilash S.L3b494632019-07-16 15:51:09 +0530308 kafka.ProducerRetryBackoff(time.Millisecond*30),
309 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700310 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700311
cuilin20187b2a8c32019-03-26 19:52:28 -0700312 return nil, errors.New("unsupported-client-type")
313}
314
Neha Sharma96b7bf22020-06-15 10:37:32 +0000315func (a *adapter) setKVClient(ctx context.Context) error {
316 client, err := newKVClient(ctx, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout)
cuilin20187b2a8c32019-03-26 19:52:28 -0700317 if err != nil {
318 a.kvClient = nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700319 return err
320 }
321 a.kvClient = client
divyadesaia37f78b2020-02-07 12:41:22 +0000322
cuilin20187b2a8c32019-03-26 19:52:28 -0700323 return nil
324}
325
khenaidoo106c61a2021-08-11 18:05:46 -0400326// startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server
327func (a *adapter) startGRPCService(ctx context.Context, server *vgrpc.GrpcServer, serviceName string) {
328 logger.Infow(ctx, "starting-grpc-service", log.Fields{"service": serviceName})
329
330 probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning)
331 logger.Infow(ctx, "grpc-service-started", log.Fields{"service": serviceName})
332
333 server.Start(ctx)
334 probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusStopped)
cuilin20187b2a8c32019-03-26 19:52:28 -0700335}
336
khenaidoodc2116e2021-10-19 17:33:19 -0400337func (a *adapter) addAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler adapter_service.AdapterServiceServer) {
khenaidoo106c61a2021-08-11 18:05:46 -0400338 logger.Info(ctx, "adding-adapter-service")
339
340 server.AddService(func(gs *grpc.Server) {
khenaidoodc2116e2021-10-19 17:33:19 -0400341 adapter_service.RegisterAdapterServiceServer(gs, handler)
khenaidoo106c61a2021-08-11 18:05:46 -0400342 })
343}
344
khenaidoodc2116e2021-10-19 17:33:19 -0400345func (a *adapter) addOltInterAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler olt_inter_adapter_service.OltInterAdapterServiceServer) {
khenaidoo106c61a2021-08-11 18:05:46 -0400346 logger.Info(ctx, "adding-olt-inter-adapter-service")
347
348 server.AddService(func(gs *grpc.Server) {
khenaidoodc2116e2021-10-19 17:33:19 -0400349 olt_inter_adapter_service.RegisterOltInterAdapterServiceServer(gs, handler)
khenaidoo106c61a2021-08-11 18:05:46 -0400350 })
351}
352
353func (a *adapter) startOpenOLT(ctx context.Context, cc *vgrpc.Client, ep eventif.EventProxy,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800354 cfg *config.AdapterFlags, cm *conf.ConfigManager) (*ac.OpenOLT, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000355 logger.Info(ctx, "starting-open-olt")
cuilin20187b2a8c32019-03-26 19:52:28 -0700356 var err error
khenaidoo106c61a2021-08-11 18:05:46 -0400357 sOLT := ac.NewOpenOLT(ctx, cc, ep, cfg, cm)
cuilin20187b2a8c32019-03-26 19:52:28 -0700358
359 if err = sOLT.Start(ctx); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700360 return nil, err
361 }
362
Neha Sharma96b7bf22020-06-15 10:37:32 +0000363 logger.Info(ctx, "open-olt-started")
cuilin20187b2a8c32019-03-26 19:52:28 -0700364 return sOLT, nil
365}
366
khenaidooefff76e2021-12-15 16:51:30 -0500367func (a *adapter) startOpenOLTInterAdapter(ctx context.Context, oo *ac.OpenOLT) (*ac.OpenOLTInterAdapter, error) {
368 logger.Info(ctx, "starting-open-olt-inter-adapter")
369 var err error
370 sOLTInterAdapter := ac.NewOpenOLTInterAdapter(oo)
371
372 if err = sOLTInterAdapter.Start(ctx); err != nil {
373 return nil, err
374 }
375
376 logger.Info(ctx, "open-olt-inter-adapter-started")
377 return sOLTInterAdapter, nil
378}
379
khenaidoo106c61a2021-08-11 18:05:46 -0400380func (a *adapter) registerWithCore(ctx context.Context, serviceName string, retries int) error {
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700381 adapterID := fmt.Sprintf("openolt_%d", a.config.CurrentReplica)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000382 logger.Infow(ctx, "registering-with-core", log.Fields{
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700383 "adapterID": adapterID,
384 "currentReplica": a.config.CurrentReplica,
385 "totalReplicas": a.config.TotalReplicas,
386 })
387 adapterDescription := &voltha.Adapter{
388 Id: adapterID, // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400389 Vendor: "VOLTHA OpenOLT",
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700390 Version: version.VersionInfo.Version,
khenaidoo106c61a2021-08-11 18:05:46 -0400391 // The Endpoint refers to the address this service is listening on.
392 Endpoint: a.config.AdapterEndpoint,
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700393 Type: "openolt",
394 CurrentReplica: int32(a.config.CurrentReplica),
395 TotalReplicas: int32(a.config.TotalReplicas),
396 }
397 types := []*voltha.DeviceType{{
398 Id: "openolt",
khenaidoo106c61a2021-08-11 18:05:46 -0400399 AdapterType: "openolt", // Type of the adapter that handles device type
400 Adapter: "openolt", // Deprecated attribute
Girish Gowdru0c588b22019-04-23 23:24:56 -0400401 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
402 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700403 deviceTypes := &voltha.DeviceTypes{Items: types}
404 count := 0
405 for {
khenaidoo106c61a2021-08-11 18:05:46 -0400406 gClient, err := a.coreClient.GetCoreServiceClient()
407 if gClient != nil {
khenaidoodc2116e2021-10-19 17:33:19 -0400408 if _, err = gClient.RegisterAdapter(log.WithSpanFromContext(context.TODO(), ctx), &ca.AdapterRegistration{
khenaidoo106c61a2021-08-11 18:05:46 -0400409 Adapter: adapterDescription,
410 DTypes: deviceTypes}); err == nil {
411 break
cuilin20187b2a8c32019-03-26 19:52:28 -0700412 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700413 }
khenaidoo106c61a2021-08-11 18:05:46 -0400414 logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"endpoint": a.config.CoreEndpoint, "error": err, "count": count, "gclient": gClient})
415 if retries == count {
416 return err
417 }
418 count++
419 // Take a nap before retrying
420 time.Sleep(2 * time.Second)
cuilin20187b2a8c32019-03-26 19:52:28 -0700421 }
khenaidoo106c61a2021-08-11 18:05:46 -0400422 probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000423 logger.Info(ctx, "registered-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700424 return nil
425}
426
Neha Sharma96b7bf22020-06-15 10:37:32 +0000427func waitForExit(ctx context.Context) int {
cuilin20187b2a8c32019-03-26 19:52:28 -0700428 signalChannel := make(chan os.Signal, 1)
429 signal.Notify(signalChannel,
430 syscall.SIGHUP,
431 syscall.SIGINT,
432 syscall.SIGTERM,
433 syscall.SIGQUIT)
434
435 exitChannel := make(chan int)
436
437 go func() {
438 s := <-signalChannel
439 switch s {
440 case syscall.SIGHUP,
441 syscall.SIGINT,
442 syscall.SIGTERM,
443 syscall.SIGQUIT:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000444 logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700445 exitChannel <- 0
446 default:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000447 logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700448 exitChannel <- 1
449 }
450 }()
451
452 code := <-exitChannel
453 return code
454}
455
456func printBanner() {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800457 fmt.Println(` ____ ____ _ _______ `)
458 fmt.Println(` / _ \ / __ \| | |__ __|`)
459 fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `)
460 fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `)
461 fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `)
462 fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `)
463 fmt.Println(` | | `)
464 fmt.Println(` |_| `)
465 fmt.Println(` `)
cuilin20187b2a8c32019-03-26 19:52:28 -0700466}
467
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400468func printVersion() {
469 fmt.Println("VOLTHA OpenOLT Adapter")
470 fmt.Println(version.VersionInfo.String(" "))
471}
472
cuilin20187b2a8c32019-03-26 19:52:28 -0700473func main() {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000474 ctx := context.Background()
cuilin20187b2a8c32019-03-26 19:52:28 -0700475 start := time.Now()
476
477 cf := config.NewAdapterFlags()
478 cf.ParseCommandArguments()
479
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700480 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700481
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000482 logLevel, err := log.StringToLogLevel(cf.LogLevel)
483 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000484 logger.Fatalf(ctx, "Cannot setup logging, %s", err)
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000485 }
Rohan Agrawal2488f192020-01-31 09:26:55 +0000486
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700487 // Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000488 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
Girish Kumara1ea2aa2020-08-19 18:14:22 +0000489 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
cuilin20187b2a8c32019-03-26 19:52:28 -0700490 }
491
492 // Update all loggers (provisionned via init) with a common field
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000493 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
Girish Kumara1ea2aa2020-08-19 18:14:22 +0000494 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
cuilin20187b2a8c32019-03-26 19:52:28 -0700495 }
496
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000497 log.SetAllLogLevel(logLevel)
Rohan Agrawal93bced32020-02-11 10:16:01 +0000498
Matteo Scandolo8f2b9572020-02-28 15:35:23 -0800499 realMain()
500
Kent Hagermane6ff1012020-07-14 15:07:53 -0400501 defer func() {
502 err := log.CleanUp()
503 if err != nil {
504 logger.Errorw(context.Background(), "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
505 }
506 }()
cuilin20187b2a8c32019-03-26 19:52:28 -0700507
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400508 // Print version / build information and exit
509 if cf.DisplayVersionOnly {
510 printVersion()
511 return
512 }
513
cuilin20187b2a8c32019-03-26 19:52:28 -0700514 // Print banner if specified
515 if cf.Banner {
516 printBanner()
517 }
518
Neha Sharma96b7bf22020-06-15 10:37:32 +0000519 logger.Infow(ctx, "config", log.Fields{"config": *cf})
cuilin20187b2a8c32019-03-26 19:52:28 -0700520
521 ctx, cancel := context.WithCancel(context.Background())
522 defer cancel()
523
524 ad := newAdapter(cf)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000525
526 p := &probe.Probe{}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000527 go p.ListenAndServe(ctx, ad.config.ProbeAddress)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000528
529 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
530
Girish Kumar935f7af2020-08-18 11:59:42 +0000531 closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled)
Girish Kumar11e15972020-06-15 14:51:10 +0000532 if err != nil {
533 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
534 } else {
535 defer log.TerminateTracing(closer)
536 }
537
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000538 go ad.start(probeCtx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700539
Neha Sharma96b7bf22020-06-15 10:37:32 +0000540 code := waitForExit(ctx)
541 logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code})
cuilin20187b2a8c32019-03-26 19:52:28 -0700542
Girish Gowdra4b48fa42022-06-01 18:10:08 -0700543 // Use context with cancel as etcd-client stop could take more time sometimes to stop slowing down container shutdown.
544 ctxWithCancel, cancelFunc := context.WithCancel(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700545 // Cleanup before leaving
Girish Gowdra4b48fa42022-06-01 18:10:08 -0700546 ad.stop(ctxWithCancel)
547 // Will halt any long-running stop routine gracefully
548 cancelFunc()
cuilin20187b2a8c32019-03-26 19:52:28 -0700549
550 elapsed := time.Since(start)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000551 logger.Infow(ctx, "run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700552}