blob: ddfb2b1c59507dd2e743447e99ba5b90c0d37f93 [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
khenaidoocfee5f42018-07-19 22:47:38 -040035func 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 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:
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 }
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
75 cf := config.NewRWCoreFlags()
76 cf.ParseCommandArguments()
77
khenaidoo631fe542019-05-31 15:44:43 -040078 // Set the instance ID as the hostname
npujar1d86a522019-11-14 17:11:16 +053079 var instanceID string
khenaidoo631fe542019-05-31 15:44:43 -040080 hostName := utils.GetHostName()
81 if len(hostName) > 0 {
npujar1d86a522019-11-14 17:11:16 +053082 instanceID = hostName
khenaidoo631fe542019-05-31 15:44:43 -040083 } else {
Girish Kumarf56a4682020-03-20 20:07:46 +000084 logger.Fatal("HOSTNAME not set")
khenaidoo631fe542019-05-31 15:44:43 -040085 }
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000086
Don Newton8eca4622020-02-10 16:44:48 -050087 realMain()
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000088
89 logLevel, err := log.StringToLogLevel(cf.LogLevel)
90 if err != nil {
91 panic(err)
92 }
93
khenaidoob9203542018-09-17 22:56:37 -040094 //Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000095 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": instanceID}); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000096 logger.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
khenaidoocfee5f42018-07-19 22:47:38 -040097 }
khenaidoob9203542018-09-17 22:56:37 -040098
khenaidoo631fe542019-05-31 15:44:43 -040099 // Update all loggers (provisioned via init) with a common field
npujar1d86a522019-11-14 17:11:16 +0530100 if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceID}); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000101 logger.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
khenaidoob9203542018-09-17 22:56:37 -0400102 }
103
khenaidoo631fe542019-05-31 15:44:43 -0400104 // Update all loggers to log level specified as input parameter
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +0000105 log.SetAllLogLevel(logLevel)
khenaidoo2c6a0992019-04-29 13:46:56 -0400106
khenaidoo631fe542019-05-31 15:44:43 -0400107 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
khenaidoob9203542018-09-17 22:56:37 -0400108
npujar1d86a522019-11-14 17:11:16 +0530109 defer func() {
110 err := log.CleanUp()
111 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000112 logger.Errorw("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
Girish Kumarf56a4682020-03-20 20:07:46 +0000127 logger.Infow("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{}
Neha Sharmad1387da2020-05-07 20:07:28 +0000139 go p.ListenAndServe(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
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400144 // create and start the core
145 core := c.NewCore(probeCtx, instanceID, cf)
khenaidoocfee5f42018-07-19 22:47:38 -0400146
147 code := waitForExit()
Girish Kumarf56a4682020-03-20 20:07:46 +0000148 logger.Infow("received-a-closing-signal", log.Fields{"code": code})
khenaidoocfee5f42018-07-19 22:47:38 -0400149
150 // Cleanup before leaving
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400151 core.Stop()
khenaidoocfee5f42018-07-19 22:47:38 -0400152
153 elapsed := time.Since(start)
Girish Kumarf56a4682020-03-20 20:07:46 +0000154 logger.Infow("rw-core-run-time", log.Fields{"core": instanceID, "time": elapsed / time.Second})
khenaidoocfee5f42018-07-19 22:47:38 -0400155}