blob: 19de6f00f3d6009d6c7629c1292e4e862004b47b [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"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080030 "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"
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
khenaidoo631fe542019-05-31 15:44:43 -0400109 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
khenaidoob9203542018-09-17 22:56:37 -0400110
npujar1d86a522019-11-14 17:11:16 +0530111 defer func() {
112 err := log.CleanUp()
113 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000114 logger.Errorw(ctx, "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
npujar1d86a522019-11-14 17:11:16 +0530115 }
116 }()
khenaidoocfee5f42018-07-19 22:47:38 -0400117
khenaidoo631fe542019-05-31 15:44:43 -0400118 // Print version / build information and exit
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700119 if cf.DisplayVersionOnly {
120 printVersion()
121 return
122 }
123
khenaidoo5c11af72018-07-20 17:21:05 -0400124 // Print banner if specified
125 if cf.Banner {
126 printBanner()
127 }
128
Rohan Agrawal31f21802020-06-12 05:38:46 +0000129 logger.Infow(ctx, "rw-core-config", log.Fields{"config": *cf})
khenaidoo5c11af72018-07-20 17:21:05 -0400130
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700131 // Create a context adding the status update channel
khenaidoo5c11af72018-07-20 17:21:05 -0400132 ctx, cancel := context.WithCancel(context.Background())
133 defer cancel()
khenaidoocfee5f42018-07-19 22:47:38 -0400134
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700135 /*
136 * Create and start the liveness and readiness container management probes. This
137 * is done in the main function so just in case the main starts multiple other
138 * objects there can be a single probe end point for the process.
139 */
140 p := &probe.Probe{}
Rohan Agrawal31f21802020-06-12 05:38:46 +0000141 go p.ListenAndServe(ctx, cf.ProbeAddress)
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700142
143 // Add the probe to the context to pass to all the services started
144 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
145
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400146 // create and start the core
147 core := c.NewCore(probeCtx, instanceID, cf)
khenaidoocfee5f42018-07-19 22:47:38 -0400148
Rohan Agrawal31f21802020-06-12 05:38:46 +0000149 code := waitForExit(ctx)
150 logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code})
khenaidoocfee5f42018-07-19 22:47:38 -0400151
152 // Cleanup before leaving
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400153 core.Stop()
khenaidoocfee5f42018-07-19 22:47:38 -0400154
155 elapsed := time.Since(start)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000156 logger.Infow(ctx, "rw-core-run-time", log.Fields{"core": instanceID, "time": elapsed / time.Second})
khenaidoocfee5f42018-07-19 22:47:38 -0400157}