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