blob: dbc2756bf8c71afb6e73fb991c9d4c6aa8d24318 [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"
24 "github.com/opencord/voltha-go/adapters"
25 com "github.com/opencord/voltha-go/adapters/common"
cuilin20187b2a8c32019-03-26 19:52:28 -070026 "github.com/opencord/voltha-go/common/log"
Matt Jeanneretf880eb62019-07-16 20:08:03 -040027 "github.com/opencord/voltha-go/common/version"
cuilin20187b2a8c32019-03-26 19:52:28 -070028 "github.com/opencord/voltha-go/db/kvstore"
29 "github.com/opencord/voltha-go/kafka"
Girish Gowdru0c588b22019-04-23 23:24:56 -040030 ac "github.com/opencord/voltha-openolt-adapter/adaptercore"
31 "github.com/opencord/voltha-openolt-adapter/config"
manikkaraj kbf256be2019-03-25 00:13:48 +053032 ic "github.com/opencord/voltha-protos/go/inter_container"
33 "github.com/opencord/voltha-protos/go/voltha"
cuilin20187b2a8c32019-03-26 19:52:28 -070034 "os"
35 "os/signal"
36 "strconv"
37 "syscall"
38 "time"
39)
40
41type adapter struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070042 instanceID string
cuilin20187b2a8c32019-03-26 19:52:28 -070043 config *config.AdapterFlags
44 iAdapter adapters.IAdapter
45 kafkaClient kafka.Client
46 kvClient kvstore.Client
47 kip *kafka.InterContainerProxy
48 coreProxy *com.CoreProxy
49 adapterProxy *com.AdapterProxy
50 halted bool
51 exitChannel chan int
52 receiverChannels []<-chan *ic.InterContainerMessage
53}
54
55func init() {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070056 _, _ = log.AddPackage(log.JSON, log.DebugLevel, nil)
cuilin20187b2a8c32019-03-26 19:52:28 -070057}
58
59func newAdapter(cf *config.AdapterFlags) *adapter {
60 var a adapter
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070061 a.instanceID = cf.InstanceID
cuilin20187b2a8c32019-03-26 19:52:28 -070062 a.config = cf
63 a.halted = false
64 a.exitChannel = make(chan int, 1)
65 a.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
66 return &a
67}
68
69func (a *adapter) start(ctx context.Context) {
70 log.Info("Starting Core Adapter components")
71 var err error
72
73 // Setup KV Client
74 log.Debugw("create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070075 if err = a.setKVClient(); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -070076 log.Fatal("error-setting-kv-client")
77 }
78
79 // Setup Kafka Client
80 if a.kafkaClient, err = newKafkaClient("sarama", a.config.KafkaAdapterHost, a.config.KafkaAdapterPort); err != nil {
81 log.Fatal("Unsupported-common-client")
82 }
83
84 // Start the common InterContainer Proxy - retries indefinitely
85 if a.kip, err = a.startInterContainerProxy(-1); err != nil {
86 log.Fatal("error-starting-inter-container-proxy")
87 }
88
89 // Create the core proxy to handle requests to the Core
90 a.coreProxy = com.NewCoreProxy(a.kip, a.config.Topic, a.config.CoreTopic)
91
92 // Create the adaptor proxy to handle request between olt and onu
93 a.adapterProxy = com.NewAdapterProxy(a.kip, "brcm_openomci_onu", a.config.CoreTopic)
94
95 // Create the open OLT adapter
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070096 if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.adapterProxy, a.config.OnuNumber,
97 a.config.KVStoreHost, a.config.KVStorePort, a.config.KVStoreType); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -070098 log.Fatal("error-starting-inter-container-proxy")
99 }
100
101 // Register the core request handler
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700102 if err = a.setupRequestHandler(a.instanceID, a.iAdapter); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700103 log.Fatal("error-setting-core-request-handler")
104 }
105
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700106 // Register this adapter to the Core - retries indefinitely
cuilin20187b2a8c32019-03-26 19:52:28 -0700107 if err = a.registerWithCore(-1); err != nil {
108 log.Fatal("error-registering-with-core")
109 }
110}
111
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700112func (a *adapter) stop() {
cuilin20187b2a8c32019-03-26 19:52:28 -0700113 // Stop leadership tracking
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700114 a.halted = true
cuilin20187b2a8c32019-03-26 19:52:28 -0700115
116 // send exit signal
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700117 a.exitChannel <- 0
cuilin20187b2a8c32019-03-26 19:52:28 -0700118
119 // Cleanup - applies only if we had a kvClient
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700120 if a.kvClient != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700121 // Release all reservations
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700122 if err := a.kvClient.ReleaseAllReservations(); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700123 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
124 }
125 // Close the DB connection
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700126 a.kvClient.Close()
cuilin20187b2a8c32019-03-26 19:52:28 -0700127 }
128
129 // TODO: More cleanup
130}
131
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700132func newKVClient(storeType, address string, timeout int) (kvstore.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700133
134 log.Infow("kv-store-type", log.Fields{"store": storeType})
135 switch storeType {
136 case "consul":
137 return kvstore.NewConsulClient(address, timeout)
138 case "etcd":
139 return kvstore.NewEtcdClient(address, timeout)
140 }
141 return nil, errors.New("unsupported-kv-store")
142}
143
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700144func newKafkaClient(clientType, host string, port int) (kafka.Client, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700145
146 log.Infow("common-client-type", log.Fields{"client": clientType})
147 switch clientType {
148 case "sarama":
149 return kafka.NewSaramaClient(
150 kafka.Host(host),
151 kafka.Port(port),
152 kafka.ProducerReturnOnErrors(true),
153 kafka.ProducerReturnOnSuccess(true),
154 kafka.ProducerMaxRetries(6),
Abhilash S.L3b494632019-07-16 15:51:09 +0530155 kafka.ProducerRetryBackoff(time.Millisecond*30),
156 kafka.MetadatMaxRetries(15)), nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700157 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700158
cuilin20187b2a8c32019-03-26 19:52:28 -0700159 return nil, errors.New("unsupported-client-type")
160}
161
162func (a *adapter) setKVClient() error {
163 addr := a.config.KVStoreHost + ":" + strconv.Itoa(a.config.KVStorePort)
164 client, err := newKVClient(a.config.KVStoreType, addr, a.config.KVStoreTimeout)
165 if err != nil {
166 a.kvClient = nil
167 log.Error(err)
168 return err
169 }
170 a.kvClient = client
171 return nil
172}
173
cuilin20187b2a8c32019-03-26 19:52:28 -0700174func (a *adapter) startInterContainerProxy(retries int) (*kafka.InterContainerProxy, error) {
175 log.Infow("starting-intercontainer-messaging-proxy", log.Fields{"host": a.config.KafkaAdapterHost,
176 "port": a.config.KafkaAdapterPort, "topic": a.config.Topic})
177 var err error
178 var kip *kafka.InterContainerProxy
179 if kip, err = kafka.NewInterContainerProxy(
180 kafka.InterContainerHost(a.config.KafkaAdapterHost),
181 kafka.InterContainerPort(a.config.KafkaAdapterPort),
182 kafka.MsgClient(a.kafkaClient),
183 kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic})); err != nil {
184 log.Errorw("fail-to-create-common-proxy", log.Fields{"error": err})
185 return nil, err
186 }
187 count := 0
188 for {
189 if err = kip.Start(); err != nil {
190 log.Warnw("error-starting-messaging-proxy", log.Fields{"error": err})
191 if retries == count {
192 return nil, err
193 }
194 count = +1
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700196 time.Sleep(2 * time.Second)
197 } else {
198 break
199 }
200 }
201
202 log.Info("common-messaging-proxy-created")
203 return kip, nil
204}
205
manikkaraj kbf256be2019-03-25 00:13:48 +0530206func (a *adapter) startOpenOLT(ctx context.Context, kip *kafka.InterContainerProxy, cp *com.CoreProxy, ap *com.AdapterProxy, onuNumber int, kvStoreHost string, kvStorePort int, KVStoreType string) (*ac.OpenOLT, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700207 log.Info("starting-open-olt")
208 var err error
manikkaraj kbf256be2019-03-25 00:13:48 +0530209 sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, onuNumber, kvStoreHost, kvStorePort, KVStoreType)
cuilin20187b2a8c32019-03-26 19:52:28 -0700210
211 if err = sOLT.Start(ctx); err != nil {
212 log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err})
213 return nil, err
214 }
215
216 log.Info("open-olt-started")
217 return sOLT, nil
218}
219
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700220func (a *adapter) setupRequestHandler(coreInstanceID string, iadapter adapters.IAdapter) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700221 log.Info("setting-request-handler")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700222 requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy)
cuilin20187b2a8c32019-03-26 19:52:28 -0700223 if err := a.kip.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
224 log.Errorw("request-handler-setup-failed", log.Fields{"error": err})
225 return err
226
227 }
228 log.Info("request-handler-setup-done")
229 return nil
230}
231
232func (a *adapter) registerWithCore(retries int) error {
233 log.Info("registering-with-core")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400234 adapterDescription := &voltha.Adapter{Id: "openolt", // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400235 Vendor: "VOLTHA OpenOLT",
236 Version: version.VersionInfo.Version}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 types := []*voltha.DeviceType{{Id: "openolt",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700238 Adapter: "openolt", // Name of the adapter that handles device type
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
240 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700241 deviceTypes := &voltha.DeviceTypes{Items: types}
242 count := 0
243 for {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700244 if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700245 log.Warnw("registering-with-core-failed", log.Fields{"error": err})
246 if retries == count {
247 return err
248 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700249 count++
250 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700251 time.Sleep(2 * time.Second)
252 } else {
253 break
254 }
255 }
256 log.Info("registered-with-core")
257 return nil
258}
259
260func waitForExit() int {
261 signalChannel := make(chan os.Signal, 1)
262 signal.Notify(signalChannel,
263 syscall.SIGHUP,
264 syscall.SIGINT,
265 syscall.SIGTERM,
266 syscall.SIGQUIT)
267
268 exitChannel := make(chan int)
269
270 go func() {
271 s := <-signalChannel
272 switch s {
273 case syscall.SIGHUP,
274 syscall.SIGINT,
275 syscall.SIGTERM,
276 syscall.SIGQUIT:
277 log.Infow("closing-signal-received", log.Fields{"signal": s})
278 exitChannel <- 0
279 default:
280 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
281 exitChannel <- 1
282 }
283 }()
284
285 code := <-exitChannel
286 return code
287}
288
289func printBanner() {
290 fmt.Println(" ____ ____ _ _______ ")
291 fmt.Println(" / _ \\ / __\\| | |__ __|")
292 fmt.Println(" | | | |_ __ ___ _ __ | | | | | | | ")
293 fmt.Println(" | | | | '_\\ / _\\ '_\\ | | | | | | | ")
294 fmt.Println(" | |__| | |_) | __/ | | || |__| | |____| | ")
295 fmt.Println(" \\____/| .__/\\___|_| |_|\\____/|______|_| ")
296 fmt.Println(" | | ")
297 fmt.Println(" |_| ")
298 fmt.Println(" ")
299}
300
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400301func printVersion() {
302 fmt.Println("VOLTHA OpenOLT Adapter")
303 fmt.Println(version.VersionInfo.String(" "))
304}
305
cuilin20187b2a8c32019-03-26 19:52:28 -0700306func main() {
307 start := time.Now()
308
309 cf := config.NewAdapterFlags()
310 cf.ParseCommandArguments()
311
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700312 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700313
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700314 // Setup default logger - applies for packages that do not have specific logger set
315 if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceID": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700316 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
317 }
318
319 // Update all loggers (provisionned via init) with a common field
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700320 if err := log.UpdateAllLoggers(log.Fields{"instanceID": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700321 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
322 }
323
324 log.SetPackageLogLevel("github.com/opencord/voltha-go/adapters/common", log.DebugLevel)
325
326 defer log.CleanUp()
327
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400328 // Print version / build information and exit
329 if cf.DisplayVersionOnly {
330 printVersion()
331 return
332 }
333
cuilin20187b2a8c32019-03-26 19:52:28 -0700334 // Print banner if specified
335 if cf.Banner {
336 printBanner()
337 }
338
339 log.Infow("config", log.Fields{"config": *cf})
340
341 ctx, cancel := context.WithCancel(context.Background())
342 defer cancel()
343
344 ad := newAdapter(cf)
345 go ad.start(ctx)
346
347 code := waitForExit()
348 log.Infow("received-a-closing-signal", log.Fields{"code": code})
349
350 // Cleanup before leaving
351 ad.stop()
352
353 elapsed := time.Since(start)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700354 log.Infow("run-time", log.Fields{"instanceID": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700355}