blob: 434fc8382d795f0867efde9799e7e6b03b44d041 [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
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"
khenaidoo27e7ac92021-12-08 14:43:09 -050041 "github.com/opencord/voltha-protos/v5/go/common"
khenaidoodc2116e2021-10-19 17:33:19 -040042 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
43 "github.com/opencord/voltha-protos/v5/go/core_service"
44 "github.com/opencord/voltha-protos/v5/go/health"
45 "github.com/opencord/voltha-protos/v5/go/olt_inter_adapter_service"
khenaidoo106c61a2021-08-11 18:05:46 -040046 "github.com/opencord/voltha-protos/v5/go/voltha"
47 "google.golang.org/grpc"
48)
49
50const (
51 clusterMessagingService = "cluster-message-service"
52 oltAdapterService = "olt-adapter-service"
53 kvService = "kv-service"
54 coreService = "core-service"
cuilin20187b2a8c32019-03-26 19:52:28 -070055)
56
57type adapter struct {
khenaidoo106c61a2021-08-11 18:05:46 -040058 instanceID string
59 config *config.AdapterFlags
60 grpcServer *vgrpc.GrpcServer
61 oltAdapter *ac.OpenOLT
62 kafkaClient kafka.Client
63 kvClient kvstore.Client
64 coreClient *vgrpc.Client
65 eventProxy eventif.EventProxy
66 halted bool
67 exitChannel chan int
cuilin20187b2a8c32019-03-26 19:52:28 -070068}
69
cuilin20187b2a8c32019-03-26 19:52:28 -070070func newAdapter(cf *config.AdapterFlags) *adapter {
71 var a adapter
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070072 a.instanceID = cf.InstanceID
cuilin20187b2a8c32019-03-26 19:52:28 -070073 a.config = cf
74 a.halted = false
75 a.exitChannel = make(chan int, 1)
cuilin20187b2a8c32019-03-26 19:52:28 -070076 return &a
77}
78
79func (a *adapter) start(ctx context.Context) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000080 logger.Info(ctx, "Starting Core Adapter components")
cuilin20187b2a8c32019-03-26 19:52:28 -070081 var err error
82
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000083 var p *probe.Probe
84 if value := ctx.Value(probe.ProbeContextKey); value != nil {
85 if _, ok := value.(*probe.Probe); ok {
86 p = value.(*probe.Probe)
87 p.RegisterService(
Neha Sharma96b7bf22020-06-15 10:37:32 +000088 ctx,
khenaidoo106c61a2021-08-11 18:05:46 -040089 clusterMessagingService,
90 kvService,
91 oltAdapterService,
92 coreService,
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000093 )
94 }
95 }
96
cuilin20187b2a8c32019-03-26 19:52:28 -070097 // Setup KV Client
Neha Sharma96b7bf22020-06-15 10:37:32 +000098 logger.Debugw(ctx, "create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
99 if err = a.setKVClient(ctx); err != nil {
100 logger.Fatalw(ctx, "error-setting-kv-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700101 }
102
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000103 if p != nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400104 p.UpdateStatus(ctx, kvService, probe.ServiceStatusRunning)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000105 }
106
divyadesaia37f78b2020-02-07 12:41:22 +0000107 // Setup Log Config
Neha Sharma96b7bf22020-06-15 10:37:32 +0000108 cm := conf.NewConfigManager(ctx, a.kvClient, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout)
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800109
divyadesaid26f6b12020-03-19 06:30:28 +0000110 go conf.StartLogLevelConfigProcessing(cm, ctx)
Girish Kumar935f7af2020-08-18 11:59:42 +0000111 go conf.StartLogFeaturesConfigProcessing(cm, ctx)
divyadesaia37f78b2020-02-07 12:41:22 +0000112
cuilin20187b2a8c32019-03-26 19:52:28 -0700113 // Setup Kafka Client
khenaidoo106c61a2021-08-11 18:05:46 -0400114 if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaClusterAddress); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000115 logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700116 }
117
khenaidoo106c61a2021-08-11 18:05:46 -0400118 // Start kafka communication with the broker
119 if err := kafka.StartAndWaitUntilKafkaConnectionIsUp(ctx, a.kafkaClient, a.config.HeartbeatCheckInterval, clusterMessagingService); err != nil {
120 logger.Fatal(ctx, "unable-to-connect-to-kafka")
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000121 }
122
Devmalya Paulfb990a52019-07-09 10:01:49 -0400123 // Create the event proxy to post events to KAFKA
Himani Chawlacd407802020-12-10 12:08:59 +0530124 a.eventProxy = events.NewEventProxy(events.MsgClient(a.kafkaClient), events.MsgTopic(kafka.Topic{Name: a.config.EventTopic}))
khenaidoo106c61a2021-08-11 18:05:46 -0400125 go func() {
126 if err := a.eventProxy.Start(); err != nil {
127 logger.Fatalw(ctx, "event-proxy-cannot-start", log.Fields{"error": err})
128 }
129 }()
130
131 // Create the Core client to handle requests to the Core. Note that the coreClient is an interface and needs to be
132 // cast to the appropriate grpc client by invoking GetCoreGrpcClient on the a.coreClient
khenaidoo27e7ac92021-12-08 14:43:09 -0500133 if a.coreClient, err = vgrpc.NewClient(
134 a.config.AdapterEndpoint,
135 a.config.CoreEndpoint,
136 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
140 go a.coreClient.Start(ctx, setAndTestCoreServiceHandler)
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
khenaidoo106c61a2021-08-11 18:05:46 -0400147 // Create and start the grpc server
148 a.grpcServer = vgrpc.NewGrpcServer(a.config.GrpcAddress, nil, false, p)
149
150 //Register the adapter service
151 a.addAdapterService(ctx, a.grpcServer, a.oltAdapter)
152
153 //Register the olt inter-adapter service
154 a.addOltInterAdapterService(ctx, a.grpcServer, a.oltAdapter)
155
156 // Start the grpc server
157 go a.startGRPCService(ctx, a.grpcServer, oltAdapterService)
cuilin20187b2a8c32019-03-26 19:52:28 -0700158
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700159 // Register this adapter to the Core - retries indefinitely
khenaidoo106c61a2021-08-11 18:05:46 -0400160 if err = a.registerWithCore(ctx, coreService, -1); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000161 logger.Fatal(ctx, "error-registering-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700162 }
cbabu95f21522019-11-13 14:25:18 +0100163
cbabu116b73f2019-12-10 17:56:32 +0530164 // check the readiness and liveliness and update the probe status
165 a.checkServicesReadiness(ctx)
cbabu95f21522019-11-13 14:25:18 +0100166}
167
khenaidoo106c61a2021-08-11 18:05:46 -0400168// TODO: Any action the adapter needs to do following a Core restart?
169func (a *adapter) coreRestarted(ctx context.Context, endPoint string) error {
170 logger.Errorw(ctx, "core-restarted", log.Fields{"endpoint": endPoint})
171 return nil
172}
173
174// setAndTestCoreServiceHandler is used to test whether the remote gRPC service is up
khenaidoo27e7ac92021-12-08 14:43:09 -0500175func setAndTestCoreServiceHandler(ctx context.Context, conn *grpc.ClientConn, clientConn *common.Connection) interface{} {
khenaidoodc2116e2021-10-19 17:33:19 -0400176 svc := core_service.NewCoreServiceClient(conn)
khenaidoo27e7ac92021-12-08 14:43:09 -0500177 if h, err := svc.GetHealthStatus(ctx, clientConn); err != nil || h.State != health.HealthStatus_HEALTHY {
khenaidoo106c61a2021-08-11 18:05:46 -0400178 return nil
179 }
180 return svc
181}
182
cbabu95f21522019-11-13 14:25:18 +0100183/**
184This function checks the liveliness and readiness of the kakfa and kv-client services
185and update the status in the probe.
186*/
cbabu116b73f2019-12-10 17:56:32 +0530187func (a *adapter) checkServicesReadiness(ctx context.Context) {
188 // checks the kafka readiness
khenaidoo106c61a2021-08-11 18:05:46 -0400189 go kafka.MonitorKafkaReadiness(ctx, a.kafkaClient, a.config.LiveProbeInterval, a.config.NotLiveProbeInterval, clusterMessagingService)
cbabu116b73f2019-12-10 17:56:32 +0530190
191 // checks the kv-store readiness
192 go a.checkKvStoreReadiness(ctx)
193}
194
195/**
196This function checks the liveliness and readiness of the kv-store service
197and update the status in the probe.
198*/
199func (a *adapter) checkKvStoreReadiness(ctx context.Context) {
200 // dividing the live probe interval by 2 to get updated status every 30s
201 timeout := a.config.LiveProbeInterval / 2
202 kvStoreChannel := make(chan bool, 1)
203
204 // Default false to check the liveliness.
205 kvStoreChannel <- false
cbabu95f21522019-11-13 14:25:18 +0100206 for {
cbabu116b73f2019-12-10 17:56:32 +0530207 timeoutTimer := time.NewTimer(timeout)
208 select {
209 case liveliness := <-kvStoreChannel:
210 if !liveliness {
211 // kv-store not reachable or down, updating the status to not ready state
khenaidoo106c61a2021-08-11 18:05:46 -0400212 probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusNotReady)
cbabu116b73f2019-12-10 17:56:32 +0530213 timeout = a.config.NotLiveProbeInterval
214 } else {
215 // kv-store is reachable , updating the status to running state
khenaidoo106c61a2021-08-11 18:05:46 -0400216 probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusRunning)
cbabu116b73f2019-12-10 17:56:32 +0530217 timeout = a.config.LiveProbeInterval / 2
218 }
219 // Check if the timer has expired or not
220 if !timeoutTimer.Stop() {
221 <-timeoutTimer.C
222 }
223 case <-timeoutTimer.C:
Girish Kumarbeadc112020-02-26 18:41:02 +0000224 // Check the status of the kv-store. Use timeout of 2 seconds to avoid forever blocking
Neha Sharma96b7bf22020-06-15 10:37:32 +0000225 logger.Info(ctx, "kv-store liveliness-recheck")
Girish Kumarbeadc112020-02-26 18:41:02 +0000226 timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second)
227
228 kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx)
229 // Cleanup cancel func resources
230 cancelFunc()
cbabu95f21522019-11-13 14:25:18 +0100231 }
cbabu116b73f2019-12-10 17:56:32 +0530232 }
233}
234
npujarec5762e2020-01-01 14:08:48 +0530235func (a *adapter) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700236 // Stop leadership tracking
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700237 a.halted = true
cuilin20187b2a8c32019-03-26 19:52:28 -0700238
239 // send exit signal
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700240 a.exitChannel <- 0
cuilin20187b2a8c32019-03-26 19:52:28 -0700241
242 // Cleanup - applies only if we had a kvClient
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700243 if a.kvClient != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700244 // Release all reservations
npujarec5762e2020-01-01 14:08:48 +0530245 if err := a.kvClient.ReleaseAllReservations(ctx); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000246 logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700247 }
248 // Close the DB connection
Neha Sharma96b7bf22020-06-15 10:37:32 +0000249 a.kvClient.Close(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700250 }
251
khenaidoo106c61a2021-08-11 18:05:46 -0400252 if a.eventProxy != nil {
253 a.eventProxy.Stop()
Scott Bakere701b862020-02-20 16:19:16 -0800254 }
255
khenaidoo106c61a2021-08-11 18:05:46 -0400256 if a.kafkaClient != nil {
257 a.kafkaClient.Stop(ctx)
258 }
259
260 // Stop core client
261 if a.coreClient != nil {
262 a.coreClient.Stop(ctx)
263 }
264
265 // TODO: Stop child devices connections
266
cuilin20187b2a8c32019-03-26 19:52:28 -0700267 // TODO: More cleanup
268}
269
Neha Sharma96b7bf22020-06-15 10:37:32 +0000270func newKVClient(ctx context.Context, storeType, address string, timeout time.Duration) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700271
Neha Sharma96b7bf22020-06-15 10:37:32 +0000272 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700273 switch storeType {
cuilin20187b2a8c32019-03-26 19:52:28 -0700274 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000275 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
cuilin20187b2a8c32019-03-26 19:52:28 -0700276 }
277 return nil, errors.New("unsupported-kv-store")
278}
279
Neha Sharma96b7bf22020-06-15 10:37:32 +0000280func newKafkaClient(ctx context.Context, clientType, address string) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700281
Neha Sharma96b7bf22020-06-15 10:37:32 +0000282 logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700283 switch clientType {
284 case "sarama":
285 return kafka.NewSaramaClient(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000286 kafka.Address(address),
cuilin20187b2a8c32019-03-26 19:52:28 -0700287 kafka.ProducerReturnOnErrors(true),
288 kafka.ProducerReturnOnSuccess(true),
289 kafka.ProducerMaxRetries(6),
Abhilash S.L3b494632019-07-16 15:51:09 +0530290 kafka.ProducerRetryBackoff(time.Millisecond*30),
291 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700292 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700293
cuilin20187b2a8c32019-03-26 19:52:28 -0700294 return nil, errors.New("unsupported-client-type")
295}
296
Neha Sharma96b7bf22020-06-15 10:37:32 +0000297func (a *adapter) setKVClient(ctx context.Context) error {
298 client, err := newKVClient(ctx, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout)
cuilin20187b2a8c32019-03-26 19:52:28 -0700299 if err != nil {
300 a.kvClient = nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700301 return err
302 }
303 a.kvClient = client
divyadesaia37f78b2020-02-07 12:41:22 +0000304
cuilin20187b2a8c32019-03-26 19:52:28 -0700305 return nil
306}
307
khenaidoo106c61a2021-08-11 18:05:46 -0400308// startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server
309func (a *adapter) startGRPCService(ctx context.Context, server *vgrpc.GrpcServer, serviceName string) {
310 logger.Infow(ctx, "starting-grpc-service", log.Fields{"service": serviceName})
311
312 probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning)
313 logger.Infow(ctx, "grpc-service-started", log.Fields{"service": serviceName})
314
315 server.Start(ctx)
316 probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusStopped)
cuilin20187b2a8c32019-03-26 19:52:28 -0700317}
318
khenaidoodc2116e2021-10-19 17:33:19 -0400319func (a *adapter) addAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler adapter_service.AdapterServiceServer) {
khenaidoo106c61a2021-08-11 18:05:46 -0400320 logger.Info(ctx, "adding-adapter-service")
321
322 server.AddService(func(gs *grpc.Server) {
khenaidoodc2116e2021-10-19 17:33:19 -0400323 adapter_service.RegisterAdapterServiceServer(gs, handler)
khenaidoo106c61a2021-08-11 18:05:46 -0400324 })
325}
326
khenaidoodc2116e2021-10-19 17:33:19 -0400327func (a *adapter) addOltInterAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler olt_inter_adapter_service.OltInterAdapterServiceServer) {
khenaidoo106c61a2021-08-11 18:05:46 -0400328 logger.Info(ctx, "adding-olt-inter-adapter-service")
329
330 server.AddService(func(gs *grpc.Server) {
khenaidoodc2116e2021-10-19 17:33:19 -0400331 olt_inter_adapter_service.RegisterOltInterAdapterServiceServer(gs, handler)
khenaidoo106c61a2021-08-11 18:05:46 -0400332 })
333}
334
335func (a *adapter) startOpenOLT(ctx context.Context, cc *vgrpc.Client, ep eventif.EventProxy,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800336 cfg *config.AdapterFlags, cm *conf.ConfigManager) (*ac.OpenOLT, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000337 logger.Info(ctx, "starting-open-olt")
cuilin20187b2a8c32019-03-26 19:52:28 -0700338 var err error
khenaidoo106c61a2021-08-11 18:05:46 -0400339 sOLT := ac.NewOpenOLT(ctx, cc, ep, cfg, cm)
cuilin20187b2a8c32019-03-26 19:52:28 -0700340
341 if err = sOLT.Start(ctx); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700342 return nil, err
343 }
344
Neha Sharma96b7bf22020-06-15 10:37:32 +0000345 logger.Info(ctx, "open-olt-started")
cuilin20187b2a8c32019-03-26 19:52:28 -0700346 return sOLT, nil
347}
348
khenaidoo106c61a2021-08-11 18:05:46 -0400349func (a *adapter) registerWithCore(ctx context.Context, serviceName string, retries int) error {
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700350 adapterID := fmt.Sprintf("openolt_%d", a.config.CurrentReplica)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000351 logger.Infow(ctx, "registering-with-core", log.Fields{
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700352 "adapterID": adapterID,
353 "currentReplica": a.config.CurrentReplica,
354 "totalReplicas": a.config.TotalReplicas,
355 })
356 adapterDescription := &voltha.Adapter{
357 Id: adapterID, // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400358 Vendor: "VOLTHA OpenOLT",
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700359 Version: version.VersionInfo.Version,
khenaidoo106c61a2021-08-11 18:05:46 -0400360 // The Endpoint refers to the address this service is listening on.
361 Endpoint: a.config.AdapterEndpoint,
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700362 Type: "openolt",
363 CurrentReplica: int32(a.config.CurrentReplica),
364 TotalReplicas: int32(a.config.TotalReplicas),
365 }
366 types := []*voltha.DeviceType{{
367 Id: "openolt",
khenaidoo106c61a2021-08-11 18:05:46 -0400368 AdapterType: "openolt", // Type of the adapter that handles device type
369 Adapter: "openolt", // Deprecated attribute
Girish Gowdru0c588b22019-04-23 23:24:56 -0400370 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
371 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700372 deviceTypes := &voltha.DeviceTypes{Items: types}
373 count := 0
374 for {
khenaidoo106c61a2021-08-11 18:05:46 -0400375 gClient, err := a.coreClient.GetCoreServiceClient()
376 if gClient != nil {
khenaidoodc2116e2021-10-19 17:33:19 -0400377 if _, err = gClient.RegisterAdapter(log.WithSpanFromContext(context.TODO(), ctx), &ca.AdapterRegistration{
khenaidoo106c61a2021-08-11 18:05:46 -0400378 Adapter: adapterDescription,
379 DTypes: deviceTypes}); err == nil {
380 break
cuilin20187b2a8c32019-03-26 19:52:28 -0700381 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700382 }
khenaidoo106c61a2021-08-11 18:05:46 -0400383 logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"endpoint": a.config.CoreEndpoint, "error": err, "count": count, "gclient": gClient})
384 if retries == count {
385 return err
386 }
387 count++
388 // Take a nap before retrying
389 time.Sleep(2 * time.Second)
cuilin20187b2a8c32019-03-26 19:52:28 -0700390 }
khenaidoo106c61a2021-08-11 18:05:46 -0400391 probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000392 logger.Info(ctx, "registered-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700393 return nil
394}
395
Neha Sharma96b7bf22020-06-15 10:37:32 +0000396func waitForExit(ctx context.Context) int {
cuilin20187b2a8c32019-03-26 19:52:28 -0700397 signalChannel := make(chan os.Signal, 1)
398 signal.Notify(signalChannel,
399 syscall.SIGHUP,
400 syscall.SIGINT,
401 syscall.SIGTERM,
402 syscall.SIGQUIT)
403
404 exitChannel := make(chan int)
405
406 go func() {
407 s := <-signalChannel
408 switch s {
409 case syscall.SIGHUP,
410 syscall.SIGINT,
411 syscall.SIGTERM,
412 syscall.SIGQUIT:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000413 logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700414 exitChannel <- 0
415 default:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000416 logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700417 exitChannel <- 1
418 }
419 }()
420
421 code := <-exitChannel
422 return code
423}
424
425func printBanner() {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800426 fmt.Println(` ____ ____ _ _______ `)
427 fmt.Println(` / _ \ / __ \| | |__ __|`)
428 fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `)
429 fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `)
430 fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `)
431 fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `)
432 fmt.Println(` | | `)
433 fmt.Println(` |_| `)
434 fmt.Println(` `)
cuilin20187b2a8c32019-03-26 19:52:28 -0700435}
436
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400437func printVersion() {
438 fmt.Println("VOLTHA OpenOLT Adapter")
439 fmt.Println(version.VersionInfo.String(" "))
440}
441
cuilin20187b2a8c32019-03-26 19:52:28 -0700442func main() {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000443 ctx := context.Background()
cuilin20187b2a8c32019-03-26 19:52:28 -0700444 start := time.Now()
445
446 cf := config.NewAdapterFlags()
447 cf.ParseCommandArguments()
448
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700449 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700450
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000451 logLevel, err := log.StringToLogLevel(cf.LogLevel)
452 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000453 logger.Fatalf(ctx, "Cannot setup logging, %s", err)
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000454 }
Rohan Agrawal2488f192020-01-31 09:26:55 +0000455
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700456 // Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000457 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
Girish Kumara1ea2aa2020-08-19 18:14:22 +0000458 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
cuilin20187b2a8c32019-03-26 19:52:28 -0700459 }
460
461 // Update all loggers (provisionned via init) with a common field
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000462 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
Girish Kumara1ea2aa2020-08-19 18:14:22 +0000463 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
cuilin20187b2a8c32019-03-26 19:52:28 -0700464 }
465
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000466 log.SetAllLogLevel(logLevel)
Rohan Agrawal93bced32020-02-11 10:16:01 +0000467
Matteo Scandolo8f2b9572020-02-28 15:35:23 -0800468 realMain()
469
Kent Hagermane6ff1012020-07-14 15:07:53 -0400470 defer func() {
471 err := log.CleanUp()
472 if err != nil {
473 logger.Errorw(context.Background(), "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
474 }
475 }()
cuilin20187b2a8c32019-03-26 19:52:28 -0700476
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400477 // Print version / build information and exit
478 if cf.DisplayVersionOnly {
479 printVersion()
480 return
481 }
482
cuilin20187b2a8c32019-03-26 19:52:28 -0700483 // Print banner if specified
484 if cf.Banner {
485 printBanner()
486 }
487
Neha Sharma96b7bf22020-06-15 10:37:32 +0000488 logger.Infow(ctx, "config", log.Fields{"config": *cf})
cuilin20187b2a8c32019-03-26 19:52:28 -0700489
490 ctx, cancel := context.WithCancel(context.Background())
491 defer cancel()
492
493 ad := newAdapter(cf)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000494
495 p := &probe.Probe{}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000496 go p.ListenAndServe(ctx, ad.config.ProbeAddress)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000497
498 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
499
Girish Kumar935f7af2020-08-18 11:59:42 +0000500 closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled)
Girish Kumar11e15972020-06-15 14:51:10 +0000501 if err != nil {
502 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
503 } else {
504 defer log.TerminateTracing(closer)
505 }
506
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000507 go ad.start(probeCtx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700508
Neha Sharma96b7bf22020-06-15 10:37:32 +0000509 code := waitForExit(ctx)
510 logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code})
cuilin20187b2a8c32019-03-26 19:52:28 -0700511
512 // Cleanup before leaving
npujarec5762e2020-01-01 14:08:48 +0530513 ad.stop(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700514
515 elapsed := time.Since(start)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000516 logger.Infow(ctx, "run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700517}