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