blob: 1a9b411f152fcf84dce6fb1f81074ee2e47946fd [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),
155 kafka.ProducerRetryBackoff(time.Millisecond*30)), nil
156 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700157
cuilin20187b2a8c32019-03-26 19:52:28 -0700158 return nil, errors.New("unsupported-client-type")
159}
160
161func (a *adapter) setKVClient() error {
162 addr := a.config.KVStoreHost + ":" + strconv.Itoa(a.config.KVStorePort)
163 client, err := newKVClient(a.config.KVStoreType, addr, a.config.KVStoreTimeout)
164 if err != nil {
165 a.kvClient = nil
166 log.Error(err)
167 return err
168 }
169 a.kvClient = client
170 return nil
171}
172
cuilin20187b2a8c32019-03-26 19:52:28 -0700173func (a *adapter) startInterContainerProxy(retries int) (*kafka.InterContainerProxy, error) {
174 log.Infow("starting-intercontainer-messaging-proxy", log.Fields{"host": a.config.KafkaAdapterHost,
175 "port": a.config.KafkaAdapterPort, "topic": a.config.Topic})
176 var err error
177 var kip *kafka.InterContainerProxy
178 if kip, err = kafka.NewInterContainerProxy(
179 kafka.InterContainerHost(a.config.KafkaAdapterHost),
180 kafka.InterContainerPort(a.config.KafkaAdapterPort),
181 kafka.MsgClient(a.kafkaClient),
182 kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic})); err != nil {
183 log.Errorw("fail-to-create-common-proxy", log.Fields{"error": err})
184 return nil, err
185 }
186 count := 0
187 for {
188 if err = kip.Start(); err != nil {
189 log.Warnw("error-starting-messaging-proxy", log.Fields{"error": err})
190 if retries == count {
191 return nil, err
192 }
193 count = +1
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700194 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700195 time.Sleep(2 * time.Second)
196 } else {
197 break
198 }
199 }
200
201 log.Info("common-messaging-proxy-created")
202 return kip, nil
203}
204
manikkaraj kbf256be2019-03-25 00:13:48 +0530205func (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 -0700206 log.Info("starting-open-olt")
207 var err error
manikkaraj kbf256be2019-03-25 00:13:48 +0530208 sOLT := ac.NewOpenOLT(ctx, a.kip, cp, ap, onuNumber, kvStoreHost, kvStorePort, KVStoreType)
cuilin20187b2a8c32019-03-26 19:52:28 -0700209
210 if err = sOLT.Start(ctx); err != nil {
211 log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err})
212 return nil, err
213 }
214
215 log.Info("open-olt-started")
216 return sOLT, nil
217}
218
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700219func (a *adapter) setupRequestHandler(coreInstanceID string, iadapter adapters.IAdapter) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700220 log.Info("setting-request-handler")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700221 requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy)
cuilin20187b2a8c32019-03-26 19:52:28 -0700222 if err := a.kip.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
223 log.Errorw("request-handler-setup-failed", log.Fields{"error": err})
224 return err
225
226 }
227 log.Info("request-handler-setup-done")
228 return nil
229}
230
231func (a *adapter) registerWithCore(retries int) error {
232 log.Info("registering-with-core")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400233 adapterDescription := &voltha.Adapter{Id: "openolt", // Unique name for the device type
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400234 Vendor: "VOLTHA OpenOLT",
235 Version: version.VersionInfo.Version}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 types := []*voltha.DeviceType{{Id: "openolt",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700237 Adapter: "openolt", // Name of the adapter that handles device type
Girish Gowdru0c588b22019-04-23 23:24:56 -0400238 AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling
239 AcceptsAddRemoveFlowUpdates: true}}
cuilin20187b2a8c32019-03-26 19:52:28 -0700240 deviceTypes := &voltha.DeviceTypes{Items: types}
241 count := 0
242 for {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700243 if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700244 log.Warnw("registering-with-core-failed", log.Fields{"error": err})
245 if retries == count {
246 return err
247 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700248 count++
249 // Take a nap before retrying
cuilin20187b2a8c32019-03-26 19:52:28 -0700250 time.Sleep(2 * time.Second)
251 } else {
252 break
253 }
254 }
255 log.Info("registered-with-core")
256 return nil
257}
258
259func waitForExit() int {
260 signalChannel := make(chan os.Signal, 1)
261 signal.Notify(signalChannel,
262 syscall.SIGHUP,
263 syscall.SIGINT,
264 syscall.SIGTERM,
265 syscall.SIGQUIT)
266
267 exitChannel := make(chan int)
268
269 go func() {
270 s := <-signalChannel
271 switch s {
272 case syscall.SIGHUP,
273 syscall.SIGINT,
274 syscall.SIGTERM,
275 syscall.SIGQUIT:
276 log.Infow("closing-signal-received", log.Fields{"signal": s})
277 exitChannel <- 0
278 default:
279 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
280 exitChannel <- 1
281 }
282 }()
283
284 code := <-exitChannel
285 return code
286}
287
288func printBanner() {
289 fmt.Println(" ____ ____ _ _______ ")
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}
299
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400300func printVersion() {
301 fmt.Println("VOLTHA OpenOLT Adapter")
302 fmt.Println(version.VersionInfo.String(" "))
303}
304
cuilin20187b2a8c32019-03-26 19:52:28 -0700305func main() {
306 start := time.Now()
307
308 cf := config.NewAdapterFlags()
309 cf.ParseCommandArguments()
310
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700311 // Setup logging
cuilin20187b2a8c32019-03-26 19:52:28 -0700312
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700313 // Setup default logger - applies for packages that do not have specific logger set
314 if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceID": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700315 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
316 }
317
318 // Update all loggers (provisionned via init) with a common field
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700319 if err := log.UpdateAllLoggers(log.Fields{"instanceID": cf.InstanceID}); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700320 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
321 }
322
323 log.SetPackageLogLevel("github.com/opencord/voltha-go/adapters/common", log.DebugLevel)
324
325 defer log.CleanUp()
326
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400327 // Print version / build information and exit
328 if cf.DisplayVersionOnly {
329 printVersion()
330 return
331 }
332
cuilin20187b2a8c32019-03-26 19:52:28 -0700333 // Print banner if specified
334 if cf.Banner {
335 printBanner()
336 }
337
338 log.Infow("config", log.Fields{"config": *cf})
339
340 ctx, cancel := context.WithCancel(context.Background())
341 defer cancel()
342
343 ad := newAdapter(cf)
344 go ad.start(ctx)
345
346 code := waitForExit()
347 log.Infow("received-a-closing-signal", log.Fields{"code": code})
348
349 // Cleanup before leaving
350 ad.stop()
351
352 elapsed := time.Since(start)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700353 log.Infow("run-time", log.Fields{"instanceID": ad.config.InstanceID, "time": elapsed / time.Second})
cuilin20187b2a8c32019-03-26 19:52:28 -0700354}