cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1 | /* |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 3 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 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 |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 7 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 9 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 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. |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 15 | */ |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 16 | |
| 17 | //Package main invokes the application |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 18 | package main |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "errors" |
| 23 | "fmt" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 24 | "os" |
| 25 | "os/signal" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 26 | "syscall" |
| 27 | "time" |
| 28 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 29 | "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 Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 39 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/config" |
| 40 | ac "github.com/opencord/voltha-openolt-adapter/internal/pkg/core" |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 41 | "github.com/opencord/voltha-protos/v5/go/adapter_service" |
| 42 | 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" |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 46 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 47 | "google.golang.org/grpc" |
| 48 | ) |
| 49 | |
| 50 | const ( |
| 51 | clusterMessagingService = "cluster-message-service" |
| 52 | oltAdapterService = "olt-adapter-service" |
| 53 | kvService = "kv-service" |
| 54 | coreService = "core-service" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 55 | ) |
| 56 | |
| 57 | type adapter struct { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 58 | 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 |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 68 | } |
| 69 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 70 | func newAdapter(cf *config.AdapterFlags) *adapter { |
| 71 | var a adapter |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 72 | a.instanceID = cf.InstanceID |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 73 | a.config = cf |
| 74 | a.halted = false |
| 75 | a.exitChannel = make(chan int, 1) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 76 | return &a |
| 77 | } |
| 78 | |
| 79 | func (a *adapter) start(ctx context.Context) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 80 | logger.Info(ctx, "Starting Core Adapter components") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 81 | var err error |
| 82 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 83 | 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 Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 88 | ctx, |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 89 | clusterMessagingService, |
| 90 | kvService, |
| 91 | oltAdapterService, |
| 92 | coreService, |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 93 | ) |
| 94 | } |
| 95 | } |
| 96 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 97 | // Setup KV Client |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 98 | 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}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 103 | if p != nil { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 104 | p.UpdateStatus(ctx, kvService, probe.ServiceStatusRunning) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 105 | } |
| 106 | |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 107 | // Setup Log Config |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 108 | cm := conf.NewConfigManager(ctx, a.kvClient, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout) |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 109 | |
divyadesai | d26f6b1 | 2020-03-19 06:30:28 +0000 | [diff] [blame] | 110 | go conf.StartLogLevelConfigProcessing(cm, ctx) |
Girish Kumar | 935f7af | 2020-08-18 11:59:42 +0000 | [diff] [blame] | 111 | go conf.StartLogFeaturesConfigProcessing(cm, ctx) |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 112 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 113 | // Setup Kafka Client |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 114 | if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaClusterAddress); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 115 | logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 116 | } |
| 117 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 118 | // 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 Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 123 | // Create the event proxy to post events to KAFKA |
Himani Chawla | cd40780 | 2020-12-10 12:08:59 +0530 | [diff] [blame] | 124 | a.eventProxy = events.NewEventProxy(events.MsgClient(a.kafkaClient), events.MsgTopic(kafka.Topic{Name: a.config.EventTopic})) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 125 | 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 |
| 133 | if a.coreClient, err = vgrpc.NewClient(a.config.CoreEndpoint, |
| 134 | a.coreRestarted, |
| 135 | vgrpc.ActivityCheck(true)); err != nil { |
| 136 | logger.Fatal(ctx, "grpc-client-not-created") |
| 137 | } |
| 138 | // Start the core grpc client |
| 139 | go a.coreClient.Start(ctx, setAndTestCoreServiceHandler) |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 140 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 141 | // Create the open OLT adapter |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 142 | if a.oltAdapter, err = a.startOpenOLT(ctx, a.coreClient, a.eventProxy, a.config, cm); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 143 | logger.Fatalw(ctx, "error-starting-openolt", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 144 | } |
| 145 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 146 | // Create and start the grpc server |
| 147 | a.grpcServer = vgrpc.NewGrpcServer(a.config.GrpcAddress, nil, false, p) |
| 148 | |
| 149 | //Register the adapter service |
| 150 | a.addAdapterService(ctx, a.grpcServer, a.oltAdapter) |
| 151 | |
| 152 | //Register the olt inter-adapter service |
| 153 | a.addOltInterAdapterService(ctx, a.grpcServer, a.oltAdapter) |
| 154 | |
| 155 | // Start the grpc server |
| 156 | go a.startGRPCService(ctx, a.grpcServer, oltAdapterService) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 157 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 158 | // Register this adapter to the Core - retries indefinitely |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 159 | if err = a.registerWithCore(ctx, coreService, -1); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 160 | logger.Fatal(ctx, "error-registering-with-core") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 161 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 162 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 163 | // check the readiness and liveliness and update the probe status |
| 164 | a.checkServicesReadiness(ctx) |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 165 | } |
| 166 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 167 | // TODO: Any action the adapter needs to do following a Core restart? |
| 168 | func (a *adapter) coreRestarted(ctx context.Context, endPoint string) error { |
| 169 | logger.Errorw(ctx, "core-restarted", log.Fields{"endpoint": endPoint}) |
| 170 | return nil |
| 171 | } |
| 172 | |
| 173 | // setAndTestCoreServiceHandler is used to test whether the remote gRPC service is up |
| 174 | func setAndTestCoreServiceHandler(ctx context.Context, conn *grpc.ClientConn) interface{} { |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 175 | svc := core_service.NewCoreServiceClient(conn) |
| 176 | if h, err := svc.GetHealthStatus(ctx, &empty.Empty{}); err != nil || h.State != health.HealthStatus_HEALTHY { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 177 | return nil |
| 178 | } |
| 179 | return svc |
| 180 | } |
| 181 | |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 182 | /** |
| 183 | This function checks the liveliness and readiness of the kakfa and kv-client services |
| 184 | and update the status in the probe. |
| 185 | */ |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 186 | func (a *adapter) checkServicesReadiness(ctx context.Context) { |
| 187 | // checks the kafka readiness |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 188 | go kafka.MonitorKafkaReadiness(ctx, a.kafkaClient, a.config.LiveProbeInterval, a.config.NotLiveProbeInterval, clusterMessagingService) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 189 | |
| 190 | // checks the kv-store readiness |
| 191 | go a.checkKvStoreReadiness(ctx) |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | This function checks the liveliness and readiness of the kv-store service |
| 196 | and update the status in the probe. |
| 197 | */ |
| 198 | func (a *adapter) checkKvStoreReadiness(ctx context.Context) { |
| 199 | // dividing the live probe interval by 2 to get updated status every 30s |
| 200 | timeout := a.config.LiveProbeInterval / 2 |
| 201 | kvStoreChannel := make(chan bool, 1) |
| 202 | |
| 203 | // Default false to check the liveliness. |
| 204 | kvStoreChannel <- false |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 205 | for { |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 206 | timeoutTimer := time.NewTimer(timeout) |
| 207 | select { |
| 208 | case liveliness := <-kvStoreChannel: |
| 209 | if !liveliness { |
| 210 | // kv-store not reachable or down, updating the status to not ready state |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 211 | probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusNotReady) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 212 | timeout = a.config.NotLiveProbeInterval |
| 213 | } else { |
| 214 | // kv-store is reachable , updating the status to running state |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 215 | probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusRunning) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 216 | timeout = a.config.LiveProbeInterval / 2 |
| 217 | } |
| 218 | // Check if the timer has expired or not |
| 219 | if !timeoutTimer.Stop() { |
| 220 | <-timeoutTimer.C |
| 221 | } |
| 222 | case <-timeoutTimer.C: |
Girish Kumar | beadc11 | 2020-02-26 18:41:02 +0000 | [diff] [blame] | 223 | // Check the status of the kv-store. Use timeout of 2 seconds to avoid forever blocking |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 224 | logger.Info(ctx, "kv-store liveliness-recheck") |
Girish Kumar | beadc11 | 2020-02-26 18:41:02 +0000 | [diff] [blame] | 225 | timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second) |
| 226 | |
| 227 | kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx) |
| 228 | // Cleanup cancel func resources |
| 229 | cancelFunc() |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 230 | } |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 234 | func (a *adapter) stop(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 235 | // Stop leadership tracking |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 236 | a.halted = true |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 237 | |
| 238 | // send exit signal |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 239 | a.exitChannel <- 0 |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 240 | |
| 241 | // Cleanup - applies only if we had a kvClient |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 242 | if a.kvClient != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 243 | // Release all reservations |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 244 | if err := a.kvClient.ReleaseAllReservations(ctx); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 245 | logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 246 | } |
| 247 | // Close the DB connection |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 248 | a.kvClient.Close(ctx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 249 | } |
| 250 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 251 | if a.eventProxy != nil { |
| 252 | a.eventProxy.Stop() |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 253 | } |
| 254 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 255 | if a.kafkaClient != nil { |
| 256 | a.kafkaClient.Stop(ctx) |
| 257 | } |
| 258 | |
| 259 | // Stop core client |
| 260 | if a.coreClient != nil { |
| 261 | a.coreClient.Stop(ctx) |
| 262 | } |
| 263 | |
| 264 | // TODO: Stop child devices connections |
| 265 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 266 | // TODO: More cleanup |
| 267 | } |
| 268 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 269 | func newKVClient(ctx context.Context, storeType, address string, timeout time.Duration) (kvstore.Client, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 270 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 271 | logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 272 | switch storeType { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 273 | case "etcd": |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 274 | return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 275 | } |
| 276 | return nil, errors.New("unsupported-kv-store") |
| 277 | } |
| 278 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 279 | func newKafkaClient(ctx context.Context, clientType, address string) (kafka.Client, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 280 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 281 | logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 282 | switch clientType { |
| 283 | case "sarama": |
| 284 | return kafka.NewSaramaClient( |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 285 | kafka.Address(address), |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 286 | kafka.ProducerReturnOnErrors(true), |
| 287 | kafka.ProducerReturnOnSuccess(true), |
| 288 | kafka.ProducerMaxRetries(6), |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 289 | kafka.ProducerRetryBackoff(time.Millisecond*30), |
| 290 | kafka.MetadatMaxRetries(15)), nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 291 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 292 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 293 | return nil, errors.New("unsupported-client-type") |
| 294 | } |
| 295 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 296 | func (a *adapter) setKVClient(ctx context.Context) error { |
| 297 | client, err := newKVClient(ctx, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 298 | if err != nil { |
| 299 | a.kvClient = nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 300 | return err |
| 301 | } |
| 302 | a.kvClient = client |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 303 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 304 | return nil |
| 305 | } |
| 306 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 307 | // startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server |
| 308 | func (a *adapter) startGRPCService(ctx context.Context, server *vgrpc.GrpcServer, serviceName string) { |
| 309 | logger.Infow(ctx, "starting-grpc-service", log.Fields{"service": serviceName}) |
| 310 | |
| 311 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
| 312 | logger.Infow(ctx, "grpc-service-started", log.Fields{"service": serviceName}) |
| 313 | |
| 314 | server.Start(ctx) |
| 315 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusStopped) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 316 | } |
| 317 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 318 | func (a *adapter) addAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler adapter_service.AdapterServiceServer) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 319 | logger.Info(ctx, "adding-adapter-service") |
| 320 | |
| 321 | server.AddService(func(gs *grpc.Server) { |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 322 | adapter_service.RegisterAdapterServiceServer(gs, handler) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 323 | }) |
| 324 | } |
| 325 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 326 | func (a *adapter) addOltInterAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler olt_inter_adapter_service.OltInterAdapterServiceServer) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 327 | logger.Info(ctx, "adding-olt-inter-adapter-service") |
| 328 | |
| 329 | server.AddService(func(gs *grpc.Server) { |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 330 | olt_inter_adapter_service.RegisterOltInterAdapterServiceServer(gs, handler) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 331 | }) |
| 332 | } |
| 333 | |
| 334 | func (a *adapter) startOpenOLT(ctx context.Context, cc *vgrpc.Client, ep eventif.EventProxy, |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 335 | cfg *config.AdapterFlags, cm *conf.ConfigManager) (*ac.OpenOLT, error) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 336 | logger.Info(ctx, "starting-open-olt") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 337 | var err error |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 338 | sOLT := ac.NewOpenOLT(ctx, cc, ep, cfg, cm) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 339 | |
| 340 | if err = sOLT.Start(ctx); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 341 | return nil, err |
| 342 | } |
| 343 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 344 | logger.Info(ctx, "open-olt-started") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 345 | return sOLT, nil |
| 346 | } |
| 347 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 348 | func (a *adapter) registerWithCore(ctx context.Context, serviceName string, retries int) error { |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 349 | adapterID := fmt.Sprintf("openolt_%d", a.config.CurrentReplica) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 350 | logger.Infow(ctx, "registering-with-core", log.Fields{ |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 351 | "adapterID": adapterID, |
| 352 | "currentReplica": a.config.CurrentReplica, |
| 353 | "totalReplicas": a.config.TotalReplicas, |
| 354 | }) |
| 355 | adapterDescription := &voltha.Adapter{ |
| 356 | Id: adapterID, // Unique name for the device type |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 357 | Vendor: "VOLTHA OpenOLT", |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 358 | Version: version.VersionInfo.Version, |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 359 | // The Endpoint refers to the address this service is listening on. |
| 360 | Endpoint: a.config.AdapterEndpoint, |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 361 | Type: "openolt", |
| 362 | CurrentReplica: int32(a.config.CurrentReplica), |
| 363 | TotalReplicas: int32(a.config.TotalReplicas), |
| 364 | } |
| 365 | types := []*voltha.DeviceType{{ |
| 366 | Id: "openolt", |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 367 | AdapterType: "openolt", // Type of the adapter that handles device type |
| 368 | Adapter: "openolt", // Deprecated attribute |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 369 | AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling |
| 370 | AcceptsAddRemoveFlowUpdates: true}} |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 371 | deviceTypes := &voltha.DeviceTypes{Items: types} |
| 372 | count := 0 |
| 373 | for { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 374 | gClient, err := a.coreClient.GetCoreServiceClient() |
| 375 | if gClient != nil { |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 376 | if _, err = gClient.RegisterAdapter(log.WithSpanFromContext(context.TODO(), ctx), &ca.AdapterRegistration{ |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 377 | Adapter: adapterDescription, |
| 378 | DTypes: deviceTypes}); err == nil { |
| 379 | break |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 380 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 381 | } |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 382 | logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"endpoint": a.config.CoreEndpoint, "error": err, "count": count, "gclient": gClient}) |
| 383 | if retries == count { |
| 384 | return err |
| 385 | } |
| 386 | count++ |
| 387 | // Take a nap before retrying |
| 388 | time.Sleep(2 * time.Second) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 389 | } |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 390 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 391 | logger.Info(ctx, "registered-with-core") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 392 | return nil |
| 393 | } |
| 394 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 395 | func waitForExit(ctx context.Context) int { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 396 | signalChannel := make(chan os.Signal, 1) |
| 397 | signal.Notify(signalChannel, |
| 398 | syscall.SIGHUP, |
| 399 | syscall.SIGINT, |
| 400 | syscall.SIGTERM, |
| 401 | syscall.SIGQUIT) |
| 402 | |
| 403 | exitChannel := make(chan int) |
| 404 | |
| 405 | go func() { |
| 406 | s := <-signalChannel |
| 407 | switch s { |
| 408 | case syscall.SIGHUP, |
| 409 | syscall.SIGINT, |
| 410 | syscall.SIGTERM, |
| 411 | syscall.SIGQUIT: |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 412 | logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 413 | exitChannel <- 0 |
| 414 | default: |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 415 | logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 416 | exitChannel <- 1 |
| 417 | } |
| 418 | }() |
| 419 | |
| 420 | code := <-exitChannel |
| 421 | return code |
| 422 | } |
| 423 | |
| 424 | func printBanner() { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 425 | fmt.Println(` ____ ____ _ _______ `) |
| 426 | fmt.Println(` / _ \ / __ \| | |__ __|`) |
| 427 | fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `) |
| 428 | fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `) |
| 429 | fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `) |
| 430 | fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `) |
| 431 | fmt.Println(` | | `) |
| 432 | fmt.Println(` |_| `) |
| 433 | fmt.Println(` `) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 436 | func printVersion() { |
| 437 | fmt.Println("VOLTHA OpenOLT Adapter") |
| 438 | fmt.Println(version.VersionInfo.String(" ")) |
| 439 | } |
| 440 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 441 | func main() { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 442 | ctx := context.Background() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 443 | start := time.Now() |
| 444 | |
| 445 | cf := config.NewAdapterFlags() |
| 446 | cf.ParseCommandArguments() |
| 447 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 448 | // Setup logging |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 449 | |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 450 | logLevel, err := log.StringToLogLevel(cf.LogLevel) |
| 451 | if err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 452 | logger.Fatalf(ctx, "Cannot setup logging, %s", err) |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 453 | } |
Rohan Agrawal | 2488f19 | 2020-01-31 09:26:55 +0000 | [diff] [blame] | 454 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 455 | // Setup default logger - applies for packages that do not have specific logger set |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 456 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil { |
Girish Kumar | a1ea2aa | 2020-08-19 18:14:22 +0000 | [diff] [blame] | 457 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // Update all loggers (provisionned via init) with a common field |
Hardik Windlass | b9c869b | 2019-10-10 08:34:32 +0000 | [diff] [blame] | 461 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil { |
Girish Kumar | a1ea2aa | 2020-08-19 18:14:22 +0000 | [diff] [blame] | 462 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 465 | log.SetAllLogLevel(logLevel) |
Rohan Agrawal | 93bced3 | 2020-02-11 10:16:01 +0000 | [diff] [blame] | 466 | |
Matteo Scandolo | 8f2b957 | 2020-02-28 15:35:23 -0800 | [diff] [blame] | 467 | realMain() |
| 468 | |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 469 | defer func() { |
| 470 | err := log.CleanUp() |
| 471 | if err != nil { |
| 472 | logger.Errorw(context.Background(), "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
| 473 | } |
| 474 | }() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 475 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 476 | // Print version / build information and exit |
| 477 | if cf.DisplayVersionOnly { |
| 478 | printVersion() |
| 479 | return |
| 480 | } |
| 481 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 482 | // Print banner if specified |
| 483 | if cf.Banner { |
| 484 | printBanner() |
| 485 | } |
| 486 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 487 | logger.Infow(ctx, "config", log.Fields{"config": *cf}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 488 | |
| 489 | ctx, cancel := context.WithCancel(context.Background()) |
| 490 | defer cancel() |
| 491 | |
| 492 | ad := newAdapter(cf) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 493 | |
| 494 | p := &probe.Probe{} |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 495 | go p.ListenAndServe(ctx, ad.config.ProbeAddress) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 496 | |
| 497 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 498 | |
Girish Kumar | 935f7af | 2020-08-18 11:59:42 +0000 | [diff] [blame] | 499 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled) |
Girish Kumar | 11e1597 | 2020-06-15 14:51:10 +0000 | [diff] [blame] | 500 | if err != nil { |
| 501 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 502 | } else { |
| 503 | defer log.TerminateTracing(closer) |
| 504 | } |
| 505 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 506 | go ad.start(probeCtx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 507 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 508 | code := waitForExit(ctx) |
| 509 | logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 510 | |
| 511 | // Cleanup before leaving |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 512 | ad.stop(ctx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 513 | |
| 514 | elapsed := time.Since(start) |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 515 | logger.Infow(ctx, "run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 516 | } |