Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-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 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | "os" |
| 23 | "os/signal" |
| 24 | "syscall" |
| 25 | "time" |
| 26 | |
Elia Battiston | aa7a048 | 2022-08-17 12:24:02 +0000 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v7/pkg/db" |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 29 | "github.com/opencord/voltha-lib-go/v7/pkg/probe" |
| 30 | "github.com/opencord/voltha-lib-go/v7/pkg/version" |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 31 | "github.com/opencord/voltha-northbound-bbf-adapter/internal/clients" |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 32 | "github.com/opencord/voltha-northbound-bbf-adapter/internal/config" |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 33 | "github.com/opencord/voltha-northbound-bbf-adapter/internal/core" |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 34 | "github.com/opencord/voltha-northbound-bbf-adapter/internal/sysrepo" |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 35 | ) |
| 36 | |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 37 | //String for readiness probe services |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 38 | const ( |
| 39 | bbfAdapterService = "bbf-adapter-service" |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 40 | sysrepoService = "sysrepo" |
Elia Battiston | aa7a048 | 2022-08-17 12:24:02 +0000 | [diff] [blame] | 41 | |
| 42 | kvStoreBbfAdapterSuffix = "/bbf-adapter" |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 43 | ) |
| 44 | |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 45 | type bbfAdapter struct { |
| 46 | conf *config.BBFAdapterConfig |
| 47 | volthaNbiClient *clients.VolthaNbiClient |
Elia Battiston | a133364 | 2022-07-27 12:17:24 +0000 | [diff] [blame] | 48 | onosClient *clients.OnosClient |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 49 | sysrepoPlugin *sysrepo.SysrepoPlugin |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 50 | kafkaConsumer *clients.KafkaConsumer |
Elia Battiston | aa7a048 | 2022-08-17 12:24:02 +0000 | [diff] [blame] | 51 | kvStoreBackend *db.Backend |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | func newBbfAdapter(conf *config.BBFAdapterConfig) *bbfAdapter { |
| 55 | return &bbfAdapter{ |
| 56 | conf: conf, |
| 57 | } |
| 58 | } |
| 59 | |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 60 | func (a *bbfAdapter) start(ctx context.Context) { |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 61 | var err error |
| 62 | |
| 63 | //Connect to the voltha northbound api |
| 64 | a.volthaNbiClient = clients.NewVolthaNbiClient(a.conf.VolthaNbiEndpoint) |
| 65 | if err = a.volthaNbiClient.Connect(ctx, a.conf.TlsEnabled, a.conf.TlsVerify); err != nil { |
| 66 | logger.Fatalw(ctx, "failed-to-open-voltha-nbi-grpc-connection", log.Fields{"err": err}) |
| 67 | } else { |
| 68 | probe.UpdateStatusFromContext(ctx, a.conf.VolthaNbiEndpoint, probe.ServiceStatusRunning) |
| 69 | } |
| 70 | |
| 71 | //Check if the REST APIs of the olt app are reachable |
Elia Battiston | a133364 | 2022-07-27 12:17:24 +0000 | [diff] [blame] | 72 | a.onosClient = clients.NewOnosClient(a.conf.OnosRestEndpoint, a.conf.OnosUser, a.conf.OnosPassword) |
| 73 | if err := a.onosClient.CheckConnection(ctx); err != nil { |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 74 | logger.Fatalw(ctx, "failed-to-connect-to-onos-olt-app-api", log.Fields{"err": err}) |
| 75 | } else { |
| 76 | probe.UpdateStatusFromContext(ctx, a.conf.OnosRestEndpoint, probe.ServiceStatusRunning) |
| 77 | } |
| 78 | |
Elia Battiston | aa7a048 | 2022-08-17 12:24:02 +0000 | [diff] [blame] | 79 | //Connect to the KVStore |
| 80 | a.kvStoreBackend = db.NewBackend(ctx, a.conf.KvStoreType, a.conf.KvStoreAddress, a.conf.KvStoreTimeout, a.conf.KvStorePrefix+kvStoreBbfAdapterSuffix) |
| 81 | if err != nil { |
| 82 | logger.Fatalw(ctx, "failed-to-start-kv-store-client", log.Fields{"err": err}) |
| 83 | } |
| 84 | if a.kvStoreBackend.PerformLivenessCheck(ctx) { |
| 85 | probe.UpdateStatusFromContext(ctx, a.conf.KvStoreAddress, probe.ServiceStatusRunning) |
| 86 | } else { |
| 87 | logger.Fatalw(ctx, "failed-to-connect-to-kv-store-client", log.Fields{"err": err}) |
| 88 | } |
| 89 | |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 90 | //Create the global adapter that will be used by callbacks |
Elia Battiston | aa7a048 | 2022-08-17 12:24:02 +0000 | [diff] [blame] | 91 | core.AdapterInstance = core.NewVolthaYangAdapter(a.volthaNbiClient, a.onosClient, a.kvStoreBackend) |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 92 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 93 | //Load sysrepo plugin |
Elia Battiston | 589addb | 2022-04-04 16:40:01 +0200 | [diff] [blame] | 94 | a.sysrepoPlugin, err = sysrepo.StartNewPlugin(ctx, a.conf.SchemaMountFilePath) |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 95 | if err != nil { |
| 96 | logger.Fatalw(ctx, "failed-to-start-sysrepo-plugin", log.Fields{"err": err}) |
| 97 | } else { |
| 98 | probe.UpdateStatusFromContext(ctx, sysrepoService, probe.ServiceStatusRunning) |
| 99 | } |
| 100 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 101 | //Set up the Kafka consumer |
| 102 | a.kafkaConsumer = clients.NewKafkaConsumer(a.conf.KafkaClusterAddress) |
| 103 | if err := a.kafkaConsumer.Start(ctx, a.sysrepoPlugin.ManageVolthaEvent); err != nil { |
| 104 | logger.Fatalw(ctx, "failed-to-start-kafka-consumer", log.Fields{"err": err}) |
| 105 | } else { |
| 106 | probe.UpdateStatusFromContext(ctx, a.conf.KafkaClusterAddress, probe.ServiceStatusRunning) |
| 107 | } |
| 108 | |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 109 | //Set the service as running, making the adapter finally ready |
| 110 | probe.UpdateStatusFromContext(ctx, bbfAdapterService, probe.ServiceStatusRunning) |
| 111 | logger.Info(ctx, "bbf-adapter-ready") |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | //Close all connections of the adapter |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 115 | func (a *bbfAdapter) cleanup(ctx context.Context) { |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 116 | core.AdapterInstance = nil |
| 117 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 118 | if err := a.kafkaConsumer.Stop(); err != nil { |
| 119 | logger.Errorw(ctx, "failed-to-stop-kafka-consumer", log.Fields{"err": err}) |
| 120 | } |
| 121 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 122 | a.volthaNbiClient.Close(ctx) |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 123 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 124 | err := a.sysrepoPlugin.Stop(ctx) |
| 125 | if err != nil { |
| 126 | logger.Errorw(ctx, "failed-to-stop-sysrepo-plugin", log.Fields{"err": err}) |
| 127 | } |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 128 | |
| 129 | probe.UpdateStatusFromContext(ctx, bbfAdapterService, probe.ServiceStatusStopped) |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 132 | func printBanner() { |
| 133 | fmt.Println(" ____ ____ ______ _ _ ") |
| 134 | fmt.Println(" | _ \\| _ \\| ____| /\\ | | | | ") |
| 135 | fmt.Println(" | |_) | |_) | |__ / \\ __| | __ _ _ __ | |_ ___ _ __ ") |
| 136 | fmt.Println(" | _ <| _ <| __| / /\\ \\ / _` |/ _` | '_ \\| __/ _ \\ '__|") |
| 137 | fmt.Println(" | |_) | |_) | | / ____ \\ (_| | (_| | |_) | || __/ | ") |
| 138 | fmt.Println(" |____/|____/|_| /_/ \\_\\__,_|\\__,_| .__/ \\__\\___|_| ") |
| 139 | fmt.Println(" | | ") |
| 140 | fmt.Println(" |_| ") |
| 141 | } |
| 142 | |
| 143 | func printVersion() { |
| 144 | fmt.Println("VOLTHA Northbound BBF Adapter") |
| 145 | fmt.Println(version.VersionInfo.String(" ")) |
| 146 | } |
| 147 | |
| 148 | func waitForExit(ctx context.Context) int { |
| 149 | signalChannel := make(chan os.Signal, 1) |
| 150 | signal.Notify(signalChannel, |
| 151 | syscall.SIGHUP, |
| 152 | syscall.SIGINT, |
| 153 | syscall.SIGTERM, |
| 154 | syscall.SIGQUIT) |
| 155 | |
| 156 | exitChannel := make(chan int) |
| 157 | |
| 158 | go func() { |
| 159 | s := <-signalChannel |
| 160 | switch s { |
| 161 | case syscall.SIGHUP, |
| 162 | syscall.SIGINT, |
| 163 | syscall.SIGTERM, |
| 164 | syscall.SIGQUIT: |
| 165 | logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s}) |
| 166 | exitChannel <- 0 |
| 167 | default: |
| 168 | logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s}) |
| 169 | exitChannel <- 1 |
| 170 | } |
| 171 | }() |
| 172 | |
| 173 | code := <-exitChannel |
| 174 | return code |
| 175 | } |
| 176 | |
| 177 | func main() { |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 178 | ctx, cancelCtx := context.WithCancel(context.Background()) |
| 179 | |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 180 | start := time.Now() |
| 181 | |
| 182 | conf := config.LoadConfig(ctx) |
| 183 | |
| 184 | //Logging |
| 185 | logLevel, err := log.StringToLogLevel(conf.LogLevel) |
| 186 | if err != nil { |
| 187 | logger.Fatalf(ctx, "Cannot setup logging, %s", err) |
| 188 | } |
| 189 | |
| 190 | // Setup default logger - applies for packages that do not have specific logger set |
| 191 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{}); err != nil { |
| 192 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
| 193 | } |
| 194 | |
| 195 | // Update all loggers (provisionned via init) with a common field |
| 196 | if err := log.UpdateAllLoggers(log.Fields{}); err != nil { |
| 197 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
| 198 | } |
| 199 | |
| 200 | log.SetAllLogLevel(logLevel) |
| 201 | |
| 202 | defer func() { |
| 203 | err := log.CleanUp() |
| 204 | if err != nil { |
| 205 | logger.Errorw(context.Background(), "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
| 206 | } |
| 207 | }() |
| 208 | |
| 209 | // Print version and exit |
| 210 | if conf.PrintVersion { |
| 211 | printVersion() |
| 212 | return |
| 213 | } |
| 214 | |
| 215 | // Print banner if specified |
| 216 | if conf.PrintBanner { |
| 217 | printBanner() |
| 218 | } |
| 219 | |
| 220 | logger.Infow(ctx, "config", log.Fields{"config": *conf}) |
| 221 | |
| 222 | p := &probe.Probe{} |
| 223 | go p.ListenAndServe(ctx, conf.ProbeAddress) |
| 224 | |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 225 | //Register all services that will need to be initialized before considering the adapter ready |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 226 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 227 | p.RegisterService( |
| 228 | ctx, |
| 229 | bbfAdapterService, |
| 230 | conf.VolthaNbiEndpoint, |
| 231 | conf.OnosRestEndpoint, |
Elia Battiston | aa7a048 | 2022-08-17 12:24:02 +0000 | [diff] [blame] | 232 | conf.KvStoreAddress, |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame] | 233 | conf.KafkaClusterAddress, |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 234 | sysrepoService, |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 235 | ) |
| 236 | |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 237 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(conf.TraceEnabled, conf.TraceAgentAddress, conf.LogCorrelationEnabled) |
| 238 | if err != nil { |
| 239 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 240 | } else { |
| 241 | defer log.TerminateTracing(closer) |
| 242 | } |
| 243 | |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 244 | adapter := newBbfAdapter(conf) |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 245 | |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 246 | //Run the adapter |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 247 | adapter.start(probeCtx) |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 248 | defer adapter.cleanup(probeCtx) |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 249 | |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 250 | //Wait a signal to stop execution |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 251 | code := waitForExit(ctx) |
| 252 | logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code}) |
| 253 | |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 254 | //Stop everything that waits for the context to be done |
| 255 | cancelCtx() |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 256 | |
Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 257 | elapsed := time.Since(start) |
| 258 | logger.Infow(ctx, "run-time", log.Fields{"time": elapsed.Seconds()}) |
| 259 | } |