blob: e8bc6abb3df4f1c07333875afdefe0ebe2c51070 [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"
khenaidoo5c11af72018-07-20 17:21:05 -040023 "time"
npujar1d86a522019-11-14 17:11:16 +053024
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"
khenaidood948f772021-08-11 17:49:24 -040028 "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"
khenaidoocfee5f42018-07-19 22:47:38 -040031)
32
khenaidoo5c11af72018-07-20 17:21:05 -040033func printBanner() {
Kent Hagerman2f0d0552020-04-23 17:28:52 -040034 fmt.Println(` `)
35 fmt.Println(` ______ ______ `)
36 fmt.Println(`| _ \ \ / / ___|___ _ __ ___ `)
37 fmt.Println(`| |_) \ \ /\ / / | / _ \| '__/ _ \`)
38 fmt.Println(`| _ < \ V V /| |__| (_) | | | __/`)
39 fmt.Println(`|_| \_\ \_/\_/ \____\___/|_| \___|`)
40 fmt.Println(` `)
khenaidoo5c11af72018-07-20 17:21:05 -040041}
42
David K. Bainbridgef430cd52019-05-28 15:00:35 -070043func printVersion() {
44 fmt.Println("VOLTHA Read-Write Core")
45 fmt.Println(version.VersionInfo.String(" "))
46}
47
khenaidoocfee5f42018-07-19 22:47:38 -040048func main() {
49 start := time.Now()
50
Rohan Agrawal31f21802020-06-12 05:38:46 +000051 ctx := context.Background()
52
David K. Bainbridge6080c172021-07-24 00:22:28 +000053 cf := &config.RWCoreFlags{}
54 cf.ParseCommandArguments(os.Args[1:])
khenaidoocfee5f42018-07-19 22:47:38 -040055
khenaidoo631fe542019-05-31 15:44:43 -040056 // Set the instance ID as the hostname
npujar1d86a522019-11-14 17:11:16 +053057 var instanceID string
khenaidoo631fe542019-05-31 15:44:43 -040058 hostName := utils.GetHostName()
59 if len(hostName) > 0 {
npujar1d86a522019-11-14 17:11:16 +053060 instanceID = hostName
khenaidoo631fe542019-05-31 15:44:43 -040061 } else {
Rohan Agrawal31f21802020-06-12 05:38:46 +000062 logger.Fatal(ctx, "HOSTNAME not set")
khenaidoo631fe542019-05-31 15:44:43 -040063 }
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000064
Don Newton8eca4622020-02-10 16:44:48 -050065 realMain()
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000066
67 logLevel, err := log.StringToLogLevel(cf.LogLevel)
68 if err != nil {
69 panic(err)
70 }
71
khenaidoob9203542018-09-17 22:56:37 -040072 //Setup default logger - applies for packages that do not have specific logger set
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000073 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": instanceID}); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000074 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
khenaidoocfee5f42018-07-19 22:47:38 -040075 }
khenaidoob9203542018-09-17 22:56:37 -040076
khenaidoo631fe542019-05-31 15:44:43 -040077 // Update all loggers (provisioned via init) with a common field
npujar1d86a522019-11-14 17:11:16 +053078 if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceID}); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000079 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
khenaidoob9203542018-09-17 22:56:37 -040080 }
81
khenaidoo631fe542019-05-31 15:44:43 -040082 // Update all loggers to log level specified as input parameter
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000083 log.SetAllLogLevel(logLevel)
khenaidoo2c6a0992019-04-29 13:46:56 -040084
npujar1d86a522019-11-14 17:11:16 +053085 defer func() {
86 err := log.CleanUp()
87 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000088 logger.Errorw(ctx, "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
npujar1d86a522019-11-14 17:11:16 +053089 }
90 }()
khenaidoocfee5f42018-07-19 22:47:38 -040091
khenaidoo631fe542019-05-31 15:44:43 -040092 // Print version / build information and exit
David K. Bainbridgef430cd52019-05-28 15:00:35 -070093 if cf.DisplayVersionOnly {
94 printVersion()
95 return
96 }
97
khenaidoo5c11af72018-07-20 17:21:05 -040098 // Print banner if specified
99 if cf.Banner {
100 printBanner()
101 }
102
Rohan Agrawal31f21802020-06-12 05:38:46 +0000103 logger.Infow(ctx, "rw-core-config", log.Fields{"config": *cf})
khenaidoo5c11af72018-07-20 17:21:05 -0400104
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700105 // Create a context adding the status update channel
khenaidoo5c11af72018-07-20 17:21:05 -0400106 ctx, cancel := context.WithCancel(context.Background())
107 defer cancel()
khenaidoocfee5f42018-07-19 22:47:38 -0400108
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700109 /*
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 Agrawal31f21802020-06-12 05:38:46 +0000115 go p.ListenAndServe(ctx, cf.ProbeAddress)
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700116
117 // Add the probe to the context to pass to all the services started
118 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
119
Girish Kumarf8d4f8d2020-08-18 11:45:30 +0000120 closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled)
Girish Kumar33470e82020-06-15 13:53:13 +0000121 if err != nil {
122 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
123 } else {
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +0300124 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 Kumar33470e82020-06-15 13:53:13 +0000130 }
131
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400132 // create and start the core
khenaidood948f772021-08-11 17:49:24 -0400133 core, shutdownCtx := c.NewCore(probeCtx, instanceID, cf)
134 go core.Start(shutdownCtx, instanceID, cf)
khenaidoocfee5f42018-07-19 22:47:38 -0400135
khenaidood948f772021-08-11 17:49:24 -0400136 code := utils.WaitForExit(ctx)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000137 logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code})
khenaidoocfee5f42018-07-19 22:47:38 -0400138
139 // Cleanup before leaving
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400140 core.Stop()
khenaidoocfee5f42018-07-19 22:47:38 -0400141
142 elapsed := time.Since(start)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000143 logger.Infow(ctx, "rw-core-run-time", log.Fields{"core": instanceID, "time": elapsed / time.Second})
khenaidoocfee5f42018-07-19 22:47:38 -0400144}