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 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 29 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 30 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 31 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters" |
| 32 | com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common" |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 33 | conf "github.com/opencord/voltha-lib-go/v3/pkg/config" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 34 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 35 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 36 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 37 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 38 | "github.com/opencord/voltha-lib-go/v3/pkg/version" |
| 39 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/config" |
| 40 | ac "github.com/opencord/voltha-openolt-adapter/internal/pkg/core" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 41 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 42 | "github.com/opencord/voltha-protos/v3/go/voltha" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | type adapter struct { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 46 | instanceID string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 47 | config *config.AdapterFlags |
| 48 | iAdapter adapters.IAdapter |
| 49 | kafkaClient kafka.Client |
| 50 | kvClient kvstore.Client |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 51 | kip kafka.InterContainerProxy |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 52 | coreProxy adapterif.CoreProxy |
| 53 | adapterProxy adapterif.AdapterProxy |
| 54 | eventProxy adapterif.EventProxy |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 55 | halted bool |
| 56 | exitChannel chan int |
| 57 | receiverChannels []<-chan *ic.InterContainerMessage |
| 58 | } |
| 59 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 60 | func newAdapter(cf *config.AdapterFlags) *adapter { |
| 61 | var a adapter |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 62 | a.instanceID = cf.InstanceID |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 63 | a.config = cf |
| 64 | a.halted = false |
| 65 | a.exitChannel = make(chan int, 1) |
| 66 | a.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0) |
| 67 | return &a |
| 68 | } |
| 69 | |
| 70 | func (a *adapter) start(ctx context.Context) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 71 | logger.Info("Starting Core Adapter components") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 72 | var err error |
| 73 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 74 | var p *probe.Probe |
| 75 | if value := ctx.Value(probe.ProbeContextKey); value != nil { |
| 76 | if _, ok := value.(*probe.Probe); ok { |
| 77 | p = value.(*probe.Probe) |
| 78 | p.RegisterService( |
| 79 | "message-bus", |
| 80 | "kv-store", |
| 81 | "container-proxy", |
| 82 | "core-request-handler", |
| 83 | "register-with-core", |
| 84 | ) |
| 85 | } |
| 86 | } |
| 87 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 88 | // Setup KV Client |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 89 | logger.Debugw("create-kv-client", log.Fields{"kvstore": a.config.KVStoreType}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 90 | if err = a.setKVClient(); err != nil { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 91 | logger.Fatalw("error-setting-kv-client", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 94 | if p != nil { |
| 95 | p.UpdateStatus("kv-store", probe.ServiceStatusRunning) |
| 96 | } |
| 97 | |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 98 | // Setup Log Config |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 99 | cm := conf.NewConfigManager(a.kvClient, a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout) |
divyadesai | d26f6b1 | 2020-03-19 06:30:28 +0000 | [diff] [blame] | 100 | go conf.StartLogLevelConfigProcessing(cm, ctx) |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 101 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 102 | // Setup Kafka Client |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 103 | if a.kafkaClient, err = newKafkaClient("sarama", a.config.KafkaAdapterAddress); err != nil { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 104 | logger.Fatalw("Unsupported-common-client", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 107 | if p != nil { |
| 108 | p.UpdateStatus("message-bus", probe.ServiceStatusRunning) |
| 109 | } |
| 110 | |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 111 | // setup endpointManager |
| 112 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 113 | // Start the common InterContainer Proxy - retries indefinitely |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 114 | if a.kip, err = a.startInterContainerProxy(ctx, -1); err != nil { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 115 | logger.Fatal("error-starting-inter-container-proxy") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Create the core proxy to handle requests to the Core |
| 119 | a.coreProxy = com.NewCoreProxy(a.kip, a.config.Topic, a.config.CoreTopic) |
| 120 | |
| 121 | // Create the adaptor proxy to handle request between olt and onu |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 122 | a.adapterProxy = com.NewAdapterProxy(a.kip, "brcm_openomci_onu", a.config.CoreTopic, cm.Backend) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 123 | |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 124 | // Create the event proxy to post events to KAFKA |
| 125 | a.eventProxy = com.NewEventProxy(com.MsgClient(a.kafkaClient), com.MsgTopic(kafka.Topic{Name: a.config.EventTopic})) |
| 126 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 127 | // Create the open OLT adapter |
Girish Kumar | f26e488 | 2020-03-05 06:49:10 +0000 | [diff] [blame] | 128 | if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy, a.config); err != nil { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 129 | logger.Fatalw("error-starting-openolt", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Register the core request handler |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 133 | if err = a.setupRequestHandler(ctx, a.instanceID, a.iAdapter); err != nil { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 134 | logger.Fatalw("error-setting-core-request-handler", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 137 | // Register this adapter to the Core - retries indefinitely |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 138 | if err = a.registerWithCore(ctx, -1); err != nil { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 139 | logger.Fatal("error-registering-with-core") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 140 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 141 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 142 | // check the readiness and liveliness and update the probe status |
| 143 | a.checkServicesReadiness(ctx) |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | This function checks the liveliness and readiness of the kakfa and kv-client services |
| 148 | and update the status in the probe. |
| 149 | */ |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 150 | func (a *adapter) checkServicesReadiness(ctx context.Context) { |
| 151 | // checks the kafka readiness |
| 152 | go a.checkKafkaReadiness(ctx) |
| 153 | |
| 154 | // checks the kv-store readiness |
| 155 | go a.checkKvStoreReadiness(ctx) |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | This function checks the liveliness and readiness of the kv-store service |
| 160 | and update the status in the probe. |
| 161 | */ |
| 162 | func (a *adapter) checkKvStoreReadiness(ctx context.Context) { |
| 163 | // dividing the live probe interval by 2 to get updated status every 30s |
| 164 | timeout := a.config.LiveProbeInterval / 2 |
| 165 | kvStoreChannel := make(chan bool, 1) |
| 166 | |
| 167 | // Default false to check the liveliness. |
| 168 | kvStoreChannel <- false |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 169 | for { |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 170 | timeoutTimer := time.NewTimer(timeout) |
| 171 | select { |
| 172 | case liveliness := <-kvStoreChannel: |
| 173 | if !liveliness { |
| 174 | // kv-store not reachable or down, updating the status to not ready state |
| 175 | probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady) |
| 176 | timeout = a.config.NotLiveProbeInterval |
| 177 | } else { |
| 178 | // kv-store is reachable , updating the status to running state |
| 179 | probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning) |
| 180 | timeout = a.config.LiveProbeInterval / 2 |
| 181 | } |
| 182 | // Check if the timer has expired or not |
| 183 | if !timeoutTimer.Stop() { |
| 184 | <-timeoutTimer.C |
| 185 | } |
| 186 | case <-timeoutTimer.C: |
Girish Kumar | beadc11 | 2020-02-26 18:41:02 +0000 | [diff] [blame] | 187 | // Check the status of the kv-store. Use timeout of 2 seconds to avoid forever blocking |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 188 | logger.Info("kv-store liveliness-recheck") |
Girish Kumar | beadc11 | 2020-02-26 18:41:02 +0000 | [diff] [blame] | 189 | timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second) |
| 190 | |
| 191 | kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx) |
| 192 | // Cleanup cancel func resources |
| 193 | cancelFunc() |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 194 | } |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | This function checks the liveliness and readiness of the kafka service |
| 200 | and update the status in the probe. |
| 201 | */ |
| 202 | func (a *adapter) checkKafkaReadiness(ctx context.Context) { |
| 203 | livelinessChannel := a.kafkaClient.EnableLivenessChannel(true) |
Scott Baker | 86fce9a | 2019-12-12 09:47:17 -0800 | [diff] [blame] | 204 | healthinessChannel := a.kafkaClient.EnableHealthinessChannel(true) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 205 | timeout := a.config.LiveProbeInterval |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 206 | failed := false |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 207 | for { |
| 208 | timeoutTimer := time.NewTimer(timeout) |
| 209 | |
| 210 | select { |
Scott Baker | 86fce9a | 2019-12-12 09:47:17 -0800 | [diff] [blame] | 211 | case healthiness := <-healthinessChannel: |
| 212 | if !healthiness { |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 213 | // This will eventually cause K8s to restart the container, and will do |
| 214 | // so in a way that allows cleanup to continue, rather than an immediate |
| 215 | // panic and exit here. |
| 216 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusFailed) |
| 217 | failed = true |
| 218 | } |
| 219 | // Check if the timer has expired or not |
| 220 | if !timeoutTimer.Stop() { |
| 221 | <-timeoutTimer.C |
Scott Baker | 86fce9a | 2019-12-12 09:47:17 -0800 | [diff] [blame] | 222 | } |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 223 | case liveliness := <-livelinessChannel: |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 224 | if failed { |
| 225 | // Failures of the message bus are permanent and can't ever be recovered from, |
| 226 | // so make sure we never inadvertently reset a failed state back to unready. |
| 227 | } else if !liveliness { |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 228 | // kafka not reachable or down, updating the status to not ready state |
| 229 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady) |
| 230 | timeout = a.config.NotLiveProbeInterval |
| 231 | } else { |
| 232 | // kafka is reachable , updating the status to running state |
| 233 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning) |
| 234 | timeout = a.config.LiveProbeInterval |
| 235 | } |
| 236 | // Check if the timer has expired or not |
| 237 | if !timeoutTimer.Stop() { |
| 238 | <-timeoutTimer.C |
| 239 | } |
| 240 | case <-timeoutTimer.C: |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 241 | logger.Info("kafka-proxy-liveness-recheck") |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 242 | // send the liveness probe in a goroutine; we don't want to deadlock ourselves as |
| 243 | // the liveness probe may wait (and block) writing to our channel. |
| 244 | err := a.kafkaClient.SendLiveness() |
| 245 | if err != nil { |
| 246 | // Catch possible error case if sending liveness after Sarama has been stopped. |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 247 | logger.Warnw("error-kafka-send-liveness", log.Fields{"error": err}) |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 248 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 249 | } |
cbabu | 95f2152 | 2019-11-13 14:25:18 +0100 | [diff] [blame] | 250 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 251 | } |
| 252 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 253 | func (a *adapter) stop(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 254 | // Stop leadership tracking |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 255 | a.halted = true |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 256 | |
| 257 | // send exit signal |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 258 | a.exitChannel <- 0 |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 259 | |
| 260 | // Cleanup - applies only if we had a kvClient |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 261 | if a.kvClient != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 262 | // Release all reservations |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 263 | if err := a.kvClient.ReleaseAllReservations(ctx); err != nil { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 264 | logger.Infow("fail-to-release-all-reservations", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 265 | } |
| 266 | // Close the DB connection |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 267 | a.kvClient.Close() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 270 | if a.kip != nil { |
| 271 | a.kip.Stop() |
| 272 | } |
| 273 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 274 | // TODO: More cleanup |
| 275 | } |
| 276 | |
Neha Sharma | cc65696 | 2020-04-14 14:26:11 +0000 | [diff] [blame] | 277 | func newKVClient(storeType, address string, timeout time.Duration) (kvstore.Client, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 278 | |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 279 | logger.Infow("kv-store-type", log.Fields{"store": storeType}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 280 | switch storeType { |
| 281 | case "consul": |
| 282 | return kvstore.NewConsulClient(address, timeout) |
| 283 | case "etcd": |
Scott Baker | ed4a8e7 | 2020-04-17 11:10:20 -0700 | [diff] [blame] | 284 | return kvstore.NewEtcdClient(address, timeout, log.FatalLevel) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 285 | } |
| 286 | return nil, errors.New("unsupported-kv-store") |
| 287 | } |
| 288 | |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 289 | func newKafkaClient(clientType, address string) (kafka.Client, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 290 | |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 291 | logger.Infow("common-client-type", log.Fields{"client": clientType}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 292 | switch clientType { |
| 293 | case "sarama": |
| 294 | return kafka.NewSaramaClient( |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 295 | kafka.Address(address), |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 296 | kafka.ProducerReturnOnErrors(true), |
| 297 | kafka.ProducerReturnOnSuccess(true), |
| 298 | kafka.ProducerMaxRetries(6), |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 299 | kafka.ProducerRetryBackoff(time.Millisecond*30), |
| 300 | kafka.MetadatMaxRetries(15)), nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 301 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 302 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 303 | return nil, errors.New("unsupported-client-type") |
| 304 | } |
| 305 | |
| 306 | func (a *adapter) setKVClient() error { |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 307 | client, err := newKVClient(a.config.KVStoreType, a.config.KVStoreAddress, a.config.KVStoreTimeout) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 308 | if err != nil { |
| 309 | a.kvClient = nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 310 | return err |
| 311 | } |
| 312 | a.kvClient = client |
divyadesai | a37f78b | 2020-02-07 12:41:22 +0000 | [diff] [blame] | 313 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 314 | return nil |
| 315 | } |
| 316 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 317 | func (a *adapter) startInterContainerProxy(ctx context.Context, retries int) (kafka.InterContainerProxy, error) { |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 318 | logger.Infow("starting-intercontainer-messaging-proxy", log.Fields{"address": a.config.KafkaAdapterAddress, |
| 319 | "topic": a.config.Topic}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 320 | var err error |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 321 | kip := kafka.NewInterContainerProxy( |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 322 | kafka.InterContainerAddress(a.config.KafkaAdapterAddress), |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 323 | kafka.MsgClient(a.kafkaClient), |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 324 | kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic})) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 325 | count := 0 |
| 326 | for { |
| 327 | if err = kip.Start(); err != nil { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 328 | logger.Warnw("error-starting-messaging-proxy", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 329 | if retries == count { |
| 330 | return nil, err |
| 331 | } |
| 332 | count = +1 |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 333 | // Take a nap before retrying |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 334 | time.Sleep(2 * time.Second) |
| 335 | } else { |
| 336 | break |
| 337 | } |
| 338 | } |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 339 | probe.UpdateStatusFromContext(ctx, "container-proxy", probe.ServiceStatusRunning) |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 340 | logger.Info("common-messaging-proxy-created") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 341 | return kip, nil |
| 342 | } |
| 343 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 344 | func (a *adapter) startOpenOLT(ctx context.Context, kip kafka.InterContainerProxy, |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 345 | cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy, |
| 346 | cfg *config.AdapterFlags) (*ac.OpenOLT, error) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 347 | logger.Info("starting-open-olt") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 348 | var err error |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 349 | sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, ep, cfg) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 350 | |
| 351 | if err = sOLT.Start(ctx); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 352 | return nil, err |
| 353 | } |
| 354 | |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 355 | logger.Info("open-olt-started") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 356 | return sOLT, nil |
| 357 | } |
| 358 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 359 | func (a *adapter) setupRequestHandler(ctx context.Context, coreInstanceID string, iadapter adapters.IAdapter) error { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 360 | logger.Info("setting-request-handler") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 361 | requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 362 | if err := a.kip.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 363 | return err |
| 364 | |
| 365 | } |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 366 | probe.UpdateStatusFromContext(ctx, "core-request-handler", probe.ServiceStatusRunning) |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 367 | logger.Info("request-handler-setup-done") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 368 | return nil |
| 369 | } |
| 370 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 371 | func (a *adapter) registerWithCore(ctx context.Context, retries int) error { |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 372 | adapterID := fmt.Sprintf("openolt_%d", a.config.CurrentReplica) |
| 373 | logger.Infow("registering-with-core", log.Fields{ |
| 374 | "adapterID": adapterID, |
| 375 | "currentReplica": a.config.CurrentReplica, |
| 376 | "totalReplicas": a.config.TotalReplicas, |
| 377 | }) |
| 378 | adapterDescription := &voltha.Adapter{ |
| 379 | Id: adapterID, // Unique name for the device type |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 380 | Vendor: "VOLTHA OpenOLT", |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 381 | Version: version.VersionInfo.Version, |
| 382 | // TODO once we'll be ready to support multiple versions of the OpenOLT adapter |
| 383 | // the Endpoint will have to change to `openolt_<currentReplica`> |
serkant.uluderya | 5e3528d | 2020-05-22 19:31:07 -0700 | [diff] [blame] | 384 | Endpoint: a.config.Topic, |
Matteo Scandolo | 3ad5d2b | 2020-04-02 17:02:04 -0700 | [diff] [blame] | 385 | Type: "openolt", |
| 386 | CurrentReplica: int32(a.config.CurrentReplica), |
| 387 | TotalReplicas: int32(a.config.TotalReplicas), |
| 388 | } |
| 389 | types := []*voltha.DeviceType{{ |
| 390 | Id: "openolt", |
| 391 | Adapter: "openolt", // Type of the adapter that handles device type |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 392 | AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling |
| 393 | AcceptsAddRemoveFlowUpdates: true}} |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 394 | deviceTypes := &voltha.DeviceTypes{Items: types} |
| 395 | count := 0 |
| 396 | for { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 397 | if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 398 | logger.Warnw("registering-with-core-failed", log.Fields{"error": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 399 | if retries == count { |
| 400 | return err |
| 401 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 402 | count++ |
| 403 | // Take a nap before retrying |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 404 | time.Sleep(2 * time.Second) |
| 405 | } else { |
| 406 | break |
| 407 | } |
| 408 | } |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 409 | probe.UpdateStatusFromContext(ctx, "register-with-core", probe.ServiceStatusRunning) |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 410 | logger.Info("registered-with-core") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 411 | return nil |
| 412 | } |
| 413 | |
| 414 | func waitForExit() int { |
| 415 | signalChannel := make(chan os.Signal, 1) |
| 416 | signal.Notify(signalChannel, |
| 417 | syscall.SIGHUP, |
| 418 | syscall.SIGINT, |
| 419 | syscall.SIGTERM, |
| 420 | syscall.SIGQUIT) |
| 421 | |
| 422 | exitChannel := make(chan int) |
| 423 | |
| 424 | go func() { |
| 425 | s := <-signalChannel |
| 426 | switch s { |
| 427 | case syscall.SIGHUP, |
| 428 | syscall.SIGINT, |
| 429 | syscall.SIGTERM, |
| 430 | syscall.SIGQUIT: |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 431 | logger.Infow("closing-signal-received", log.Fields{"signal": s}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 432 | exitChannel <- 0 |
| 433 | default: |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 434 | logger.Infow("unexpected-signal-received", log.Fields{"signal": s}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 435 | exitChannel <- 1 |
| 436 | } |
| 437 | }() |
| 438 | |
| 439 | code := <-exitChannel |
| 440 | return code |
| 441 | } |
| 442 | |
| 443 | func printBanner() { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 444 | fmt.Println(` ____ ____ _ _______ `) |
| 445 | fmt.Println(` / _ \ / __ \| | |__ __|`) |
| 446 | fmt.Println(` | | | |_ __ ___ _ __ | | | | | | | `) |
| 447 | fmt.Println(` | | | | '_ \ / _ \ '_ \ | | | | | | | `) |
| 448 | fmt.Println(` | |__| | |_) | __/ | | || |__| | |____| | `) |
| 449 | fmt.Println(` \____/| .__/ \___|_| |_| \____/|______|_| `) |
| 450 | fmt.Println(` | | `) |
| 451 | fmt.Println(` |_| `) |
| 452 | fmt.Println(` `) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 455 | func printVersion() { |
| 456 | fmt.Println("VOLTHA OpenOLT Adapter") |
| 457 | fmt.Println(version.VersionInfo.String(" ")) |
| 458 | } |
| 459 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 460 | func main() { |
| 461 | start := time.Now() |
| 462 | |
| 463 | cf := config.NewAdapterFlags() |
| 464 | cf.ParseCommandArguments() |
| 465 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 466 | // Setup logging |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 467 | |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 468 | logLevel, err := log.StringToLogLevel(cf.LogLevel) |
| 469 | if err != nil { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 470 | logger.Fatalf("Cannot setup logging, %s", err) |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 471 | } |
Rohan Agrawal | 2488f19 | 2020-01-31 09:26:55 +0000 | [diff] [blame] | 472 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 473 | // 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] | 474 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 475 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 476 | } |
| 477 | |
| 478 | // Update all loggers (provisionned via init) with a common field |
Hardik Windlass | b9c869b | 2019-10-10 08:34:32 +0000 | [diff] [blame] | 479 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 480 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 481 | } |
| 482 | |
Rohan Agrawal | 02f784d | 2020-02-14 09:34:02 +0000 | [diff] [blame] | 483 | log.SetAllLogLevel(logLevel) |
Rohan Agrawal | 93bced3 | 2020-02-11 10:16:01 +0000 | [diff] [blame] | 484 | |
Matteo Scandolo | 8f2b957 | 2020-02-28 15:35:23 -0800 | [diff] [blame] | 485 | realMain() |
| 486 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 487 | defer log.CleanUp() |
| 488 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 489 | // Print version / build information and exit |
| 490 | if cf.DisplayVersionOnly { |
| 491 | printVersion() |
| 492 | return |
| 493 | } |
| 494 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 495 | // Print banner if specified |
| 496 | if cf.Banner { |
| 497 | printBanner() |
| 498 | } |
| 499 | |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 500 | logger.Infow("config", log.Fields{"config": *cf}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 501 | |
| 502 | ctx, cancel := context.WithCancel(context.Background()) |
| 503 | defer cancel() |
| 504 | |
| 505 | ad := newAdapter(cf) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 506 | |
| 507 | p := &probe.Probe{} |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 508 | go p.ListenAndServe(ad.config.ProbeAddress) |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 509 | |
| 510 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 511 | |
| 512 | go ad.start(probeCtx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 513 | |
| 514 | code := waitForExit() |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 515 | logger.Infow("received-a-closing-signal", log.Fields{"code": code}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 516 | |
| 517 | // Cleanup before leaving |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 518 | ad.stop(ctx) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 519 | |
| 520 | elapsed := time.Since(start) |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 521 | logger.Infow("run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 522 | } |