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