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