blob: eaebd9a106c9b2cb756a92bf983ad435f0659acb [file] [log] [blame]
cuilin20187b2a8c32019-03-26 19:52:28 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
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
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
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
Scott Baker51290152019-10-24 14:23:20 -070030 "github.com/opencord/voltha-lib-go/v2/pkg/adapters/adapterif"
kdarapu381c6902019-07-31 18:23:16 +053031
Scott Baker51290152019-10-24 14:23:20 -070032 "github.com/opencord/voltha-lib-go/v2/pkg/adapters"
33 com "github.com/opencord/voltha-lib-go/v2/pkg/adapters/common"
34 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
35 "github.com/opencord/voltha-lib-go/v2/pkg/kafka"
36 "github.com/opencord/voltha-lib-go/v2/pkg/log"
37 "github.com/opencord/voltha-lib-go/v2/pkg/probe"
Girish Gowdru0c588b22019-04-23 23:24:56 -040038 ac "github.com/opencord/voltha-openolt-adapter/adaptercore"
39 "github.com/opencord/voltha-openolt-adapter/config"
Matt Jeanneret0c9ae282019-07-18 18:14:28 -040040 "github.com/opencord/voltha-openolt-adapter/config/version"
manikkaraj kbf256be2019-03-25 00:13:48 +053041 ic "github.com/opencord/voltha-protos/go/inter_container"
42 "github.com/opencord/voltha-protos/go/voltha"
cuilin20187b2a8c32019-03-26 19:52:28 -070043)
44
45type adapter struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070046 instanceID string
cuilin20187b2a8c32019-03-26 19:52:28 -070047 config *config.AdapterFlags
48 iAdapter adapters.IAdapter
49 kafkaClient kafka.Client
50 kvClient kvstore.Client
51 kip *kafka.InterContainerProxy
kdarapu381c6902019-07-31 18:23:16 +053052 coreProxy adapterif.CoreProxy
53 adapterProxy adapterif.AdapterProxy
54 eventProxy adapterif.EventProxy
cuilin20187b2a8c32019-03-26 19:52:28 -070055 halted bool
56 exitChannel chan int
57 receiverChannels []<-chan *ic.InterContainerMessage
58}
59
60func init() {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070061 _, _ = log.AddPackage(log.JSON, log.DebugLevel, nil)
cuilin20187b2a8c32019-03-26 19:52:28 -070062}
63
64func newAdapter(cf *config.AdapterFlags) *adapter {
65 var a adapter
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070066 a.instanceID = cf.InstanceID
cuilin20187b2a8c32019-03-26 19:52:28 -070067 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
74func (a *adapter) start(ctx context.Context) {
75 log.Info("Starting Core Adapter components")
76 var err error
77
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000078 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
cuilin20187b2a8c32019-03-26 19:52:28 -070092 // Setup KV Client
93 log.Debugw("create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070094 if err = a.setKVClient(); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -070095 log.Fatal("error-setting-kv-client")
96 }
97
Rohan Agrawal828bf4e2019-10-22 10:13:19 +000098 if p != nil {
99 p.UpdateStatus("kv-store", probe.ServiceStatusRunning)
100 }
101
cuilin20187b2a8c32019-03-26 19:52:28 -0700102 // 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 Agrawal828bf4e2019-10-22 10:13:19 +0000107 if p != nil {
108 p.UpdateStatus("message-bus", probe.ServiceStatusRunning)
109 }
110
cuilin20187b2a8c32019-03-26 19:52:28 -0700111 // Start the common InterContainer Proxy - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000112 if a.kip, err = a.startInterContainerProxy(ctx, -1); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700113 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 Paulfb990a52019-07-09 10:01:49 -0400122 // 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
cuilin20187b2a8c32019-03-26 19:52:28 -0700125 // Create the open OLT adapter
kdarapu381c6902019-07-31 18:23:16 +0530126 if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy,
127 a.config.OnuNumber,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700128 a.config.KVStoreHost, a.config.KVStorePort, a.config.KVStoreType); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700129 log.Fatal("error-starting-inter-container-proxy")
130 }
131
132 // Register the core request handler
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000133 if err = a.setupRequestHandler(ctx, a.instanceID, a.iAdapter); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700134 log.Fatal("error-setting-core-request-handler")
135 }
136
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700137 // Register this adapter to the Core - retries indefinitely
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000138 if err = a.registerWithCore(ctx, -1); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700139 log.Fatal("error-registering-with-core")
140 }
141}
142
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700143func (a *adapter) stop() {
cuilin20187b2a8c32019-03-26 19:52:28 -0700144 // Stop leadership tracking
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700145 a.halted = true
cuilin20187b2a8c32019-03-26 19:52:28 -0700146
147 // send exit signal
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700148 a.exitChannel <- 0
cuilin20187b2a8c32019-03-26 19:52:28 -0700149
150 // Cleanup - applies only if we had a kvClient
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700151 if a.kvClient != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700152 // Release all reservations
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700153 if err := a.kvClient.ReleaseAllReservations(); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700154 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
155 }
156 // Close the DB connection
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700157 a.kvClient.Close()
cuilin20187b2a8c32019-03-26 19:52:28 -0700158 }
159
160 // TODO: More cleanup
161}
162
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700163func newKVClient(storeType, address string, timeout int) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700164
165 log.Infow("kv-store-type", log.Fields{"store": storeType})
166 switch storeType {
167 case "consul":
168 return kvstore.NewConsulClient(address, timeout)
169 case "etcd":
170 return kvstore.NewEtcdClient(address, timeout)
171 }
172 return nil, errors.New("unsupported-kv-store")
173}
174
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700175func newKafkaClient(clientType, host string, port int) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700176
177 log.Infow("common-client-type", log.Fields{"client": clientType})
178 switch clientType {
179 case "sarama":
180 return kafka.NewSaramaClient(
181 kafka.Host(host),
182 kafka.Port(port),
183 kafka.ProducerReturnOnErrors(true),
184 kafka.ProducerReturnOnSuccess(true),
185 kafka.ProducerMaxRetries(6),
Abhilash S.L3b494632019-07-16 15:51:09 +0530186 kafka.ProducerRetryBackoff(time.Millisecond*30),
187 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700188 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700189
cuilin20187b2a8c32019-03-26 19:52:28 -0700190 return nil, errors.New("unsupported-client-type")
191}
192
193func (a *adapter) setKVClient() error {
194 addr := a.config.KVStoreHost + ":" + strconv.Itoa(a.config.KVStorePort)
195 client, err := newKVClient(a.config.KVStoreType, addr, a.config.KVStoreTimeout)
196 if err != nil {
197 a.kvClient = nil
198 log.Error(err)
199 return err
200 }
201 a.kvClient = client
202 return nil
203}
204
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000205func (a *adapter) startInterContainerProxy(ctx context.Context, retries int) (*kafka.InterContainerProxy, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700206 log.Infow("starting-intercontainer-messaging-proxy", log.Fields{"host": a.config.KafkaAdapterHost,
207 "port": a.config.KafkaAdapterPort, "topic": a.config.Topic})
208 var err error
209 var kip *kafka.InterContainerProxy
210 if kip, err = kafka.NewInterContainerProxy(
211 kafka.InterContainerHost(a.config.KafkaAdapterHost),
212 kafka.InterContainerPort(a.config.KafkaAdapterPort),
213 kafka.MsgClient(a.kafkaClient),
214 kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic})); err != nil {
215 log.Errorw("fail-to-create-common-proxy", log.Fields{"error": err})
216 return nil, err
217 }
218 count := 0
219 for {
220 if err = kip.Start(); err != nil {
221 log.Warnw("error-starting-messaging-proxy", log.Fields{"error": err})
222 if retries == count {
223 return nil, err
224 }
225 count = +1
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700226 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700227 time.Sleep(2 * time.Second)
228 } else {
229 break
230 }
231 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000232 probe.UpdateStatusFromContext(ctx, "container-proxy", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700233 log.Info("common-messaging-proxy-created")
234 return kip, nil
235}
236
kdarapu381c6902019-07-31 18:23:16 +0530237func (a *adapter) startOpenOLT(ctx context.Context, kip *kafka.InterContainerProxy,
238 cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy, onuNumber int, kvStoreHost string,
239 kvStorePort int, KVStoreType string) (*ac.OpenOLT, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700240 log.Info("starting-open-olt")
241 var err error
Devmalya Paulfb990a52019-07-09 10:01:49 -0400242 sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, ep, onuNumber, kvStoreHost, kvStorePort, KVStoreType)
cuilin20187b2a8c32019-03-26 19:52:28 -0700243
244 if err = sOLT.Start(ctx); err != nil {
245 log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err})
246 return nil, err
247 }
248
249 log.Info("open-olt-started")
250 return sOLT, nil
251}
252
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000253func (a *adapter) setupRequestHandler(ctx context.Context, coreInstanceID string, iadapter adapters.IAdapter) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700254 log.Info("setting-request-handler")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700255 requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy)
cuilin20187b2a8c32019-03-26 19:52:28 -0700256 if err := a.kip.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
257 log.Errorw("request-handler-setup-failed", log.Fields{"error": err})
258 return err
259
260 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000261 probe.UpdateStatusFromContext(ctx, "core-request-handler", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700262 log.Info("request-handler-setup-done")
263 return nil
264}
265
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000266func (a *adapter) registerWithCore(ctx context.Context, retries int) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700267 log.Info("registering-with-core")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400268 adapterDescription := &voltha.Adapter{Id: "openolt", // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400269 Vendor: "VOLTHA OpenOLT",
270 Version: version.VersionInfo.Version}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400271 types := []*voltha.DeviceType{{Id: "openolt",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700272 Adapter: "openolt", // Name of the adapter that handles device type
Girish Gowdru0c588b22019-04-23 23:24:56 -0400273 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
274 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700275 deviceTypes := &voltha.DeviceTypes{Items: types}
276 count := 0
277 for {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700278 if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700279 log.Warnw("registering-with-core-failed", log.Fields{"error": err})
280 if retries == count {
281 return err
282 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700283 count++
284 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700285 time.Sleep(2 * time.Second)
286 } else {
287 break
288 }
289 }
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000290 probe.UpdateStatusFromContext(ctx, "register-with-core", probe.ServiceStatusRunning)
cuilin20187b2a8c32019-03-26 19:52:28 -0700291 log.Info("registered-with-core")
292 return nil
293}
294
295func waitForExit() int {
296 signalChannel := make(chan os.Signal, 1)
297 signal.Notify(signalChannel,
298 syscall.SIGHUP,
299 syscall.SIGINT,
300 syscall.SIGTERM,
301 syscall.SIGQUIT)
302
303 exitChannel := make(chan int)
304
305 go func() {
306 s := <-signalChannel
307 switch s {
308 case syscall.SIGHUP,
309 syscall.SIGINT,
310 syscall.SIGTERM,
311 syscall.SIGQUIT:
312 log.Infow("closing-signal-received", log.Fields{"signal": s})
313 exitChannel <- 0
314 default:
315 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
316 exitChannel <- 1
317 }
318 }()
319
320 code := <-exitChannel
321 return code
322}
323
324func printBanner() {
325 fmt.Println(" ____ ____ _ _______ ")
326 fmt.Println(" / _ \\ / __\\| | |__ __|")
327 fmt.Println(" | | | |_ __ ___ _ __ | | | | | | | ")
328 fmt.Println(" | | | | '_\\ / _\\ '_\\ | | | | | | | ")
329 fmt.Println(" | |__| | |_) | __/ | | || |__| | |____| | ")
330 fmt.Println(" \\____/| .__/\\___|_| |_|\\____/|______|_| ")
331 fmt.Println(" | | ")
332 fmt.Println(" |_| ")
333 fmt.Println(" ")
334}
335
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400336func printVersion() {
337 fmt.Println("VOLTHA OpenOLT Adapter")
338 fmt.Println(version.VersionInfo.String(" "))
339}
340
cuilin20187b2a8c32019-03-26 19:52:28 -0700341func main() {
342 start := time.Now()
343
344 cf := config.NewAdapterFlags()
345 cf.ParseCommandArguments()
346
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700347 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700348
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700349 // Setup default logger - applies for packages that do not have specific logger set
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000350 if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700351 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
352 }
353
354 // Update all loggers (provisionned via init) with a common field
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000355 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700356 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
357 }
358
Scott Baker51290152019-10-24 14:23:20 -0700359 log.SetPackageLogLevel("github.com/opencord/voltha-lib-go/v2/pkg/adapters/common", log.DebugLevel)
cuilin20187b2a8c32019-03-26 19:52:28 -0700360
361 defer log.CleanUp()
362
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400363 // Print version / build information and exit
364 if cf.DisplayVersionOnly {
365 printVersion()
366 return
367 }
368
cuilin20187b2a8c32019-03-26 19:52:28 -0700369 // Print banner if specified
370 if cf.Banner {
371 printBanner()
372 }
373
374 log.Infow("config", log.Fields{"config": *cf})
375
376 ctx, cancel := context.WithCancel(context.Background())
377 defer cancel()
378
379 ad := newAdapter(cf)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000380
381 p := &probe.Probe{}
382 go p.ListenAndServe(fmt.Sprintf("%s:%d", ad.config.ProbeHost, ad.config.ProbePort))
383
384 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
385
386 go ad.start(probeCtx)
cuilin20187b2a8c32019-03-26 19:52:28 -0700387
388 code := waitForExit()
389 log.Infow("received-a-closing-signal", log.Fields{"code": code})
390
391 // Cleanup before leaving
392 ad.stop()
393
394 elapsed := time.Since(start)
Hardik Windlassb9c869b2019-10-10 08:34:32 +0000395 log.Infow("run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700396}