Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 1 | /* |
Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 2 | * Copyright 2020-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 3 | * |
| 4 | * 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * 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. |
| 15 | */ |
| 16 | |
Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 17 | // Package main -> this is the entry point of the OpenOnuAdapter |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 18 | package main |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "errors" |
| 23 | "fmt" |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 24 | "io/ioutil" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 25 | "os" |
| 26 | "os/signal" |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 27 | "strings" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 28 | "syscall" |
| 29 | "time" |
| 30 | |
nikesh.krishnan | ca4afa3 | 2023-06-28 03:42:16 +0530 | [diff] [blame] | 31 | grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 32 | conf "github.com/opencord/voltha-lib-go/v7/pkg/config" |
| 33 | "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore" |
| 34 | "github.com/opencord/voltha-lib-go/v7/pkg/events" |
| 35 | "github.com/opencord/voltha-lib-go/v7/pkg/events/eventif" |
| 36 | vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc" |
| 37 | "github.com/opencord/voltha-lib-go/v7/pkg/kafka" |
| 38 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 39 | "github.com/opencord/voltha-lib-go/v7/pkg/probe" |
| 40 | "github.com/opencord/voltha-lib-go/v7/pkg/version" |
khenaidoo | 42dcdfd | 2021-10-19 17:34:12 -0400 | [diff] [blame] | 41 | "github.com/opencord/voltha-protos/v5/go/adapter_service" |
| 42 | "github.com/opencord/voltha-protos/v5/go/core_service" |
khenaidoo | 42dcdfd | 2021-10-19 17:34:12 -0400 | [diff] [blame] | 43 | "github.com/opencord/voltha-protos/v5/go/onu_inter_adapter_service" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 44 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 45 | "google.golang.org/grpc" |
nikesh.krishnan | ca4afa3 | 2023-06-28 03:42:16 +0530 | [diff] [blame] | 46 | codes "google.golang.org/grpc/codes" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 47 | |
khenaidoo | 42dcdfd | 2021-10-19 17:34:12 -0400 | [diff] [blame] | 48 | "github.com/opencord/voltha-protos/v5/go/core_adapter" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 49 | |
Matteo Scandolo | 761f751 | 2020-11-23 15:52:40 -0800 | [diff] [blame] | 50 | "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/config" |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 51 | ac "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/core" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 52 | ) |
| 53 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 54 | const ( |
| 55 | clusterMessagingService = "cluster-message-service" |
| 56 | onuAdapterService = "onu-adapter-service" |
| 57 | kvService = "kv-service" |
| 58 | coreService = "core-service" |
| 59 | ) |
| 60 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 61 | type adapter struct { |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 62 | //defaultAppName string |
khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 63 | instanceID string |
| 64 | config *config.AdapterFlags |
| 65 | kafkaClient kafka.Client |
| 66 | kvClient kvstore.Client |
| 67 | eventProxy eventif.EventProxy |
| 68 | grpcServer *vgrpc.GrpcServer |
| 69 | onuAdapter *ac.OpenONUAC |
| 70 | onuInterAdapter *ac.OpenONUACInterAdapter |
| 71 | coreClient *vgrpc.Client |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 74 | func newAdapter(cf *config.AdapterFlags) *adapter { |
| 75 | var a adapter |
| 76 | a.instanceID = cf.InstanceID |
| 77 | a.config = cf |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 78 | return &a |
| 79 | } |
| 80 | |
| 81 | func (a *adapter) start(ctx context.Context) error { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 82 | logger.Info(ctx, "Starting Core Adapter components") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 83 | var err error |
| 84 | |
| 85 | var p *probe.Probe |
| 86 | if value := ctx.Value(probe.ProbeContextKey); value != nil { |
| 87 | if _, ok := value.(*probe.Probe); ok { |
| 88 | p = value.(*probe.Probe) |
| 89 | p.RegisterService( |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 90 | ctx, |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 91 | clusterMessagingService, |
| 92 | kvService, |
| 93 | onuAdapterService, |
| 94 | coreService, |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 95 | ) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Setup KV Client |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 100 | logger.Debugw(ctx, "create-kv-client", log.Fields{"kvstore": a.config.KVStoreType}) |
| 101 | if err = a.setKVClient(ctx); err != nil { |
| 102 | logger.Fatalw(ctx, "error-setting-kv-client", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 105 | // Setup Log Config |
Matteo Scandolo | 127c59d | 2021-01-28 11:31:18 -0800 | [diff] [blame] | 106 | cm := conf.NewConfigManager(ctx, a.kvClient, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 107 | go conf.StartLogLevelConfigProcessing(cm, ctx) |
| 108 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 109 | // Setup Kafka Client |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 110 | if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaClusterAddress); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 111 | logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 112 | } |
| 113 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 114 | // Start kafka communication with the broker |
| 115 | if err := kafka.StartAndWaitUntilKafkaConnectionIsUp(ctx, a.kafkaClient, a.config.HeartbeatCheckInterval, clusterMessagingService); err != nil { |
| 116 | logger.Fatal(ctx, "unable-to-connect-to-kafka") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 117 | } |
| 118 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 119 | // Wait until connection to KV store is established |
| 120 | if err := WaitUntilKvStoreConnectionIsUp(ctx, a.kvClient, a.config.KVStoreTimeout, kvService); err != nil { |
| 121 | logger.Fatal(ctx, "unable-to-connect-to-kv-store") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 124 | // Create the event proxy to post events to KAFKA |
Himani Chawla | c07fda0 | 2020-12-09 16:21:21 +0530 | [diff] [blame] | 125 | a.eventProxy = events.NewEventProxy(events.MsgClient(a.kafkaClient), events.MsgTopic(kafka.Topic{Name: a.config.EventTopic})) |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 126 | go func() { |
| 127 | if err := a.eventProxy.Start(); err != nil { |
| 128 | logger.Fatalw(ctx, "event-proxy-cannot-start", log.Fields{"error": err}) |
| 129 | } |
| 130 | }() |
| 131 | |
| 132 | // Create the Core client to handle requests to the Core. Note that the coreClient is an interface and needs to be |
| 133 | // cast to the appropriate grpc client by invoking GetCoreGrpcClient on the a.coreClient |
khenaidoo | 55cebc6 | 2021-12-08 14:44:41 -0500 | [diff] [blame] | 134 | if a.coreClient, err = vgrpc.NewClient( |
| 135 | a.config.AdapterEndpoint, |
| 136 | a.config.CoreEndpoint, |
khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 137 | "core_service.CoreService", |
khenaidoo | 55cebc6 | 2021-12-08 14:44:41 -0500 | [diff] [blame] | 138 | a.coreRestarted); err != nil { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 139 | logger.Fatal(ctx, "grpc-client-not-created") |
| 140 | } |
nikesh.krishnan | ca4afa3 | 2023-06-28 03:42:16 +0530 | [diff] [blame] | 141 | // the backoff function sets the wait time bw each grpc retries, if not set it will take the deafault value of 50ms which is too low, the jitter sets the rpc retry wait time to be in a range of[PerRPCRetryTimeout-0.2, PerRPCRetryTimeout+0.2] |
| 142 | backoffCtxOption := grpc_retry.WithBackoff(grpc_retry.BackoffLinearWithJitter(a.config.PerRPCRetryTimeout, 0.2)) |
| 143 | |
| 144 | retryCodes := []codes.Code{ |
| 145 | codes.Unavailable, // server is currently unavailable |
| 146 | codes.DeadlineExceeded, // deadline for the operation was exceeded |
| 147 | } |
| 148 | grpcRetryOptions := grpc_retry.UnaryClientInterceptor(grpc_retry.WithMax(a.config.MaxRetries), grpc_retry.WithPerRetryTimeout(a.config.PerRPCRetryTimeout), grpc_retry.WithCodes(retryCodes...), backoffCtxOption) |
| 149 | logger.Debug(ctx, "Configuration values", log.Fields{"RETRY": a.config.MaxRetries, "TIMEOUT": a.config.PerRPCRetryTimeout}) |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 150 | // Start the core grpc client |
nikesh.krishnan | ca4afa3 | 2023-06-28 03:42:16 +0530 | [diff] [blame] | 151 | go a.coreClient.Start(ctx, getCoreServiceClientHandler, grpcRetryOptions) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 152 | |
| 153 | // Create the open ONU interface adapter |
khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 154 | if a.onuAdapter, err = a.startONUAdapter(ctx, a.coreClient, a.eventProxy, a.config, cm); err != nil { |
| 155 | logger.Fatalw(ctx, "error-starting-startONUAdapter", log.Fields{"error": err}) |
| 156 | } |
| 157 | |
| 158 | // Create the open ONU Inter adapter |
| 159 | if a.onuInterAdapter, err = a.startONUInterAdapter(ctx, a.onuAdapter); err != nil { |
| 160 | logger.Fatalw(ctx, "error-starting-startONUInterAdapter", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 161 | } |
| 162 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 163 | // Create and start the grpc server |
| 164 | a.grpcServer = vgrpc.NewGrpcServer(a.config.GrpcAddress, nil, false, p) |
| 165 | |
| 166 | //Register the adapter service |
| 167 | a.addAdapterService(ctx, a.grpcServer, a.onuAdapter) |
| 168 | |
| 169 | //Register the onu inter adapter service |
khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 170 | a.addOnuInterAdapterService(ctx, a.grpcServer, a.onuInterAdapter) |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 171 | |
| 172 | go a.startGRPCService(ctx, a.grpcServer, onuAdapterService) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 173 | |
| 174 | // Register this adapter to the Core - retries indefinitely |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 175 | if err = a.registerWithCore(ctx, coreService, -1); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 176 | logger.Fatalw(ctx, "error-registering-with-core", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 177 | } |
| 178 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 179 | // Start the readiness and liveliness check and update the probe status |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 180 | a.checkServicesReadiness(ctx) |
| 181 | return err |
| 182 | } |
| 183 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 184 | // TODO: Any action the adapter needs to do following a Core restart? |
| 185 | func (a *adapter) coreRestarted(ctx context.Context, endPoint string) error { |
| 186 | logger.Errorw(ctx, "core-restarted", log.Fields{"endpoint": endPoint}) |
| 187 | return nil |
| 188 | } |
| 189 | |
khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 190 | // getCoreServiceClientHandler is used to setup the remote gRPC service |
| 191 | func getCoreServiceClientHandler(ctx context.Context, conn *grpc.ClientConn) interface{} { |
| 192 | if conn == nil { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 193 | return nil |
| 194 | } |
khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 195 | return core_service.NewCoreServiceClient(conn) |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 198 | func (a *adapter) stop(ctx context.Context) { |
khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 199 | // Cleanup the grpc services first |
| 200 | if err := a.onuAdapter.Stop(ctx); err != nil { |
| 201 | logger.Errorw(ctx, "failure-stopping-onu-adapter-service", log.Fields{"error": err, "adapter": a.config.AdapterEndpoint}) |
| 202 | } |
| 203 | if err := a.onuInterAdapter.Stop(ctx); err != nil { |
| 204 | logger.Errorw(ctx, "failure-stopping-onu-inter-adapter-service", log.Fields{"error": err, "adapter": a.config.AdapterEndpoint}) |
| 205 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 206 | // Cleanup - applies only if we had a kvClient |
| 207 | if a.kvClient != nil { |
| 208 | // Release all reservations |
| 209 | if err := a.kvClient.ReleaseAllReservations(ctx); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 210 | logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 211 | } |
| 212 | // Close the DB connection |
Girish Gowdra | 5550783 | 2022-06-01 18:12:06 -0700 | [diff] [blame] | 213 | go a.kvClient.Close(ctx) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 214 | } |
| 215 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 216 | if a.eventProxy != nil { |
| 217 | a.eventProxy.Stop() |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 218 | } |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 219 | |
| 220 | if a.kafkaClient != nil { |
| 221 | a.kafkaClient.Stop(ctx) |
| 222 | } |
| 223 | |
| 224 | // Stop core client |
| 225 | if a.coreClient != nil { |
| 226 | a.coreClient.Stop(ctx) |
| 227 | } |
| 228 | |
| 229 | // TODO: More cleanup |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // ############################################# |
| 233 | // Adapter Utility methods ##### begin ######### |
| 234 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 235 | func newKVClient(ctx context.Context, storeType, address string, timeout time.Duration) (kvstore.Client, error) { |
| 236 | logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 237 | switch storeType { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 238 | case "etcd": |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 239 | return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 240 | } |
| 241 | return nil, errors.New("unsupported-kv-store") |
| 242 | } |
| 243 | |
Matteo Scandolo | 127c59d | 2021-01-28 11:31:18 -0800 | [diff] [blame] | 244 | func newKafkaClient(ctx context.Context, clientType, addr string) (kafka.Client, error) { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 245 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 246 | logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType}) |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 247 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 248 | switch clientType { |
| 249 | case "sarama": |
| 250 | return kafka.NewSaramaClient( |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 251 | kafka.Address(addr), |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 252 | kafka.ProducerReturnOnErrors(true), |
| 253 | kafka.ProducerReturnOnSuccess(true), |
| 254 | kafka.ProducerMaxRetries(6), |
| 255 | kafka.ProducerRetryBackoff(time.Millisecond*30), |
| 256 | kafka.MetadatMaxRetries(15)), nil |
| 257 | } |
| 258 | |
| 259 | return nil, errors.New("unsupported-client-type") |
| 260 | } |
| 261 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 262 | func (a *adapter) setKVClient(ctx context.Context) error { |
Matteo Scandolo | 127c59d | 2021-01-28 11:31:18 -0800 | [diff] [blame] | 263 | client, err := newKVClient(ctx, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 264 | if err != nil { |
| 265 | a.kvClient = nil |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 266 | logger.Errorw(ctx, "error-starting-KVClient", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 267 | return err |
| 268 | } |
| 269 | a.kvClient = client |
| 270 | return nil |
| 271 | } |
| 272 | |
khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 273 | func (a *adapter) startONUAdapter(ctx context.Context, cc *vgrpc.Client, ep eventif.EventProxy, |
Matteo Scandolo | f1f39a7 | 2020-11-24 12:08:11 -0800 | [diff] [blame] | 274 | cfg *config.AdapterFlags, cm *conf.ConfigManager) (*ac.OpenONUAC, error) { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 275 | var err error |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 276 | sAcONU := ac.NewOpenONUAC(ctx, cc, ep, a.kvClient, cfg, cm) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 277 | |
| 278 | if err = sAcONU.Start(ctx); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 279 | logger.Fatalw(ctx, "error-starting-OpenOnuAdapterCore", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 280 | return nil, err |
| 281 | } |
| 282 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 283 | logger.Info(ctx, "open-ont-OpenOnuAdapterCore-started") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 284 | return sAcONU, nil |
| 285 | } |
| 286 | |
khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 287 | func (a *adapter) startONUInterAdapter(ctx context.Context, onuA *ac.OpenONUAC) (*ac.OpenONUACInterAdapter, error) { |
| 288 | var err error |
| 289 | sAcONUInterAdapter := ac.NewOpenONUACAdapter(ctx, onuA) |
| 290 | |
| 291 | if err = sAcONUInterAdapter.Start(ctx); err != nil { |
| 292 | logger.Fatalw(ctx, "error-starting-OpenONUACInterAdapter", log.Fields{"error": err}) |
| 293 | return nil, err |
| 294 | } |
| 295 | |
| 296 | logger.Info(ctx, "OpenONUACInterAdapter-started") |
| 297 | return sAcONUInterAdapter, nil |
| 298 | } |
| 299 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 300 | func (a *adapter) registerWithCore(ctx context.Context, serviceName string, retries int) error { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 301 | adapterID := fmt.Sprintf("brcm_openomci_onu_%d", a.config.CurrentReplica) |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 302 | vendorIdsList := strings.Split(a.config.OnuVendorIds, ",") |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 303 | logger.Infow(ctx, "registering-with-core", log.Fields{ |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 304 | "adapterID": adapterID, |
| 305 | "currentReplica": a.config.CurrentReplica, |
| 306 | "totalReplicas": a.config.TotalReplicas, |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 307 | "onuVendorIds": vendorIdsList, |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 308 | }) |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 309 | adapterDescription := &voltha.Adapter{ |
Andrea Campanella | 961734c | 2021-01-18 11:44:47 +0100 | [diff] [blame] | 310 | Id: adapterID, // Unique name for the device type ->exact type required for OLT comm???? |
| 311 | Vendor: "VOLTHA OpenONUGo", |
| 312 | Version: version.VersionInfo.Version, |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 313 | Endpoint: a.config.AdapterEndpoint, |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 314 | Type: "brcm_openomci_onu", |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 315 | CurrentReplica: int32(a.config.CurrentReplica), |
| 316 | TotalReplicas: int32(a.config.TotalReplicas), |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 317 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 318 | types := []*voltha.DeviceType{{Id: "brcm_openomci_onu", |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 319 | VendorIds: vendorIdsList, |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 320 | AdapterType: "brcm_openomci_onu", // Type of adapter that handles this device type |
| 321 | Adapter: "brcm_openomci_onu", // Deprecated attribute |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 322 | AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling |
| 323 | AcceptsAddRemoveFlowUpdates: true}} |
| 324 | deviceTypes := &voltha.DeviceTypes{Items: types} |
| 325 | count := 0 |
| 326 | for { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 327 | gClient, err := a.coreClient.GetCoreServiceClient() |
| 328 | if gClient != nil { |
| 329 | if gClient != nil { |
khenaidoo | 42dcdfd | 2021-10-19 17:34:12 -0400 | [diff] [blame] | 330 | if _, err = gClient.RegisterAdapter(log.WithSpanFromContext(context.TODO(), ctx), &core_adapter.AdapterRegistration{ |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 331 | Adapter: adapterDescription, |
| 332 | DTypes: deviceTypes}); err == nil { |
| 333 | break |
| 334 | } |
| 335 | } |
| 336 | logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"endpoint": a.config.CoreEndpoint, "error": err, "count": count, "gclient": gClient}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 337 | if retries == count { |
| 338 | return err |
| 339 | } |
| 340 | count++ |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 341 | // Take a power nap before retrying |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 342 | time.Sleep(2 * time.Second) |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 343 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 346 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 347 | logger.Info(ctx, "registered-with-core") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 348 | return nil |
| 349 | } |
| 350 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 351 | // startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server |
| 352 | func (a *adapter) startGRPCService(ctx context.Context, server *vgrpc.GrpcServer, serviceName string) { |
| 353 | logger.Infow(ctx, "service-created", log.Fields{"service": serviceName}) |
| 354 | |
| 355 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
| 356 | logger.Infow(ctx, "service-started", log.Fields{"service": serviceName}) |
| 357 | |
| 358 | server.Start(ctx) |
| 359 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusStopped) |
| 360 | } |
| 361 | |
khenaidoo | 42dcdfd | 2021-10-19 17:34:12 -0400 | [diff] [blame] | 362 | func (a *adapter) addAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler adapter_service.AdapterServiceServer) { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 363 | logger.Info(ctx, "adding-adapter-service") |
| 364 | |
| 365 | server.AddService(func(gs *grpc.Server) { |
khenaidoo | 42dcdfd | 2021-10-19 17:34:12 -0400 | [diff] [blame] | 366 | adapter_service.RegisterAdapterServiceServer(gs, handler) |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 367 | }) |
| 368 | } |
| 369 | |
khenaidoo | 42dcdfd | 2021-10-19 17:34:12 -0400 | [diff] [blame] | 370 | func (a *adapter) addOnuInterAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler onu_inter_adapter_service.OnuInterAdapterServiceServer) { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 371 | logger.Info(ctx, "adding-onu-inter-adapter-service") |
| 372 | |
| 373 | server.AddService(func(gs *grpc.Server) { |
khenaidoo | 42dcdfd | 2021-10-19 17:34:12 -0400 | [diff] [blame] | 374 | onu_inter_adapter_service.RegisterOnuInterAdapterServiceServer(gs, handler) |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 375 | }) |
| 376 | } |
| 377 | |
Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 378 | /* |
| 379 | * |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 380 | This function checks the liveliness and readiness of the kakfa and kv-client services |
| 381 | and update the status in the probe. |
| 382 | */ |
| 383 | func (a *adapter) checkServicesReadiness(ctx context.Context) { |
| 384 | // checks the kafka readiness |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 385 | go kafka.MonitorKafkaReadiness(ctx, a.kafkaClient, a.config.LiveProbeInterval, a.config.NotLiveProbeInterval, clusterMessagingService) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 386 | |
| 387 | // checks the kv-store readiness |
| 388 | go a.checkKvStoreReadiness(ctx) |
| 389 | } |
| 390 | |
Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 391 | /* |
| 392 | * |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 393 | This function checks the liveliness and readiness of the kv-store service |
| 394 | and update the status in the probe. |
| 395 | */ |
| 396 | func (a *adapter) checkKvStoreReadiness(ctx context.Context) { |
| 397 | // dividing the live probe interval by 2 to get updated status every 30s |
| 398 | timeout := a.config.LiveProbeInterval / 2 |
| 399 | kvStoreChannel := make(chan bool, 1) |
| 400 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 401 | // Default true - we are here only after we already had a KV store connection |
| 402 | kvStoreChannel <- true |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 403 | for { |
| 404 | timeoutTimer := time.NewTimer(timeout) |
| 405 | select { |
| 406 | case liveliness := <-kvStoreChannel: |
| 407 | if !liveliness { |
| 408 | // kv-store not reachable or down, updating the status to not ready state |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 409 | probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusNotReady) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 410 | timeout = a.config.NotLiveProbeInterval |
| 411 | } else { |
| 412 | // kv-store is reachable , updating the status to running state |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 413 | probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusRunning) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 414 | timeout = a.config.LiveProbeInterval / 2 |
| 415 | } |
| 416 | // Check if the timer has expired or not |
| 417 | if !timeoutTimer.Stop() { |
| 418 | <-timeoutTimer.C |
| 419 | } |
| 420 | case <-timeoutTimer.C: |
| 421 | // Check the status of the kv-store |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 422 | logger.Info(ctx, "kv-store liveliness-recheck") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 423 | if a.kvClient.IsConnectionUp(ctx) { |
| 424 | kvStoreChannel <- true |
| 425 | } else { |
| 426 | kvStoreChannel <- false |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 432 | // WaitUntilKvStoreConnectionIsUp waits until the KV client can establish a connection to the KV server or until the |
| 433 | // context times out. |
| 434 | func WaitUntilKvStoreConnectionIsUp(ctx context.Context, kvClient kvstore.Client, connectionRetryInterval time.Duration, serviceName string) error { |
| 435 | if kvClient == nil { |
| 436 | return errors.New("kvclient-is-nil") |
| 437 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 438 | for { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 439 | if !kvClient.IsConnectionUp(ctx) { |
| 440 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusNotReady) |
| 441 | logger.Warnw(ctx, "kvconnection-down", log.Fields{"service-name": serviceName, "connect-retry-interval": connectionRetryInterval}) |
| 442 | select { |
| 443 | case <-time.After(connectionRetryInterval): |
| 444 | continue |
| 445 | case <-ctx.Done(): |
| 446 | return ctx.Err() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 449 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
| 450 | logger.Info(ctx, "kv-connection-up") |
| 451 | break |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 452 | } |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 453 | return nil |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // Adapter Utility methods ##### end ######### |
| 457 | // ############################################# |
| 458 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 459 | func getVerifiedCodeVersion(ctx context.Context) string { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 460 | if version.VersionInfo.Version == "unknown-version" { |
| 461 | content, err := ioutil.ReadFile("VERSION") |
| 462 | if err == nil { |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 463 | return string(content) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 464 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 465 | logger.Error(ctx, "'VERSION'-file not readable") |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 466 | } |
| 467 | return version.VersionInfo.Version |
| 468 | } |
| 469 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 470 | func printVersion(appName string) { |
| 471 | fmt.Println(appName) |
| 472 | fmt.Println(version.VersionInfo.String(" ")) |
| 473 | } |
| 474 | |
| 475 | func printBanner() { |
| 476 | fmt.Println(" ____ ____ ___ ___ _ ") |
| 477 | fmt.Println(" / __ \\ / __ \\| \\ \\ | | | | |") |
| 478 | fmt.Println(" | | | |_ __ ___ _ __ | | | | |\\ \\ | | | | | ____ ____") |
| 479 | fmt.Println(" | | | | '_ \\ / _ \\ '_ \\ | | | | | \\ \\ | | | | | / '_ \\ / _' \\") |
| 480 | fmt.Println(" | |__| | |_) | __/| | | || |__| | | \\ \\| | \\__/ || (__) | (__) |") |
| 481 | fmt.Println(" \\___ /| .__/ \\___|_| |_| \\____/|_| \\___|______| \\.___ |\\___./") |
| 482 | fmt.Println(" | | __| |") |
| 483 | fmt.Println(" |_| |____/") |
| 484 | fmt.Println(" ") |
| 485 | } |
| 486 | |
| 487 | func waitForExit(ctx context.Context) int { |
| 488 | signalChannel := make(chan os.Signal, 1) |
| 489 | signal.Notify(signalChannel, |
| 490 | syscall.SIGHUP, |
| 491 | syscall.SIGINT, |
| 492 | syscall.SIGTERM, |
| 493 | syscall.SIGQUIT) |
| 494 | |
| 495 | exitChannel := make(chan int) |
| 496 | |
| 497 | go func() { |
| 498 | select { |
| 499 | case <-ctx.Done(): |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 500 | logger.Infow(ctx, "Adapter run aborted due to internal errors", log.Fields{"context": "done"}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 501 | exitChannel <- 2 |
| 502 | case s := <-signalChannel: |
| 503 | switch s { |
| 504 | case syscall.SIGHUP, |
| 505 | syscall.SIGINT, |
| 506 | syscall.SIGTERM, |
| 507 | syscall.SIGQUIT: |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 508 | logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 509 | exitChannel <- 0 |
| 510 | default: |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 511 | logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 512 | exitChannel <- 1 |
| 513 | } |
| 514 | } |
| 515 | }() |
| 516 | |
| 517 | code := <-exitChannel |
| 518 | return code |
| 519 | } |
| 520 | |
| 521 | func main() { |
| 522 | start := time.Now() |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 523 | ctx, cancel := context.WithCancel(context.Background()) |
| 524 | defer cancel() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 525 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 526 | cf := &config.AdapterFlags{} |
| 527 | cf.ParseCommandArguments(os.Args[1:]) |
| 528 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 529 | defaultAppName := cf.InstanceID + "_" + getVerifiedCodeVersion(ctx) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 530 | |
| 531 | // Setup logging |
| 532 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 533 | logLevel, err := log.StringToLogLevel(cf.LogLevel) |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 534 | if err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 535 | logger.Fatalf(ctx, "Cannot setup logging, %s", err) |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 536 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 537 | |
| 538 | // Setup default logger - applies for packages that do not have specific logger set |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 539 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 540 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 543 | // Update all loggers (provisioned via init) with a common field |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 544 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 545 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 548 | log.SetAllLogLevel(logLevel) |
| 549 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 550 | realMain(ctx) //fatal on httpListen(0,6060) ... |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 551 | |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 552 | defer func() { |
| 553 | _ = log.CleanUp() |
| 554 | }() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 555 | // Print version / build information and exit |
| 556 | if cf.DisplayVersionOnly { |
| 557 | printVersion(defaultAppName) |
| 558 | return |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 559 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 560 | logger.Infow(ctx, "config", log.Fields{"StartName": defaultAppName}) |
| 561 | logger.Infow(ctx, "config", log.Fields{"BuildVersion": version.VersionInfo.String(" ")}) |
| 562 | logger.Infow(ctx, "config", log.Fields{"Arguments": os.Args[1:]}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 563 | |
| 564 | // Print banner if specified |
| 565 | if cf.Banner { |
| 566 | printBanner() |
| 567 | } |
| 568 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 569 | logger.Infow(ctx, "config", log.Fields{"config": *cf}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 570 | |
| 571 | ad := newAdapter(cf) |
| 572 | |
| 573 | p := &probe.Probe{} |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 574 | logger.Infow(ctx, "resources", log.Fields{"Context": ctx, "Adapter": ad.instanceID, "ProbeCoreState": p.GetStatus("register-with-core")}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 575 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 576 | go p.ListenAndServe(ctx, fmt.Sprintf("%s:%d", ad.config.ProbeHost, ad.config.ProbePort)) |
| 577 | logger.Infow(ctx, "probeState", log.Fields{"ProbeCoreState": p.GetStatus("register-with-core")}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 578 | |
| 579 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 580 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 581 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled) |
| 582 | if err != nil { |
| 583 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 584 | } else { |
| 585 | defer log.TerminateTracing(closer) |
| 586 | } |
| 587 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 588 | go func() { |
| 589 | err := ad.start(probeCtx) |
| 590 | // If this operation returns an error |
| 591 | // cancel all operations using this context |
| 592 | if err != nil { |
| 593 | cancel() |
| 594 | } |
| 595 | }() |
| 596 | |
| 597 | code := waitForExit(ctx) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 598 | logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 599 | |
khenaidoo | 1fd58e0 | 2021-10-13 11:51:20 -0400 | [diff] [blame] | 600 | // Set the ONU adapter GRPC service as not ready. This will prevent any request from coming to this adapter instance |
| 601 | probe.UpdateStatusFromContext(probeCtx, onuAdapterService, probe.ServiceStatusStopped) |
| 602 | |
Girish Gowdra | 5550783 | 2022-06-01 18:12:06 -0700 | [diff] [blame] | 603 | // Use context with cancel as etcd-client stop could take more time sometimes to stop slowing down container shutdown. |
| 604 | ctxWithCancel, cancelFunc := context.WithCancel(ctx) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 605 | // Cleanup before leaving |
Girish Gowdra | 5550783 | 2022-06-01 18:12:06 -0700 | [diff] [blame] | 606 | ad.stop(ctxWithCancel) |
| 607 | // Will halt any long-running stop routine gracefully |
| 608 | cancelFunc() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 609 | |
| 610 | elapsed := time.Since(start) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 611 | logger.Infow(ctx, "run-time", log.Fields{"Name": "openadapter", "time": elapsed / time.Microsecond}) |
| 612 | //logger.Infow(ctx,"run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 613 | } |