khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 17 | package main |
| 18 | |
| 19 | import ( |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 20 | "context" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 21 | "fmt" |
| 22 | "os" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 23 | "time" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 24 | |
| 25 | "github.com/opencord/voltha-go/rw_core/config" |
| 26 | c "github.com/opencord/voltha-go/rw_core/core" |
| 27 | "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [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" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 31 | ) |
| 32 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 33 | func printBanner() { |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 34 | fmt.Println(` `) |
| 35 | fmt.Println(` ______ ______ `) |
| 36 | fmt.Println(`| _ \ \ / / ___|___ _ __ ___ `) |
| 37 | fmt.Println(`| |_) \ \ /\ / / | / _ \| '__/ _ \`) |
| 38 | fmt.Println(`| _ < \ V V /| |__| (_) | | | __/`) |
| 39 | fmt.Println(`|_| \_\ \_/\_/ \____\___/|_| \___|`) |
| 40 | fmt.Println(` `) |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 41 | } |
| 42 | |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 43 | func printVersion() { |
| 44 | fmt.Println("VOLTHA Read-Write Core") |
| 45 | fmt.Println(version.VersionInfo.String(" ")) |
| 46 | } |
| 47 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 48 | func main() { |
| 49 | start := time.Now() |
| 50 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 51 | ctx := context.Background() |
| 52 | |
David K. Bainbridge | 6080c17 | 2021-07-24 00:22:28 +0000 | [diff] [blame] | 53 | cf := &config.RWCoreFlags{} |
| 54 | cf.ParseCommandArguments(os.Args[1:]) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 55 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 56 | // Set the instance ID as the hostname |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 57 | var instanceID string |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 58 | hostName := utils.GetHostName() |
| 59 | if len(hostName) > 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 60 | instanceID = hostName |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 61 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 62 | logger.Fatal(ctx, "HOSTNAME not set") |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 63 | } |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 64 | |
Don Newton | 8eca462 | 2020-02-10 16:44:48 -0500 | [diff] [blame] | 65 | realMain() |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 66 | |
| 67 | logLevel, err := log.StringToLogLevel(cf.LogLevel) |
| 68 | if err != nil { |
| 69 | panic(err) |
| 70 | } |
| 71 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 72 | //Setup default logger - applies for packages that do not have specific logger set |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 73 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": instanceID}); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 74 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 75 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 76 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 77 | // Update all loggers (provisioned via init) with a common field |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 78 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceID}); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 79 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 80 | } |
| 81 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 82 | // Update all loggers to log level specified as input parameter |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 83 | log.SetAllLogLevel(logLevel) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 84 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 85 | defer func() { |
| 86 | err := log.CleanUp() |
| 87 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 88 | logger.Errorw(ctx, "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 89 | } |
| 90 | }() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 91 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 92 | // Print version / build information and exit |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 93 | if cf.DisplayVersionOnly { |
| 94 | printVersion() |
| 95 | return |
| 96 | } |
| 97 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 98 | // Print banner if specified |
| 99 | if cf.Banner { |
| 100 | printBanner() |
| 101 | } |
| 102 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 103 | logger.Infow(ctx, "rw-core-config", log.Fields{"config": *cf}) |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 104 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 105 | // Create a context adding the status update channel |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 106 | ctx, cancel := context.WithCancel(context.Background()) |
| 107 | defer cancel() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 108 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 109 | /* |
| 110 | * Create and start the liveness and readiness container management probes. This |
| 111 | * is done in the main function so just in case the main starts multiple other |
| 112 | * objects there can be a single probe end point for the process. |
| 113 | */ |
| 114 | p := &probe.Probe{} |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 115 | go p.ListenAndServe(ctx, cf.ProbeAddress) |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 116 | |
| 117 | // Add the probe to the context to pass to all the services started |
| 118 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 119 | |
Girish Kumar | f8d4f8d | 2020-08-18 11:45:30 +0000 | [diff] [blame] | 120 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled) |
Girish Kumar | 33470e8 | 2020-06-15 13:53:13 +0000 | [diff] [blame] | 121 | if err != nil { |
| 122 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 123 | } else { |
Andrey Pozolotin | 34dd63f | 2021-05-31 21:26:40 +0300 | [diff] [blame] | 124 | defer func() { |
| 125 | err = closer.Close() |
| 126 | if err != nil { |
| 127 | logger.Errorw(ctx, "failed-to-close-trace-closer", log.Fields{"error": err}) |
| 128 | } |
| 129 | }() |
Girish Kumar | 33470e8 | 2020-06-15 13:53:13 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 132 | // create and start the core |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 133 | core, shutdownCtx := c.NewCore(probeCtx, instanceID, cf) |
| 134 | go core.Start(shutdownCtx, instanceID, cf) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 135 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 136 | code := utils.WaitForExit(ctx) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 137 | logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 138 | |
| 139 | // Cleanup before leaving |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 140 | core.Stop() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 141 | |
| 142 | elapsed := time.Since(start) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 143 | logger.Infow(ctx, "rw-core-run-time", log.Fields{"core": instanceID, "time": elapsed / time.Second}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 144 | } |