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