Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 1 | /*
|
| 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 | */
|
| 16 | package main
|
| 17 |
|
| 18 | import (
|
| 19 | "context"
|
| 20 | "errors"
|
| 21 | "fmt"
|
| 22 | "github.com/opencord/voltha-go/adapters"
|
| 23 | com "github.com/opencord/voltha-go/adapters/common"
|
| 24 | ac "github.com/opencord/voltha-go/adapters/openolt/adaptercore"
|
| 25 | "github.com/opencord/voltha-go/adapters/openolt/config"
|
| 26 | "github.com/opencord/voltha-go/common/log"
|
| 27 | "github.com/opencord/voltha-go/db/kvstore"
|
| 28 | "github.com/opencord/voltha-go/kafka"
|
| 29 | ic "github.com/opencord/voltha-go/protos/inter_container"
|
| 30 | "github.com/opencord/voltha-go/protos/voltha"
|
| 31 | "os"
|
| 32 | "os/signal"
|
| 33 | "strconv"
|
| 34 | "syscall"
|
| 35 | "time"
|
| 36 | )
|
| 37 |
|
| 38 | type adapter struct {
|
| 39 | instanceId string
|
| 40 | config *config.AdapterFlags
|
| 41 | iAdapter adapters.IAdapter
|
| 42 | kafkaClient kafka.Client
|
| 43 | kvClient kvstore.Client
|
| 44 | kip *kafka.InterContainerProxy
|
| 45 | coreProxy *com.CoreProxy
|
| 46 | halted bool
|
| 47 | exitChannel chan int
|
| 48 | receiverChannels []<-chan *ic.InterContainerMessage
|
| 49 | }
|
| 50 |
|
| 51 | func init() {
|
| 52 | log.AddPackage(log.JSON, log.DebugLevel, nil)
|
| 53 | }
|
| 54 |
|
| 55 | func newAdapter(cf *config.AdapterFlags) *adapter {
|
| 56 | var a adapter
|
| 57 | a.instanceId = cf.InstanceID
|
| 58 | a.config = cf
|
| 59 | a.halted = false
|
| 60 | a.exitChannel = make(chan int, 1)
|
| 61 | a.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
|
| 62 | return &a
|
| 63 | }
|
| 64 |
|
| 65 | func (a *adapter) start(ctx context.Context) {
|
| 66 | log.Info("Starting Core Adapter components")
|
| 67 | var err error
|
| 68 |
|
| 69 | // Setup KV Client
|
| 70 | log.Debugw("create-kv-client", log.Fields{"kvstore": a.config.KVStoreType})
|
| 71 | if err := a.setKVClient(); err != nil {
|
| 72 | log.Fatal("error-setting-kv-client")
|
| 73 | }
|
| 74 |
|
| 75 | // Setup Kafka Client
|
| 76 | if a.kafkaClient, err = newKafkaClient("sarama", a.config.KafkaAdapterHost, a.config.KafkaAdapterPort); err != nil {
|
| 77 | log.Fatal("Unsupported-common-client")
|
| 78 | }
|
| 79 |
|
| 80 | // Start the common InterContainer Proxy - retries indefinitely
|
| 81 | if a.kip, err = a.startInterContainerProxy(-1); err != nil {
|
| 82 | log.Fatal("error-starting-inter-container-proxy")
|
| 83 | }
|
| 84 |
|
| 85 | // Create the core proxy to handle requests to the Core
|
| 86 | a.coreProxy = com.NewCoreProxy(a.kip, a.config.Topic, a.config.CoreTopic)
|
| 87 |
|
| 88 | // Create the open OLT adapter
|
| 89 | if a.iAdapter, err = a.startOpenOLT(ctx, a.kip, a.coreProxy, a.config.OnuNumber); err != nil {
|
| 90 | log.Fatal("error-starting-inter-container-proxy")
|
| 91 | }
|
| 92 |
|
| 93 | // Register the core request handler
|
| 94 | if err = a.setupRequestHandler(a.instanceId, a.iAdapter); err != nil {
|
| 95 | log.Fatal("error-setting-core-request-handler")
|
| 96 | }
|
| 97 |
|
| 98 | // Register this adapter to the Core - retries indefinitely
|
| 99 | if err = a.registerWithCore(-1); err != nil {
|
| 100 | log.Fatal("error-registering-with-core")
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | func (rw *adapter) stop() {
|
| 105 | // Stop leadership tracking
|
| 106 | rw.halted = true
|
| 107 |
|
| 108 | // send exit signal
|
| 109 | rw.exitChannel <- 0
|
| 110 |
|
| 111 | // Cleanup - applies only if we had a kvClient
|
| 112 | if rw.kvClient != nil {
|
| 113 | // Release all reservations
|
| 114 | if err := rw.kvClient.ReleaseAllReservations(); err != nil {
|
| 115 | log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
|
| 116 | }
|
| 117 | // Close the DB connection
|
| 118 | rw.kvClient.Close()
|
| 119 | }
|
| 120 |
|
| 121 | // TODO: More cleanup
|
| 122 | }
|
| 123 |
|
| 124 | func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
|
| 125 |
|
| 126 | log.Infow("kv-store-type", log.Fields{"store": storeType})
|
| 127 | switch storeType {
|
| 128 | case "consul":
|
| 129 | return kvstore.NewConsulClient(address, timeout)
|
| 130 | case "etcd":
|
| 131 | return kvstore.NewEtcdClient(address, timeout)
|
| 132 | }
|
| 133 | return nil, errors.New("unsupported-kv-store")
|
| 134 | }
|
| 135 |
|
| 136 | func newKafkaClient(clientType string, host string, port int) (kafka.Client, error) {
|
| 137 |
|
| 138 | log.Infow("common-client-type", log.Fields{"client": clientType})
|
| 139 | switch clientType {
|
| 140 | case "sarama":
|
| 141 | return kafka.NewSaramaClient(
|
| 142 | kafka.Host(host),
|
| 143 | kafka.Port(port),
|
| 144 | kafka.ProducerReturnOnErrors(true),
|
| 145 | kafka.ProducerReturnOnSuccess(true),
|
| 146 | kafka.ProducerMaxRetries(6),
|
| 147 | kafka.ProducerRetryBackoff(time.Millisecond*30)), nil
|
| 148 | }
|
| 149 | return nil, errors.New("unsupported-client-type")
|
| 150 | }
|
| 151 |
|
| 152 | func (a *adapter) setKVClient() error {
|
| 153 | addr := a.config.KVStoreHost + ":" + strconv.Itoa(a.config.KVStorePort)
|
| 154 | client, err := newKVClient(a.config.KVStoreType, addr, a.config.KVStoreTimeout)
|
| 155 | if err != nil {
|
| 156 | a.kvClient = nil
|
| 157 | log.Error(err)
|
| 158 | return err
|
| 159 | }
|
| 160 | a.kvClient = client
|
| 161 | return nil
|
| 162 | }
|
| 163 |
|
| 164 | func toString(value interface{}) (string, error) {
|
| 165 | switch t := value.(type) {
|
| 166 | case []byte:
|
| 167 | return string(value.([]byte)), nil
|
| 168 | case string:
|
| 169 | return value.(string), nil
|
| 170 | default:
|
| 171 | return "", fmt.Errorf("unexpected-type-%T", t)
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | func (a *adapter) startInterContainerProxy(retries int) (*kafka.InterContainerProxy, error) {
|
| 176 | log.Infow("starting-intercontainer-messaging-proxy", log.Fields{"host": a.config.KafkaAdapterHost,
|
| 177 | "port": a.config.KafkaAdapterPort, "topic": a.config.Topic})
|
| 178 | var err error
|
| 179 | var kip *kafka.InterContainerProxy
|
| 180 | if kip, err = kafka.NewInterContainerProxy(
|
| 181 | kafka.InterContainerHost(a.config.KafkaAdapterHost),
|
| 182 | kafka.InterContainerPort(a.config.KafkaAdapterPort),
|
| 183 | kafka.MsgClient(a.kafkaClient),
|
| 184 | kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic})); err != nil {
|
| 185 | log.Errorw("fail-to-create-common-proxy", log.Fields{"error": err})
|
| 186 | return nil, err
|
| 187 | }
|
| 188 | count := 0
|
| 189 | for {
|
| 190 | if err = kip.Start(); err != nil {
|
| 191 | log.Warnw("error-starting-messaging-proxy", log.Fields{"error": err})
|
| 192 | if retries == count {
|
| 193 | return nil, err
|
| 194 | }
|
| 195 | count = +1
|
| 196 | // Take a nap before retrying
|
| 197 | time.Sleep(2 * time.Second)
|
| 198 | } else {
|
| 199 | break
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | log.Info("common-messaging-proxy-created")
|
| 204 | return kip, nil
|
| 205 | }
|
| 206 |
|
| 207 | func (a *adapter) startOpenOLT(ctx context.Context, kip *kafka.InterContainerProxy, cp *com.CoreProxy, onuNumber int) (*ac.OpenOLT, error) {
|
| 208 | log.Info("starting-open-olt")
|
| 209 | var err error
|
| 210 | sOLT := ac.NewOpenOLT(ctx, a.kip, cp, onuNumber)
|
| 211 |
|
| 212 | if err = sOLT.Start(ctx); err != nil {
|
| 213 | log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err})
|
| 214 | return nil, err
|
| 215 | }
|
| 216 |
|
| 217 | log.Info("open-olt-started")
|
| 218 | return sOLT, nil
|
| 219 | }
|
| 220 |
|
| 221 | func (a *adapter) setupRequestHandler(coreInstanceId string, iadapter adapters.IAdapter) error {
|
| 222 | log.Info("setting-request-handler")
|
| 223 | requestProxy := com.NewRequestHandlerProxy(coreInstanceId, iadapter, a.coreProxy)
|
| 224 | if err := a.kip.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil {
|
| 225 | log.Errorw("request-handler-setup-failed", log.Fields{"error": err})
|
| 226 | return err
|
| 227 |
|
| 228 | }
|
| 229 | log.Info("request-handler-setup-done")
|
| 230 | return nil
|
| 231 | }
|
| 232 |
|
| 233 | func (a *adapter) registerWithCore(retries int) error {
|
| 234 | log.Info("registering-with-core")
|
| 235 | adapterDescription := &voltha.Adapter{Id: "openolt", Vendor: "simulation Enterprise Inc"}
|
| 236 | types := []*voltha.DeviceType{{Id: "openolt", Adapter: "openolt"}}
|
| 237 | deviceTypes := &voltha.DeviceTypes{Items: types}
|
| 238 | count := 0
|
| 239 | for {
|
| 240 | if err := a.coreProxy.RegisterAdapter(nil, adapterDescription, deviceTypes); err != nil {
|
| 241 | log.Warnw("registering-with-core-failed", log.Fields{"error": err})
|
| 242 | if retries == count {
|
| 243 | return err
|
| 244 | }
|
| 245 | count += 1
|
| 246 | // Take a nap before retrying
|
| 247 | time.Sleep(2 * time.Second)
|
| 248 | } else {
|
| 249 | break
|
| 250 | }
|
| 251 | }
|
| 252 | log.Info("registered-with-core")
|
| 253 | return nil
|
| 254 | }
|
| 255 |
|
| 256 | func waitForExit() int {
|
| 257 | signalChannel := make(chan os.Signal, 1)
|
| 258 | signal.Notify(signalChannel,
|
| 259 | syscall.SIGHUP,
|
| 260 | syscall.SIGINT,
|
| 261 | syscall.SIGTERM,
|
| 262 | syscall.SIGQUIT)
|
| 263 |
|
| 264 | exitChannel := make(chan int)
|
| 265 |
|
| 266 | go func() {
|
| 267 | s := <-signalChannel
|
| 268 | switch s {
|
| 269 | case syscall.SIGHUP,
|
| 270 | syscall.SIGINT,
|
| 271 | syscall.SIGTERM,
|
| 272 | syscall.SIGQUIT:
|
| 273 | log.Infow("closing-signal-received", log.Fields{"signal": s})
|
| 274 | exitChannel <- 0
|
| 275 | default:
|
| 276 | log.Infow("unexpected-signal-received", log.Fields{"signal": s})
|
| 277 | exitChannel <- 1
|
| 278 | }
|
| 279 | }()
|
| 280 |
|
| 281 | code := <-exitChannel
|
| 282 | return code
|
| 283 | }
|
| 284 |
|
| 285 | func printBanner() {
|
| 286 | fmt.Println(" ____ ____ _ _______ ")
|
| 287 | fmt.Println(" / _ \\ / __\\| | |__ __|")
|
| 288 | fmt.Println(" | | | |_ __ ___ _ __ | | | | | | | ")
|
| 289 | fmt.Println(" | | | | '_\\ / _\\ '_\\ | | | | | | | ")
|
| 290 | fmt.Println(" | |__| | |_) | __/ | | || |__| | |____| | ")
|
| 291 | fmt.Println(" \\____/| .__/\\___|_| |_|\\____/|______|_| ")
|
| 292 | fmt.Println(" | | ")
|
| 293 | fmt.Println(" |_| ")
|
| 294 | fmt.Println(" ")
|
| 295 | }
|
| 296 |
|
| 297 | func main() {
|
| 298 | start := time.Now()
|
| 299 |
|
| 300 | cf := config.NewAdapterFlags()
|
| 301 | cf.ParseCommandArguments()
|
| 302 |
|
| 303 | //// Setup logging
|
| 304 |
|
| 305 | //Setup default logger - applies for packages that do not have specific logger set
|
| 306 | if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
|
| 307 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
|
| 308 | }
|
| 309 |
|
| 310 | // Update all loggers (provisionned via init) with a common field
|
| 311 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
|
| 312 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
|
| 313 | }
|
| 314 |
|
| 315 | log.SetPackageLogLevel("github.com/opencord/voltha-go/adapters/common", log.DebugLevel)
|
| 316 |
|
| 317 | defer log.CleanUp()
|
| 318 |
|
| 319 | // Print banner if specified
|
| 320 | if cf.Banner {
|
| 321 | printBanner()
|
| 322 | }
|
| 323 |
|
| 324 | log.Infow("config", log.Fields{"config": *cf})
|
| 325 |
|
| 326 | ctx, cancel := context.WithCancel(context.Background())
|
| 327 | defer cancel()
|
| 328 |
|
| 329 | ad := newAdapter(cf)
|
| 330 | go ad.start(ctx)
|
| 331 |
|
| 332 | code := waitForExit()
|
| 333 | log.Infow("received-a-closing-signal", log.Fields{"code": code})
|
| 334 |
|
| 335 | // Cleanup before leaving
|
| 336 | ad.stop()
|
| 337 |
|
| 338 | elapsed := time.Since(start)
|
| 339 | log.Infow("run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second})
|
| 340 | }
|