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