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" |
| 30 | "github.com/opencord/voltha-northbound-bbf-adapter/internal/config" |
| 31 | ) |
| 32 | |
| 33 | const ( |
| 34 | bbfAdapterService = "bbf-adapter-service" |
| 35 | ) |
| 36 | |
| 37 | func printBanner() { |
| 38 | fmt.Println(" ____ ____ ______ _ _ ") |
| 39 | fmt.Println(" | _ \\| _ \\| ____| /\\ | | | | ") |
| 40 | fmt.Println(" | |_) | |_) | |__ / \\ __| | __ _ _ __ | |_ ___ _ __ ") |
| 41 | fmt.Println(" | _ <| _ <| __| / /\\ \\ / _` |/ _` | '_ \\| __/ _ \\ '__|") |
| 42 | fmt.Println(" | |_) | |_) | | / ____ \\ (_| | (_| | |_) | || __/ | ") |
| 43 | fmt.Println(" |____/|____/|_| /_/ \\_\\__,_|\\__,_| .__/ \\__\\___|_| ") |
| 44 | fmt.Println(" | | ") |
| 45 | fmt.Println(" |_| ") |
| 46 | } |
| 47 | |
| 48 | func printVersion() { |
| 49 | fmt.Println("VOLTHA Northbound BBF Adapter") |
| 50 | fmt.Println(version.VersionInfo.String(" ")) |
| 51 | } |
| 52 | |
| 53 | func waitForExit(ctx context.Context) int { |
| 54 | signalChannel := make(chan os.Signal, 1) |
| 55 | signal.Notify(signalChannel, |
| 56 | syscall.SIGHUP, |
| 57 | syscall.SIGINT, |
| 58 | syscall.SIGTERM, |
| 59 | syscall.SIGQUIT) |
| 60 | |
| 61 | exitChannel := make(chan int) |
| 62 | |
| 63 | go func() { |
| 64 | s := <-signalChannel |
| 65 | switch s { |
| 66 | case syscall.SIGHUP, |
| 67 | syscall.SIGINT, |
| 68 | syscall.SIGTERM, |
| 69 | syscall.SIGQUIT: |
| 70 | logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s}) |
| 71 | exitChannel <- 0 |
| 72 | default: |
| 73 | logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s}) |
| 74 | exitChannel <- 1 |
| 75 | } |
| 76 | }() |
| 77 | |
| 78 | code := <-exitChannel |
| 79 | return code |
| 80 | } |
| 81 | |
| 82 | func main() { |
| 83 | ctx := context.Background() |
| 84 | start := time.Now() |
| 85 | |
| 86 | conf := config.LoadConfig(ctx) |
| 87 | |
| 88 | //Logging |
| 89 | logLevel, err := log.StringToLogLevel(conf.LogLevel) |
| 90 | if err != nil { |
| 91 | logger.Fatalf(ctx, "Cannot setup logging, %s", err) |
| 92 | } |
| 93 | |
| 94 | // Setup default logger - applies for packages that do not have specific logger set |
| 95 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{}); err != nil { |
| 96 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
| 97 | } |
| 98 | |
| 99 | // Update all loggers (provisionned via init) with a common field |
| 100 | if err := log.UpdateAllLoggers(log.Fields{}); err != nil { |
| 101 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
| 102 | } |
| 103 | |
| 104 | log.SetAllLogLevel(logLevel) |
| 105 | |
| 106 | defer func() { |
| 107 | err := log.CleanUp() |
| 108 | if err != nil { |
| 109 | logger.Errorw(context.Background(), "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
| 110 | } |
| 111 | }() |
| 112 | |
| 113 | // Print version and exit |
| 114 | if conf.PrintVersion { |
| 115 | printVersion() |
| 116 | return |
| 117 | } |
| 118 | |
| 119 | // Print banner if specified |
| 120 | if conf.PrintBanner { |
| 121 | printBanner() |
| 122 | } |
| 123 | |
| 124 | logger.Infow(ctx, "config", log.Fields{"config": *conf}) |
| 125 | |
| 126 | p := &probe.Probe{} |
| 127 | go p.ListenAndServe(ctx, conf.ProbeAddress) |
| 128 | |
| 129 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 130 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(conf.TraceEnabled, conf.TraceAgentAddress, conf.LogCorrelationEnabled) |
| 131 | if err != nil { |
| 132 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 133 | } else { |
| 134 | defer log.TerminateTracing(closer) |
| 135 | } |
| 136 | |
| 137 | //Configure readiness probe |
| 138 | p.RegisterService( |
| 139 | probeCtx, |
| 140 | bbfAdapterService, |
| 141 | ) |
| 142 | |
| 143 | go func() { |
| 144 | probe.UpdateStatusFromContext(probeCtx, bbfAdapterService, probe.ServiceStatusRunning) |
| 145 | |
| 146 | for { |
| 147 | select { |
| 148 | case <-ctx.Done(): |
| 149 | logger.Info(ctx, "Context closed") |
| 150 | break |
| 151 | case <-time.After(15 * time.Second): |
| 152 | logger.Warn(ctx, "BBF Adapter currently has no implemented logic.") |
| 153 | } |
| 154 | } |
| 155 | }() |
| 156 | |
| 157 | code := waitForExit(ctx) |
| 158 | logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code}) |
| 159 | |
| 160 | elapsed := time.Since(start) |
| 161 | logger.Infow(ctx, "run-time", log.Fields{"time": elapsed.Seconds()}) |
| 162 | } |