blob: 281756dd26b344b8a50e99a760bf04d9a77b41e3 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoocfee5f42018-07-19 22:47:38 -040017package main
18
19import (
khenaidoo5c11af72018-07-20 17:21:05 -040020 "context"
khenaidoocfee5f42018-07-19 22:47:38 -040021 "fmt"
22 "os"
23 "os/signal"
khenaidoocfee5f42018-07-19 22:47:38 -040024 "syscall"
khenaidoo5c11af72018-07-20 17:21:05 -040025 "time"
npujar1d86a522019-11-14 17:11:16 +053026
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"
yasin sapli5458a1c2021-06-14 22:24:38 +000030 "github.com/opencord/voltha-lib-go/v5/pkg/log"
31 "github.com/opencord/voltha-lib-go/v5/pkg/probe"
32 "github.com/opencord/voltha-lib-go/v5/pkg/version"
khenaidoocfee5f42018-07-19 22:47:38 -040033)
34
Rohan Agrawal31f21802020-06-12 05:38:46 +000035func waitForExit(ctx context.Context) int {
khenaidoocfee5f42018-07-19 22:47:38 -040036 signalChannel := make(chan os.Signal, 1)
37 signal.Notify(signalChannel,
38 syscall.SIGHUP,
39 syscall.SIGINT,
40 syscall.SIGTERM,
41 syscall.SIGQUIT)
42
Kent Hagerman2f0d0552020-04-23 17:28:52 -040043 s := <-signalChannel
44 switch s {
45 case syscall.SIGHUP,
46 syscall.SIGINT,
47 syscall.SIGTERM,
48 syscall.SIGQUIT:
Rohan Agrawal31f21802020-06-12 05:38:46 +000049 logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s})
Kent Hagerman2f0d0552020-04-23 17:28:52 -040050 return 0
51 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +000052 logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s})
Kent Hagerman2f0d0552020-04-23 17:28:52 -040053 return 1
54 }
khenaidoocfee5f42018-07-19 22:47:38 -040055}
56
khenaidoo5c11af72018-07-20 17:21:05 -040057func printBanner() {
Kent Hagerman2f0d0552020-04-23 17:28:52 -040058 fmt.Println(` `)
59 fmt.Println(` ______ ______ `)
60 fmt.Println(`| _ \ \ / / ___|___ _ __ ___ `)
61 fmt.Println(`| |_) \ \ /\ / / | / _ \| '__/ _ \`)
62 fmt.Println(`| _ < \ V V /| |__| (_) | | | __/`)
63 fmt.Println(`|_| \_\ \_/\_/ \____\___/|_| \___|`)
64 fmt.Println(` `)
khenaidoo5c11af72018-07-20 17:21:05 -040065}
66
David K. Bainbridgef430cd52019-05-28 15:00:35 -070067func printVersion() {
68 fmt.Println("VOLTHA Read-Write Core")
69 fmt.Println(version.VersionInfo.String(" "))
70}
71
khenaidoocfee5f42018-07-19 22:47:38 -040072func main() {
73 start := time.Now()
74
Rohan Agrawal31f21802020-06-12 05:38:46 +000075 ctx := context.Background()
76
khenaidoocfee5f42018-07-19 22:47:38 -040077 cf := config.NewRWCoreFlags()
78 cf.ParseCommandArguments()
79
khenaidoo631fe542019-05-31 15:44:43 -040080 // Set the instance ID as the hostname
npujar1d86a522019-11-14 17:11:16 +053081 var instanceID string
khenaidoo631fe542019-05-31 15:44:43 -040082 hostName := utils.GetHostName()
83 if len(hostName) > 0 {
npujar1d86a522019-11-14 17:11:16 +053084 instanceID = hostName
khenaidoo631fe542019-05-31 15:44:43 -040085 } else {
Rohan Agrawal31f21802020-06-12 05:38:46 +000086 logger.Fatal(ctx, "HOSTNAME not set")
khenaidoo631fe542019-05-31 15:44:43 -040087 }
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000088
Don Newton8eca4622020-02-10 16:44:48 -050089 realMain()
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000090
91 logLevel, err := log.StringToLogLevel(cf.LogLevel)
92 if err != nil {
93 panic(err)
94 }
95
khenaidoob9203542018-09-17 22:56:37 -040096 //Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000097 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": instanceID}); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000098 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
khenaidoocfee5f42018-07-19 22:47:38 -040099 }
khenaidoob9203542018-09-17 22:56:37 -0400100
khenaidoo631fe542019-05-31 15:44:43 -0400101 // Update all loggers (provisioned via init) with a common field
npujar1d86a522019-11-14 17:11:16 +0530102 if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceID}); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000103 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
khenaidoob9203542018-09-17 22:56:37 -0400104 }
105
khenaidoo631fe542019-05-31 15:44:43 -0400106 // Update all loggers to log level specified as input parameter
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +0000107 log.SetAllLogLevel(logLevel)
khenaidoo2c6a0992019-04-29 13:46:56 -0400108
npujar1d86a522019-11-14 17:11:16 +0530109 defer func() {
110 err := log.CleanUp()
111 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000112 logger.Errorw(ctx, "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
npujar1d86a522019-11-14 17:11:16 +0530113 }
114 }()
khenaidoocfee5f42018-07-19 22:47:38 -0400115
khenaidoo631fe542019-05-31 15:44:43 -0400116 // Print version / build information and exit
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700117 if cf.DisplayVersionOnly {
118 printVersion()
119 return
120 }
121
khenaidoo5c11af72018-07-20 17:21:05 -0400122 // Print banner if specified
123 if cf.Banner {
124 printBanner()
125 }
126
Rohan Agrawal31f21802020-06-12 05:38:46 +0000127 logger.Infow(ctx, "rw-core-config", log.Fields{"config": *cf})
khenaidoo5c11af72018-07-20 17:21:05 -0400128
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700129 // Create a context adding the status update channel
khenaidoo5c11af72018-07-20 17:21:05 -0400130 ctx, cancel := context.WithCancel(context.Background())
131 defer cancel()
khenaidoocfee5f42018-07-19 22:47:38 -0400132
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700133 /*
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{}
Rohan Agrawal31f21802020-06-12 05:38:46 +0000139 go p.ListenAndServe(ctx, cf.ProbeAddress)
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700140
141 // Add the probe to the context to pass to all the services started
142 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
143
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000144 closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled)
Girish Kumar33470e82020-06-15 13:53:13 +0000145 if err != nil {
146 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
147 } else {
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +0300148 defer func() {
149 err = closer.Close()
150 if err != nil {
151 logger.Errorw(ctx, "failed-to-close-trace-closer", log.Fields{"error": err})
152 }
153 }()
Girish Kumar33470e82020-06-15 13:53:13 +0000154 }
155
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400156 // create and start the core
157 core := c.NewCore(probeCtx, instanceID, cf)
khenaidoocfee5f42018-07-19 22:47:38 -0400158
Rohan Agrawal31f21802020-06-12 05:38:46 +0000159 code := waitForExit(ctx)
160 logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code})
khenaidoocfee5f42018-07-19 22:47:38 -0400161
162 // Cleanup before leaving
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400163 core.Stop()
khenaidoocfee5f42018-07-19 22:47:38 -0400164
165 elapsed := time.Since(start)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000166 logger.Infow(ctx, "rw-core-run-time", log.Fields{"core": instanceID, "time": elapsed / time.Second})
khenaidoocfee5f42018-07-19 22:47:38 -0400167}