blob: 70c4b3535a1290ebf643ae7127e0c222266a77ef [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
Joey Armstrong7a9af442024-01-03 19:26:36 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoobf6e7bb2018-08-14 22:27:29 -04003
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"
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +053022 "io"
khenaidoocfee5f42018-07-19 22:47:38 -040023 "os"
khenaidoo5c11af72018-07-20 17:21:05 -040024 "time"
npujar1d86a522019-11-14 17:11:16 +053025
26 "github.com/opencord/voltha-go/rw_core/config"
27 c "github.com/opencord/voltha-go/rw_core/core"
28 "github.com/opencord/voltha-go/rw_core/utils"
khenaidood948f772021-08-11 17:49:24 -040029 "github.com/opencord/voltha-lib-go/v7/pkg/log"
30 "github.com/opencord/voltha-lib-go/v7/pkg/probe"
31 "github.com/opencord/voltha-lib-go/v7/pkg/version"
khenaidoocfee5f42018-07-19 22:47:38 -040032)
33
khenaidoo5c11af72018-07-20 17:21:05 -040034func printBanner() {
Kent Hagerman2f0d0552020-04-23 17:28:52 -040035 fmt.Println(` `)
36 fmt.Println(` ______ ______ `)
37 fmt.Println(`| _ \ \ / / ___|___ _ __ ___ `)
38 fmt.Println(`| |_) \ \ /\ / / | / _ \| '__/ _ \`)
39 fmt.Println(`| _ < \ V V /| |__| (_) | | | __/`)
40 fmt.Println(`|_| \_\ \_/\_/ \____\___/|_| \___|`)
41 fmt.Println(` `)
khenaidoo5c11af72018-07-20 17:21:05 -040042}
43
David K. Bainbridgef430cd52019-05-28 15:00:35 -070044func printVersion() {
45 fmt.Println("VOLTHA Read-Write Core")
46 fmt.Println(version.VersionInfo.String(" "))
47}
48
khenaidoocfee5f42018-07-19 22:47:38 -040049func main() {
50 start := time.Now()
51
Rohan Agrawal31f21802020-06-12 05:38:46 +000052 ctx := context.Background()
53
David K. Bainbridge6080c172021-07-24 00:22:28 +000054 cf := &config.RWCoreFlags{}
55 cf.ParseCommandArguments(os.Args[1:])
khenaidoocfee5f42018-07-19 22:47:38 -040056
khenaidoo631fe542019-05-31 15:44:43 -040057 // Set the instance ID as the hostname
npujar1d86a522019-11-14 17:11:16 +053058 var instanceID string
khenaidoo631fe542019-05-31 15:44:43 -040059 hostName := utils.GetHostName()
60 if len(hostName) > 0 {
npujar1d86a522019-11-14 17:11:16 +053061 instanceID = hostName
khenaidoo631fe542019-05-31 15:44:43 -040062 } else {
Rohan Agrawal31f21802020-06-12 05:38:46 +000063 logger.Fatal(ctx, "HOSTNAME not set")
khenaidoo631fe542019-05-31 15:44:43 -040064 }
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000065
Don Newton8eca4622020-02-10 16:44:48 -050066 realMain()
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000067
68 logLevel, err := log.StringToLogLevel(cf.LogLevel)
69 if err != nil {
70 panic(err)
71 }
72
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +053073 // Setup default logger - applies for packages that do not have specific logger set
74 if _, err = log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": instanceID}); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000075 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
khenaidoocfee5f42018-07-19 22:47:38 -040076 }
khenaidoob9203542018-09-17 22:56:37 -040077
khenaidoo631fe542019-05-31 15:44:43 -040078 // Update all loggers (provisioned via init) with a common field
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +053079 if err = log.UpdateAllLoggers(log.Fields{"instanceId": instanceID}); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000080 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
khenaidoob9203542018-09-17 22:56:37 -040081 }
82
khenaidoo631fe542019-05-31 15:44:43 -040083 // Update all loggers to log level specified as input parameter
Rohan Agrawal7f72f0c2020-01-14 12:05:51 +000084 log.SetAllLogLevel(logLevel)
khenaidoo2c6a0992019-04-29 13:46:56 -040085
npujar1d86a522019-11-14 17:11:16 +053086 defer func() {
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +053087 err = log.CleanUp()
npujar1d86a522019-11-14 17:11:16 +053088 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000089 logger.Errorw(ctx, "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
npujar1d86a522019-11-14 17:11:16 +053090 }
91 }()
khenaidoocfee5f42018-07-19 22:47:38 -040092
khenaidoo631fe542019-05-31 15:44:43 -040093 // Print version / build information and exit
David K. Bainbridgef430cd52019-05-28 15:00:35 -070094 if cf.DisplayVersionOnly {
95 printVersion()
96 return
97 }
98
khenaidoo5c11af72018-07-20 17:21:05 -040099 // Print banner if specified
100 if cf.Banner {
101 printBanner()
102 }
103
Rohan Agrawal31f21802020-06-12 05:38:46 +0000104 logger.Infow(ctx, "rw-core-config", log.Fields{"config": *cf})
khenaidoo5c11af72018-07-20 17:21:05 -0400105
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700106 // Create a context adding the status update channel
khenaidoo5c11af72018-07-20 17:21:05 -0400107 ctx, cancel := context.WithCancel(context.Background())
108 defer cancel()
khenaidoocfee5f42018-07-19 22:47:38 -0400109
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700110 /*
111 * Create and start the liveness and readiness container management probes. This
112 * is done in the main function so just in case the main starts multiple other
113 * objects there can be a single probe end point for the process.
114 */
115 p := &probe.Probe{}
Rohan Agrawal31f21802020-06-12 05:38:46 +0000116 go p.ListenAndServe(ctx, cf.ProbeAddress)
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700117
118 // Add the probe to the context to pass to all the services started
119 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
120
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +0530121 var closer io.Closer
122 closer, err = log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled)
Girish Kumar33470e82020-06-15 13:53:13 +0000123 if err != nil {
124 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
125 } else {
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +0300126 defer func() {
127 err = closer.Close()
128 if err != nil {
129 logger.Errorw(ctx, "failed-to-close-trace-closer", log.Fields{"error": err})
130 }
131 }()
Girish Kumar33470e82020-06-15 13:53:13 +0000132 }
133
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400134 // create and start the core
khenaidood948f772021-08-11 17:49:24 -0400135 core, shutdownCtx := c.NewCore(probeCtx, instanceID, cf)
136 go core.Start(shutdownCtx, instanceID, cf)
khenaidoocfee5f42018-07-19 22:47:38 -0400137
khenaidood948f772021-08-11 17:49:24 -0400138 code := utils.WaitForExit(ctx)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000139 logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code})
khenaidoocfee5f42018-07-19 22:47:38 -0400140
141 // Cleanup before leaving
khenaidooa46458b2021-12-15 16:50:44 -0500142 core.Stop(shutdownCtx)
khenaidoocfee5f42018-07-19 22:47:38 -0400143
144 elapsed := time.Since(start)
Girish Gowdra11ddb232022-05-26 12:19:59 -0700145
Rohan Agrawal31f21802020-06-12 05:38:46 +0000146 logger.Infow(ctx, "rw-core-run-time", log.Fields{"core": instanceID, "time": elapsed / time.Second})
khenaidoocfee5f42018-07-19 22:47:38 -0400147}