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