blob: 148f3bdb53c68e39d0ac8c48785781bb422e53ee [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 "github.com/golang/protobuf/ptypes/empty"
30 conf "github.com/opencord/voltha-lib-go/v7/pkg/config"
31 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
32 "github.com/opencord/voltha-lib-go/v7/pkg/events"
33 "github.com/opencord/voltha-lib-go/v7/pkg/events/eventif"
34 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
35 "github.com/opencord/voltha-lib-go/v7/pkg/kafka"
36 "github.com/opencord/voltha-lib-go/v7/pkg/log"
37 "github.com/opencord/voltha-lib-go/v7/pkg/probe"
38 "github.com/opencord/voltha-lib-go/v7/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"
khenaidoo106c61a2021-08-11 18:05:46 -040041 "github.com/opencord/voltha-protos/v5/go/adapter_services"
42 "github.com/opencord/voltha-protos/v5/go/core"
43 ic "github.com/opencord/voltha-protos/v5/go/inter_container"
44 "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 {
khenaidoo106c61a2021-08-11 18:05:46 -040056 instanceID string
57 config *config.AdapterFlags
58 grpcServer *vgrpc.GrpcServer
59 oltAdapter *ac.OpenOLT
60 kafkaClient kafka.Client
61 kvClient kvstore.Client
62 coreClient *vgrpc.Client
63 eventProxy eventif.EventProxy
64 halted bool
65 exitChannel chan int
cuilin20187b2a8c32019-03-26 19:52:28 -070066}
67
cuilin20187b2a8c32019-03-26 19:52:28 -070068func newAdapter(cf *config.AdapterFlags) *adapter {
69 var a adapter
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070070 a.instanceID = cf.InstanceID
cuilin20187b2a8c32019-03-26 19:52:28 -070071 a.config = cf
72 a.halted = false
73 a.exitChannel = make(chan int, 1)
cuilin20187b2a8c32019-03-26 19:52:28 -070074 return &a
75}
76
77func (a *adapter) start(ctx context.Context) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000078 logger.Info(ctx, "Starting Core Adapter components")
cuilin20187b2a8c32019-03-26 19:52:28 -070079 var err error
80
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000081 var p *probe.Probe
82 if value := ctx.Value(probe.ProbeContextKey); value != nil {
83 if _, ok := value.(*probe.Probe); ok {
84 p = value.(*probe.Probe)
85 p.RegisterService(
Neha Sharma96b7bf22020-06-15 10:37:32 +000086 ctx,
khenaidoo106c61a2021-08-11 18:05:46 -040087 clusterMessagingService,
88 kvService,
89 oltAdapterService,
90 coreService,
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000091 )
92 }
93 }
94
cuilin20187b2a8c32019-03-26 19:52:28 -070095 // Setup KV Client
Neha Sharma96b7bf22020-06-15 10:37:32 +000096 logger.Debugw(ctx, "create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
97 if err = a.setKVClient(ctx); err != nil {
98 logger.Fatalw(ctx, "error-setting-kv-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -070099 }
100
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000101 if p != nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400102 p.UpdateStatus(ctx, kvService, probe.ServiceStatusRunning)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000103 }
104
divyadesaia37f78b2020-02-07 12:41:22 +0000105 // Setup Log Config
Neha Sharma96b7bf22020-06-15 10:37:32 +0000106 cm := conf.NewConfigManager(ctx, a.kvClient, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout)
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800107
divyadesaid26f6b12020-03-19 06:30:28 +0000108 go conf.StartLogLevelConfigProcessing(cm, ctx)
Girish Kumar935f7af2020-08-18 11:59:42 +0000109 go conf.StartLogFeaturesConfigProcessing(cm, ctx)
divyadesaia37f78b2020-02-07 12:41:22 +0000110
cuilin20187b2a8c32019-03-26 19:52:28 -0700111 // Setup Kafka Client
khenaidoo106c61a2021-08-11 18:05:46 -0400112 if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaClusterAddress); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000113 logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700114 }
115
khenaidoo106c61a2021-08-11 18:05:46 -0400116 // Start kafka communication with the broker
117 if err := kafka.StartAndWaitUntilKafkaConnectionIsUp(ctx, a.kafkaClient, a.config.HeartbeatCheckInterval, clusterMessagingService); err != nil {
118 logger.Fatal(ctx, "unable-to-connect-to-kafka")
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000119 }
120
Devmalya Paulfb990a52019-07-09 10:01:49 -0400121 // Create the event proxy to post events to KAFKA
Himani Chawlacd407802020-12-10 12:08:59 +0530122 a.eventProxy = events.NewEventProxy(events.MsgClient(a.kafkaClient), events.MsgTopic(kafka.Topic{Name: a.config.EventTopic}))
khenaidoo106c61a2021-08-11 18:05:46 -0400123 go func() {
124 if err := a.eventProxy.Start(); err != nil {
125 logger.Fatalw(ctx, "event-proxy-cannot-start", log.Fields{"error": err})
126 }
127 }()
128
129 // Create the Core client to handle requests to the Core. Note that the coreClient is an interface and needs to be
130 // cast to the appropriate grpc client by invoking GetCoreGrpcClient on the a.coreClient
131 if a.coreClient, err = vgrpc.NewClient(a.config.CoreEndpoint,
132 a.coreRestarted,
133 vgrpc.ActivityCheck(true)); err != nil {
134 logger.Fatal(ctx, "grpc-client-not-created")
135 }
136 // Start the core grpc client
137 go a.coreClient.Start(ctx, setAndTestCoreServiceHandler)
Devmalya Paulfb990a52019-07-09 10:01:49 -0400138
cuilin20187b2a8c32019-03-26 19:52:28 -0700139 // Create the open OLT adapter
khenaidoo106c61a2021-08-11 18:05:46 -0400140 if a.oltAdapter, err = a.startOpenOLT(ctx, a.coreClient, a.eventProxy, a.config, cm); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000141 logger.Fatalw(ctx, "error-starting-openolt", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700142 }
143
khenaidoo106c61a2021-08-11 18:05:46 -0400144 // Create and start the grpc server
145 a.grpcServer = vgrpc.NewGrpcServer(a.config.GrpcAddress, nil, false, p)
146
147 //Register the adapter service
148 a.addAdapterService(ctx, a.grpcServer, a.oltAdapter)
149
150 //Register the olt inter-adapter service
151 a.addOltInterAdapterService(ctx, a.grpcServer, a.oltAdapter)
152
153 // Start the grpc server
154 go a.startGRPCService(ctx, a.grpcServer, oltAdapterService)
cuilin20187b2a8c32019-03-26 19:52:28 -0700155
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700156 // Register this adapter to the Core - retries indefinitely
khenaidoo106c61a2021-08-11 18:05:46 -0400157 if err = a.registerWithCore(ctx, coreService, -1); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000158 logger.Fatal(ctx, "error-registering-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700159 }
cbabu95f21522019-11-13 14:25:18 +0100160
cbabu116b73f2019-12-10 17:56:32 +0530161 // check the readiness and liveliness and update the probe status
162 a.checkServicesReadiness(ctx)
cbabu95f21522019-11-13 14:25:18 +0100163}
164
khenaidoo106c61a2021-08-11 18:05:46 -0400165// TODO: Any action the adapter needs to do following a Core restart?
166func (a *adapter) coreRestarted(ctx context.Context, endPoint string) error {
167 logger.Errorw(ctx, "core-restarted", log.Fields{"endpoint": endPoint})
168 return nil
169}
170
171// setAndTestCoreServiceHandler is used to test whether the remote gRPC service is up
172func setAndTestCoreServiceHandler(ctx context.Context, conn *grpc.ClientConn) interface{} {
173 svc := core.NewCoreServiceClient(conn)
174 if h, err := svc.GetHealthStatus(ctx, &empty.Empty{}); err != nil || h.State != voltha.HealthStatus_HEALTHY {
175 return nil
176 }
177 return svc
178}
179
cbabu95f21522019-11-13 14:25:18 +0100180/**
181This function checks the liveliness and readiness of the kakfa and kv-client services
182and update the status in the probe.
183*/
cbabu116b73f2019-12-10 17:56:32 +0530184func (a *adapter) checkServicesReadiness(ctx context.Context) {
185 // checks the kafka readiness
khenaidoo106c61a2021-08-11 18:05:46 -0400186 go kafka.MonitorKafkaReadiness(ctx, a.kafkaClient, a.config.LiveProbeInterval, a.config.NotLiveProbeInterval, clusterMessagingService)
cbabu116b73f2019-12-10 17:56:32 +0530187
188 // checks the kv-store readiness
189 go a.checkKvStoreReadiness(ctx)
190}
191
192/**
193This function checks the liveliness and readiness of the kv-store service
194and update the status in the probe.
195*/
196func (a *adapter) checkKvStoreReadiness(ctx context.Context) {
197 // dividing the live probe interval by 2 to get updated status every 30s
198 timeout := a.config.LiveProbeInterval / 2
199 kvStoreChannel := make(chan bool, 1)
200
201 // Default false to check the liveliness.
202 kvStoreChannel <- false
cbabu95f21522019-11-13 14:25:18 +0100203 for {
cbabu116b73f2019-12-10 17:56:32 +0530204 timeoutTimer := time.NewTimer(timeout)
205 select {
206 case liveliness := <-kvStoreChannel:
207 if !liveliness {
208 // kv-store not reachable or down, updating the status to not ready state
khenaidoo106c61a2021-08-11 18:05:46 -0400209 probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusNotReady)
cbabu116b73f2019-12-10 17:56:32 +0530210 timeout = a.config.NotLiveProbeInterval
211 } else {
212 // kv-store is reachable , updating the status to running state
khenaidoo106c61a2021-08-11 18:05:46 -0400213 probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusRunning)
cbabu116b73f2019-12-10 17:56:32 +0530214 timeout = a.config.LiveProbeInterval / 2
215 }
216 // Check if the timer has expired or not
217 if !timeoutTimer.Stop() {
218 <-timeoutTimer.C
219 }
220 case <-timeoutTimer.C:
Girish Kumarbeadc112020-02-26 18:41:02 +0000221 // Check the status of the kv-store. Use timeout of 2 seconds to avoid forever blocking
Neha Sharma96b7bf22020-06-15 10:37:32 +0000222 logger.Info(ctx, "kv-store liveliness-recheck")
Girish Kumarbeadc112020-02-26 18:41:02 +0000223 timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second)
224
225 kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx)
226 // Cleanup cancel func resources
227 cancelFunc()
cbabu95f21522019-11-13 14:25:18 +0100228 }
cbabu116b73f2019-12-10 17:56:32 +0530229 }
230}
231
npujarec5762e2020-01-01 14:08:48 +0530232func (a *adapter) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700233 // Stop leadership tracking
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700234 a.halted = true
cuilin20187b2a8c32019-03-26 19:52:28 -0700235
236 // send exit signal
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700237 a.exitChannel <- 0
cuilin20187b2a8c32019-03-26 19:52:28 -0700238
239 // Cleanup - applies only if we had a kvClient
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700240 if a.kvClient != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700241 // Release all reservations
npujarec5762e2020-01-01 14:08:48 +0530242 if err := a.kvClient.ReleaseAllReservations(ctx); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000243 logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700244 }
245 // Close the DB connection
Neha Sharma96b7bf22020-06-15 10:37:32 +0000246 a.kvClient.Close(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700247 }
248
khenaidoo106c61a2021-08-11 18:05:46 -0400249 if a.eventProxy != nil {
250 a.eventProxy.Stop()
Scott Bakere701b862020-02-20 16:19:16 -0800251 }
252
khenaidoo106c61a2021-08-11 18:05:46 -0400253 if a.kafkaClient != nil {
254 a.kafkaClient.Stop(ctx)
255 }
256
257 // Stop core client
258 if a.coreClient != nil {
259 a.coreClient.Stop(ctx)
260 }
261
262 // TODO: Stop child devices connections
263
cuilin20187b2a8c32019-03-26 19:52:28 -0700264 // TODO: More cleanup
265}
266
Neha Sharma96b7bf22020-06-15 10:37:32 +0000267func newKVClient(ctx context.Context, storeType, address string, timeout time.Duration) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700268
Neha Sharma96b7bf22020-06-15 10:37:32 +0000269 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700270 switch storeType {
cuilin20187b2a8c32019-03-26 19:52:28 -0700271 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000272 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
cuilin20187b2a8c32019-03-26 19:52:28 -0700273 }
274 return nil, errors.New("unsupported-kv-store")
275}
276
Neha Sharma96b7bf22020-06-15 10:37:32 +0000277func newKafkaClient(ctx context.Context, clientType, address string) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700278
Neha Sharma96b7bf22020-06-15 10:37:32 +0000279 logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700280 switch clientType {
281 case "sarama":
282 return kafka.NewSaramaClient(
Neha Sharma3f221ae2020-04-29 19:02:12 +0000283 kafka.Address(address),
cuilin20187b2a8c32019-03-26 19:52:28 -0700284 kafka.ProducerReturnOnErrors(true),
285 kafka.ProducerReturnOnSuccess(true),
286 kafka.ProducerMaxRetries(6),
Abhilash S.L3b494632019-07-16 15:51:09 +0530287 kafka.ProducerRetryBackoff(time.Millisecond*30),
288 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700289 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700290
cuilin20187b2a8c32019-03-26 19:52:28 -0700291 return nil, errors.New("unsupported-client-type")
292}
293
Neha Sharma96b7bf22020-06-15 10:37:32 +0000294func (a *adapter) setKVClient(ctx context.Context) error {
295 client, err := newKVClient(ctx, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout)
cuilin20187b2a8c32019-03-26 19:52:28 -0700296 if err != nil {
297 a.kvClient = nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700298 return err
299 }
300 a.kvClient = client
divyadesaia37f78b2020-02-07 12:41:22 +0000301
cuilin20187b2a8c32019-03-26 19:52:28 -0700302 return nil
303}
304
khenaidoo106c61a2021-08-11 18:05:46 -0400305// startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server
306func (a *adapter) startGRPCService(ctx context.Context, server *vgrpc.GrpcServer, serviceName string) {
307 logger.Infow(ctx, "starting-grpc-service", log.Fields{"service": serviceName})
308
309 probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning)
310 logger.Infow(ctx, "grpc-service-started", log.Fields{"service": serviceName})
311
312 server.Start(ctx)
313 probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusStopped)
cuilin20187b2a8c32019-03-26 19:52:28 -0700314}
315
khenaidoo106c61a2021-08-11 18:05:46 -0400316func (a *adapter) addAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler adapter_services.AdapterServiceServer) {
317 logger.Info(ctx, "adding-adapter-service")
318
319 server.AddService(func(gs *grpc.Server) {
320 adapter_services.RegisterAdapterServiceServer(gs, handler)
321 })
322}
323
324func (a *adapter) addOltInterAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler adapter_services.OltInterAdapterServiceServer) {
325 logger.Info(ctx, "adding-olt-inter-adapter-service")
326
327 server.AddService(func(gs *grpc.Server) {
328 adapter_services.RegisterOltInterAdapterServiceServer(gs, handler)
329 })
330}
331
332func (a *adapter) startOpenOLT(ctx context.Context, cc *vgrpc.Client, ep eventif.EventProxy,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800333 cfg *config.AdapterFlags, cm *conf.ConfigManager) (*ac.OpenOLT, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000334 logger.Info(ctx, "starting-open-olt")
cuilin20187b2a8c32019-03-26 19:52:28 -0700335 var err error
khenaidoo106c61a2021-08-11 18:05:46 -0400336 sOLT := ac.NewOpenOLT(ctx, cc, ep, cfg, cm)
cuilin20187b2a8c32019-03-26 19:52:28 -0700337
338 if err = sOLT.Start(ctx); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700339 return nil, err
340 }
341
Neha Sharma96b7bf22020-06-15 10:37:32 +0000342 logger.Info(ctx, "open-olt-started")
cuilin20187b2a8c32019-03-26 19:52:28 -0700343 return sOLT, nil
344}
345
khenaidoo106c61a2021-08-11 18:05:46 -0400346func (a *adapter) registerWithCore(ctx context.Context, serviceName string, retries int) error {
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700347 adapterID := fmt.Sprintf("openolt_%d", a.config.CurrentReplica)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000348 logger.Infow(ctx, "registering-with-core", log.Fields{
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700349 "adapterID": adapterID,
350 "currentReplica": a.config.CurrentReplica,
351 "totalReplicas": a.config.TotalReplicas,
352 })
353 adapterDescription := &voltha.Adapter{
354 Id: adapterID, // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400355 Vendor: "VOLTHA OpenOLT",
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700356 Version: version.VersionInfo.Version,
khenaidoo106c61a2021-08-11 18:05:46 -0400357 // The Endpoint refers to the address this service is listening on.
358 Endpoint: a.config.AdapterEndpoint,
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700359 Type: "openolt",
360 CurrentReplica: int32(a.config.CurrentReplica),
361 TotalReplicas: int32(a.config.TotalReplicas),
362 }
363 types := []*voltha.DeviceType{{
364 Id: "openolt",
khenaidoo106c61a2021-08-11 18:05:46 -0400365 AdapterType: "openolt", // Type of the adapter that handles device type
366 Adapter: "openolt", // Deprecated attribute
Girish Gowdru0c588b22019-04-23 23:24:56 -0400367 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
368 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700369 deviceTypes := &voltha.DeviceTypes{Items: types}
370 count := 0
371 for {
khenaidoo106c61a2021-08-11 18:05:46 -0400372 gClient, err := a.coreClient.GetCoreServiceClient()
373 if gClient != nil {
374 if _, err = gClient.RegisterAdapter(log.WithSpanFromContext(context.TODO(), ctx), &ic.AdapterRegistration{
375 Adapter: adapterDescription,
376 DTypes: deviceTypes}); err == nil {
377 break
cuilin20187b2a8c32019-03-26 19:52:28 -0700378 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700379 }
khenaidoo106c61a2021-08-11 18:05:46 -0400380 logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"endpoint": a.config.CoreEndpoint, "error": err, "count": count, "gclient": gClient})
381 if retries == count {
382 return err
383 }
384 count++
385 // Take a nap before retrying
386 time.Sleep(2 * time.Second)
cuilin20187b2a8c32019-03-26 19:52:28 -0700387 }
khenaidoo106c61a2021-08-11 18:05:46 -0400388 probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000389 logger.Info(ctx, "registered-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700390 return nil
391}
392
Neha Sharma96b7bf22020-06-15 10:37:32 +0000393func waitForExit(ctx context.Context) int {
cuilin20187b2a8c32019-03-26 19:52:28 -0700394 signalChannel := make(chan os.Signal, 1)
395 signal.Notify(signalChannel,
396 syscall.SIGHUP,
397 syscall.SIGINT,
398 syscall.SIGTERM,
399 syscall.SIGQUIT)
400
401 exitChannel := make(chan int)
402
403 go func() {
404 s := <-signalChannel
405 switch s {
406 case syscall.SIGHUP,
407 syscall.SIGINT,
408 syscall.SIGTERM,
409 syscall.SIGQUIT:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000410 logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700411 exitChannel <- 0
412 default:
Neha Sharma96b7bf22020-06-15 10:37:32 +0000413 logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700414 exitChannel <- 1
415 }
416 }()
417
418 code := <-exitChannel
419 return code
420}
421
422func printBanner() {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800423 fmt.Println(` ____ ____ _ _______ `)
424 fmt.Println(` / _ \ / __ \| | |__ __|`)
425 fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `)
426 fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `)
427 fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `)
428 fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `)
429 fmt.Println(` | | `)
430 fmt.Println(` |_| `)
431 fmt.Println(` `)
cuilin20187b2a8c32019-03-26 19:52:28 -0700432}
433
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400434func printVersion() {
435 fmt.Println("VOLTHA OpenOLT Adapter")
436 fmt.Println(version.VersionInfo.String(" "))
437}
438
cuilin20187b2a8c32019-03-26 19:52:28 -0700439func main() {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000440 ctx := context.Background()
cuilin20187b2a8c32019-03-26 19:52:28 -0700441 start := time.Now()
442
443 cf := config.NewAdapterFlags()
444 cf.ParseCommandArguments()
445
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700446 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700447
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000448 logLevel, err := log.StringToLogLevel(cf.LogLevel)
449 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000450 logger.Fatalf(ctx, "Cannot setup logging, %s", err)
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000451 }
Rohan Agrawal2488f192020-01-31 09:26:55 +0000452
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700453 // Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000454 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
Girish Kumara1ea2aa2020-08-19 18:14:22 +0000455 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
cuilin20187b2a8c32019-03-26 19:52:28 -0700456 }
457
458 // Update all loggers (provisionned via init) with a common field
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000459 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
Girish Kumara1ea2aa2020-08-19 18:14:22 +0000460 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
cuilin20187b2a8c32019-03-26 19:52:28 -0700461 }
462
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000463 log.SetAllLogLevel(logLevel)
Rohan Agrawal93bced32020-02-11 10:16:01 +0000464
Matteo Scandolo8f2b9572020-02-28 15:35:23 -0800465 realMain()
466
Kent Hagermane6ff1012020-07-14 15:07:53 -0400467 defer func() {
468 err := log.CleanUp()
469 if err != nil {
470 logger.Errorw(context.Background(), "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
471 }
472 }()
cuilin20187b2a8c32019-03-26 19:52:28 -0700473
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400474 // Print version / build information and exit
475 if cf.DisplayVersionOnly {
476 printVersion()
477 return
478 }
479
cuilin20187b2a8c32019-03-26 19:52:28 -0700480 // Print banner if specified
481 if cf.Banner {
482 printBanner()
483 }
484
Neha Sharma96b7bf22020-06-15 10:37:32 +0000485 logger.Infow(ctx, "config", log.Fields{"config": *cf})
cuilin20187b2a8c32019-03-26 19:52:28 -0700486
487 ctx, cancel := context.WithCancel(context.Background())
488 defer cancel()
489
490 ad := newAdapter(cf)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000491
492 p := &probe.Probe{}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000493 go p.ListenAndServe(ctx, ad.config.ProbeAddress)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000494
495 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
496
Girish Kumar935f7af2020-08-18 11:59:42 +0000497 closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled)
Girish Kumar11e15972020-06-15 14:51:10 +0000498 if err != nil {
499 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
500 } else {
501 defer log.TerminateTracing(closer)
502 }
503
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000504 go ad.start(probeCtx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700505
Neha Sharma96b7bf22020-06-15 10:37:32 +0000506 code := waitForExit(ctx)
507 logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code})
cuilin20187b2a8c32019-03-26 19:52:28 -0700508
509 // Cleanup before leaving
npujarec5762e2020-01-01 14:08:48 +0530510 ad.stop(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700511
512 elapsed := time.Since(start)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000513 logger.Infow(ctx, "run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700514}