blob: a31b520ec982d4ef7ca97322ccc84eaf50191e01 [file] [log] [blame]
cuilin20187b2a8c32019-03-26 19:52:28 -07001/*
cbabu116b73f2019-12-10 17:56:32 +05302* Copyright 2018-present Open Networking Foundation
cuilin20187b2a8c32019-03-26 19:52:28 -07003
cbabu116b73f2019-12-10 17:56:32 +05304* 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
cuilin20187b2a8c32019-03-26 19:52:28 -07007
cbabu116b73f2019-12-10 17:56:32 +05308* http://www.apache.org/licenses/LICENSE-2.0
cuilin20187b2a8c32019-03-26 19:52:28 -07009
cbabu116b73f2019-12-10 17:56:32 +053010* 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.
cuilin20187b2a8c32019-03-26 19:52:28 -070015 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070016
17//Package main invokes the application
cuilin20187b2a8c32019-03-26 19:52:28 -070018package main
19
20import (
21 "context"
22 "errors"
23 "fmt"
kdarapu381c6902019-07-31 18:23:16 +053024 "os"
25 "os/signal"
26 "strconv"
27 "syscall"
28 "time"
29
Esin Karamanccb714b2019-11-29 15:02:06 +000030 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
kdarapu381c6902019-07-31 18:23:16 +053031
Esin Karamanccb714b2019-11-29 15:02:06 +000032 "github.com/opencord/voltha-lib-go/v3/pkg/adapters"
33 com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common"
divyadesaia37f78b2020-02-07 12:41:22 +000034 conf "github.com/opencord/voltha-lib-go/v3/pkg/config"
Esin Karamanccb714b2019-11-29 15:02:06 +000035 "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 Bakerdbd960e2020-02-28 08:57:51 -080039 "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 Karamanccb714b2019-11-29 15:02:06 +000042 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
43 "github.com/opencord/voltha-protos/v3/go/voltha"
cuilin20187b2a8c32019-03-26 19:52:28 -070044)
45
46type adapter struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070047 instanceID string
cuilin20187b2a8c32019-03-26 19:52:28 -070048 config *config.AdapterFlags
49 iAdapter adapters.IAdapter
50 kafkaClient kafka.Client
51 kvClient kvstore.Client
npujarec5762e2020-01-01 14:08:48 +053052 kip kafka.InterContainerProxy
kdarapu381c6902019-07-31 18:23:16 +053053 coreProxy adapterif.CoreProxy
54 adapterProxy adapterif.AdapterProxy
55 eventProxy adapterif.EventProxy
cuilin20187b2a8c32019-03-26 19:52:28 -070056 halted bool
57 exitChannel chan int
58 receiverChannels []<-chan *ic.InterContainerMessage
59}
60
cuilin20187b2a8c32019-03-26 19:52:28 -070061func newAdapter(cf *config.AdapterFlags) *adapter {
62 var a adapter
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070063 a.instanceID = cf.InstanceID
cuilin20187b2a8c32019-03-26 19:52:28 -070064 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
71func (a *adapter) start(ctx context.Context) {
Girish Kumar2ad402b2020-03-20 19:45:12 +000072 logger.Info("Starting Core Adapter components")
cuilin20187b2a8c32019-03-26 19:52:28 -070073 var err error
74
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000075 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
cuilin20187b2a8c32019-03-26 19:52:28 -070089 // Setup KV Client
Girish Kumar2ad402b2020-03-20 19:45:12 +000090 logger.Debugw("create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070091 if err = a.setKVClient(); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +000092 logger.Fatalw("error-setting-kv-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -070093 }
94
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000095 if p != nil {
96 p.UpdateStatus("kv-store", probe.ServiceStatusRunning)
97 }
98
divyadesaia37f78b2020-02-07 12:41:22 +000099 // Setup Log Config
100 cm := conf.NewConfigManager(a.kvClient, a.config.KVStoreType, a.config.KVStoreHost, a.config.KVStorePort, a.config.KVStoreTimeout)
divyadesaid26f6b12020-03-19 06:30:28 +0000101 go conf.StartLogLevelConfigProcessing(cm, ctx)
divyadesaia37f78b2020-02-07 12:41:22 +0000102
cuilin20187b2a8c32019-03-26 19:52:28 -0700103 // Setup Kafka Client
104 if a.kafkaClient, err = newKafkaClient("sarama", a.config.KafkaAdapterHost, a.config.KafkaAdapterPort); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000105 logger.Fatalw("Unsupported-common-client", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700106 }
107
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000108 if p != nil {
109 p.UpdateStatus("message-bus", probe.ServiceStatusRunning)
110 }
111
cuilin20187b2a8c32019-03-26 19:52:28 -0700112 // Start the common InterContainer Proxy - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000113 if a.kip, err = a.startInterContainerProxy(ctx, -1); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000114 logger.Fatal("error-starting-inter-container-proxy")
cuilin20187b2a8c32019-03-26 19:52:28 -0700115 }
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 Paulfb990a52019-07-09 10:01:49 -0400123 // 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
cuilin20187b2a8c32019-03-26 19:52:28 -0700126 // Create the open OLT adapter
Girish Kumarf26e4882020-03-05 06:49:10 +0000127 if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy, a.config); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000128 logger.Fatalw("error-starting-openolt", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700129 }
130
131 // Register the core request handler
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000132 if err = a.setupRequestHandler(ctx, a.instanceID, a.iAdapter); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000133 logger.Fatalw("error-setting-core-request-handler", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700134 }
135
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700136 // Register this adapter to the Core - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000137 if err = a.registerWithCore(ctx, -1); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000138 logger.Fatal("error-registering-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700139 }
cbabu95f21522019-11-13 14:25:18 +0100140
cbabu116b73f2019-12-10 17:56:32 +0530141 // check the readiness and liveliness and update the probe status
142 a.checkServicesReadiness(ctx)
cbabu95f21522019-11-13 14:25:18 +0100143}
144
145/**
146This function checks the liveliness and readiness of the kakfa and kv-client services
147and update the status in the probe.
148*/
cbabu116b73f2019-12-10 17:56:32 +0530149func (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/**
158This function checks the liveliness and readiness of the kv-store service
159and update the status in the probe.
160*/
161func (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
cbabu95f21522019-11-13 14:25:18 +0100168 for {
cbabu116b73f2019-12-10 17:56:32 +0530169 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 Kumarbeadc112020-02-26 18:41:02 +0000186 // Check the status of the kv-store. Use timeout of 2 seconds to avoid forever blocking
Girish Kumar2ad402b2020-03-20 19:45:12 +0000187 logger.Info("kv-store liveliness-recheck")
Girish Kumarbeadc112020-02-26 18:41:02 +0000188 timeoutCtx, cancelFunc := context.WithTimeout(ctx, 2*time.Second)
189
190 kvStoreChannel <- a.kvClient.IsConnectionUp(timeoutCtx)
191 // Cleanup cancel func resources
192 cancelFunc()
cbabu95f21522019-11-13 14:25:18 +0100193 }
cbabu116b73f2019-12-10 17:56:32 +0530194 }
195}
196
197/**
198This function checks the liveliness and readiness of the kafka service
199and update the status in the probe.
200*/
201func (a *adapter) checkKafkaReadiness(ctx context.Context) {
202 livelinessChannel := a.kafkaClient.EnableLivenessChannel(true)
Scott Baker86fce9a2019-12-12 09:47:17 -0800203 healthinessChannel := a.kafkaClient.EnableHealthinessChannel(true)
cbabu116b73f2019-12-10 17:56:32 +0530204 timeout := a.config.LiveProbeInterval
Scott Bakere701b862020-02-20 16:19:16 -0800205 failed := false
cbabu116b73f2019-12-10 17:56:32 +0530206 for {
207 timeoutTimer := time.NewTimer(timeout)
208
209 select {
Scott Baker86fce9a2019-12-12 09:47:17 -0800210 case healthiness := <-healthinessChannel:
211 if !healthiness {
Scott Bakere701b862020-02-20 16:19:16 -0800212 // 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 Baker86fce9a2019-12-12 09:47:17 -0800221 }
cbabu116b73f2019-12-10 17:56:32 +0530222 case liveliness := <-livelinessChannel:
Scott Bakere701b862020-02-20 16:19:16 -0800223 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 {
cbabu116b73f2019-12-10 17:56:32 +0530227 // 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 Kumar2ad402b2020-03-20 19:45:12 +0000240 logger.Info("kafka-proxy-liveness-recheck")
cbabu116b73f2019-12-10 17:56:32 +0530241 // 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 Kumar2ad402b2020-03-20 19:45:12 +0000246 logger.Warnw("error-kafka-send-liveness", log.Fields{"error": err})
cbabu116b73f2019-12-10 17:56:32 +0530247 }
cbabu95f21522019-11-13 14:25:18 +0100248 }
cbabu95f21522019-11-13 14:25:18 +0100249 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700250}
251
npujarec5762e2020-01-01 14:08:48 +0530252func (a *adapter) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700253 // Stop leadership tracking
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 a.halted = true
cuilin20187b2a8c32019-03-26 19:52:28 -0700255
256 // send exit signal
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700257 a.exitChannel <- 0
cuilin20187b2a8c32019-03-26 19:52:28 -0700258
259 // Cleanup - applies only if we had a kvClient
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700260 if a.kvClient != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700261 // Release all reservations
npujarec5762e2020-01-01 14:08:48 +0530262 if err := a.kvClient.ReleaseAllReservations(ctx); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000263 logger.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700264 }
265 // Close the DB connection
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700266 a.kvClient.Close()
cuilin20187b2a8c32019-03-26 19:52:28 -0700267 }
268
Scott Bakere701b862020-02-20 16:19:16 -0800269 if a.kip != nil {
270 a.kip.Stop()
271 }
272
cuilin20187b2a8c32019-03-26 19:52:28 -0700273 // TODO: More cleanup
274}
275
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700276func newKVClient(storeType, address string, timeout int) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700277
Girish Kumar2ad402b2020-03-20 19:45:12 +0000278 logger.Infow("kv-store-type", log.Fields{"store": storeType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700279 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 Gowdru6a80bbd2019-07-02 07:36:09 -0700288func newKafkaClient(clientType, host string, port int) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700289
Girish Kumar2ad402b2020-03-20 19:45:12 +0000290 logger.Infow("common-client-type", log.Fields{"client": clientType})
cuilin20187b2a8c32019-03-26 19:52:28 -0700291 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.L3b494632019-07-16 15:51:09 +0530299 kafka.ProducerRetryBackoff(time.Millisecond*30),
300 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700301 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700302
cuilin20187b2a8c32019-03-26 19:52:28 -0700303 return nil, errors.New("unsupported-client-type")
304}
305
306func (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
cuilin20187b2a8c32019-03-26 19:52:28 -0700311 return err
312 }
313 a.kvClient = client
divyadesaia37f78b2020-02-07 12:41:22 +0000314
cuilin20187b2a8c32019-03-26 19:52:28 -0700315 return nil
316}
317
npujarec5762e2020-01-01 14:08:48 +0530318func (a *adapter) startInterContainerProxy(ctx context.Context, retries int) (kafka.InterContainerProxy, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000319 logger.Infow("starting-intercontainer-messaging-proxy", log.Fields{"host": a.config.KafkaAdapterHost,
cuilin20187b2a8c32019-03-26 19:52:28 -0700320 "port": a.config.KafkaAdapterPort, "topic": a.config.Topic})
321 var err error
npujarec5762e2020-01-01 14:08:48 +0530322 kip := kafka.NewInterContainerProxy(
cuilin20187b2a8c32019-03-26 19:52:28 -0700323 kafka.InterContainerHost(a.config.KafkaAdapterHost),
324 kafka.InterContainerPort(a.config.KafkaAdapterPort),
325 kafka.MsgClient(a.kafkaClient),
npujarec5762e2020-01-01 14:08:48 +0530326 kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic}))
cuilin20187b2a8c32019-03-26 19:52:28 -0700327 count := 0
328 for {
329 if err = kip.Start(); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000330 logger.Warnw("error-starting-messaging-proxy", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700331 if retries == count {
332 return nil, err
333 }
334 count = +1
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700335 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700336 time.Sleep(2 * time.Second)
337 } else {
338 break
339 }
340 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000341 probe.UpdateStatusFromContext(ctx, "container-proxy", probe.ServiceStatusRunning)
Girish Kumar2ad402b2020-03-20 19:45:12 +0000342 logger.Info("common-messaging-proxy-created")
cuilin20187b2a8c32019-03-26 19:52:28 -0700343 return kip, nil
344}
345
npujarec5762e2020-01-01 14:08:48 +0530346func (a *adapter) startOpenOLT(ctx context.Context, kip kafka.InterContainerProxy,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530347 cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy,
348 cfg *config.AdapterFlags) (*ac.OpenOLT, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000349 logger.Info("starting-open-olt")
cuilin20187b2a8c32019-03-26 19:52:28 -0700350 var err error
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530351 sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, ep, cfg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700352
353 if err = sOLT.Start(ctx); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700354 return nil, err
355 }
356
Girish Kumar2ad402b2020-03-20 19:45:12 +0000357 logger.Info("open-olt-started")
cuilin20187b2a8c32019-03-26 19:52:28 -0700358 return sOLT, nil
359}
360
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000361func (a *adapter) setupRequestHandler(ctx context.Context, coreInstanceID string, iadapter adapters.IAdapter) error {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000362 logger.Info("setting-request-handler")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700363 requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy)
cuilin20187b2a8c32019-03-26 19:52:28 -0700364 if err := a.kip.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700365 return err
366
367 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000368 probe.UpdateStatusFromContext(ctx, "core-request-handler", probe.ServiceStatusRunning)
Girish Kumar2ad402b2020-03-20 19:45:12 +0000369 logger.Info("request-handler-setup-done")
cuilin20187b2a8c32019-03-26 19:52:28 -0700370 return nil
371}
372
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000373func (a *adapter) registerWithCore(ctx context.Context, retries int) error {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000374 logger.Info("registering-with-core")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 adapterDescription := &voltha.Adapter{Id: "openolt", // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400376 Vendor: "VOLTHA OpenOLT",
377 Version: version.VersionInfo.Version}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400378 types := []*voltha.DeviceType{{Id: "openolt",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700379 Adapter: "openolt", // Name of the adapter that handles device type
Girish Gowdru0c588b22019-04-23 23:24:56 -0400380 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
381 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700382 deviceTypes := &voltha.DeviceTypes{Items: types}
383 count := 0
384 for {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700385 if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000386 logger.Warnw("registering-with-core-failed", log.Fields{"error": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700387 if retries == count {
388 return err
389 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700390 count++
391 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700392 time.Sleep(2 * time.Second)
393 } else {
394 break
395 }
396 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000397 probe.UpdateStatusFromContext(ctx, "register-with-core", probe.ServiceStatusRunning)
Girish Kumar2ad402b2020-03-20 19:45:12 +0000398 logger.Info("registered-with-core")
cuilin20187b2a8c32019-03-26 19:52:28 -0700399 return nil
400}
401
402func 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 Kumar2ad402b2020-03-20 19:45:12 +0000419 logger.Infow("closing-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700420 exitChannel <- 0
421 default:
Girish Kumar2ad402b2020-03-20 19:45:12 +0000422 logger.Infow("unexpected-signal-received", log.Fields{"signal": s})
cuilin20187b2a8c32019-03-26 19:52:28 -0700423 exitChannel <- 1
424 }
425 }()
426
427 code := <-exitChannel
428 return code
429}
430
431func printBanner() {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800432 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(` `)
cuilin20187b2a8c32019-03-26 19:52:28 -0700441}
442
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400443func printVersion() {
444 fmt.Println("VOLTHA OpenOLT Adapter")
445 fmt.Println(version.VersionInfo.String(" "))
446}
447
cuilin20187b2a8c32019-03-26 19:52:28 -0700448func main() {
449 start := time.Now()
450
451 cf := config.NewAdapterFlags()
452 cf.ParseCommandArguments()
453
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700454 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700455
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000456 logLevel, err := log.StringToLogLevel(cf.LogLevel)
457 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000458 logger.Fatalf("Cannot setup logging, %s", err)
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000459 }
Rohan Agrawal2488f192020-01-31 09:26:55 +0000460
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461 // Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000462 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700463 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
464 }
465
466 // Update all loggers (provisionned via init) with a common field
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000467 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700468 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
469 }
470
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000471 log.SetAllLogLevel(logLevel)
Rohan Agrawal93bced32020-02-11 10:16:01 +0000472
Matteo Scandolo8f2b9572020-02-28 15:35:23 -0800473 realMain()
474
cuilin20187b2a8c32019-03-26 19:52:28 -0700475 defer log.CleanUp()
476
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400477 // Print version / build information and exit
478 if cf.DisplayVersionOnly {
479 printVersion()
480 return
481 }
482
cuilin20187b2a8c32019-03-26 19:52:28 -0700483 // Print banner if specified
484 if cf.Banner {
485 printBanner()
486 }
487
Girish Kumar2ad402b2020-03-20 19:45:12 +0000488 logger.Infow("config", log.Fields{"config": *cf})
cuilin20187b2a8c32019-03-26 19:52:28 -0700489
490 ctx, cancel := context.WithCancel(context.Background())
491 defer cancel()
492
493 ad := newAdapter(cf)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000494
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)
cuilin20187b2a8c32019-03-26 19:52:28 -0700501
502 code := waitForExit()
Girish Kumar2ad402b2020-03-20 19:45:12 +0000503 logger.Infow("received-a-closing-signal", log.Fields{"code": code})
cuilin20187b2a8c32019-03-26 19:52:28 -0700504
505 // Cleanup before leaving
npujarec5762e2020-01-01 14:08:48 +0530506 ad.stop(ctx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700507
508 elapsed := time.Since(start)
Girish Kumar2ad402b2020-03-20 19:45:12 +0000509 logger.Infow("run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700510}