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