blob: c51b726bb2cff50146b712865fc8b597c7358b9f [file] [log] [blame]
David K. Bainbridge157bdab2020-01-16 14:38:05 -08001/*
2 Copyright 2019 the original author or authors.
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*/
16
17package main
18
19import (
20 "context"
21 "flag"
22 "fmt"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080023 "os"
Neha Sharma87d43d72020-04-08 14:10:40 +000024 "time"
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000025
26 "github.com/opencord/ofagent-go/internal/pkg/ofagent"
27 conf "github.com/opencord/voltha-lib-go/v7/pkg/config"
28 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
29 "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"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080032)
33
34func printBanner() {
35 fmt.Println(` ___ _____ _ _ `)
36 fmt.Println(` / _ \| ___/ \ __ _ ___ _ __ | |_ `)
37 fmt.Println(` | | | | |_ / _ \ / _' |/ _ \ '_ \| __|`)
38 fmt.Println(` | |_| | _/ ___ \ (_| | __/ | | | |_ `)
39 fmt.Println(` \___/|_|/_/ \_\__, |\___|_| |_|\__|`)
40 fmt.Println(` |___/ `)
41}
42
43func printVersion() {
44 fmt.Println("OFAgent")
45 fmt.Println(version.VersionInfo.String(" "))
46}
47
Neha Sharma318a1292020-05-14 19:53:26 +000048func setLogConfig(ctx context.Context, kvStoreAddress, kvStoreType string, kvStoreTimeout time.Duration) (kvstore.Client, error) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000049 client, err := kvstore.NewEtcdClient(ctx, kvStoreAddress, kvStoreTimeout, log.WarnLevel)
Neha Sharma87d43d72020-04-08 14:10:40 +000050
divyadesai81bb7ba2020-03-11 11:45:23 +000051 if err != nil {
52 return nil, err
53 }
54
Rohan Agrawalc32d9932020-06-15 11:01:47 +000055 cm := conf.NewConfigManager(ctx, client, kvStoreType, kvStoreAddress, kvStoreTimeout)
divyadesai52dc0882020-03-19 06:38:11 +000056 go conf.StartLogLevelConfigProcessing(cm, ctx)
Girish Kumarcd402012020-08-18 12:17:38 +000057 go conf.StartLogFeaturesConfigProcessing(cm, ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +000058 return client, nil
59}
60
61func stop(ctx context.Context, kvClient kvstore.Client) {
62
63 // Cleanup - applies only if we had a kvClient
64 if kvClient != nil {
65 // Release all reservations
66 if err := kvClient.ReleaseAllReservations(ctx); err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000067 logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +000068 }
69 // Close the DB connection
Rohan Agrawalc32d9932020-06-15 11:01:47 +000070 kvClient.Close(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +000071 }
72
73}
74
David K. Bainbridge157bdab2020-01-16 14:38:05 -080075func main() {
76
77 config, _ := parseCommandLineArguments()
78 flag.Parse()
79
80 if config.Version {
81 printVersion()
82 os.Exit(0)
83 }
84
85 if config.Banner {
86 printBanner()
87 }
88
Rohan Agrawalc32d9932020-06-15 11:01:47 +000089 /*
90 * Create a context which to start the services used by the applicaiton
91 * and attach the probe to that context
92 */
93 p := &probe.Probe{}
94 ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p)
95
divyadesai81bb7ba2020-03-11 11:45:23 +000096 // Setup logging
David K. Bainbridge157bdab2020-01-16 14:38:05 -080097
divyadesai81bb7ba2020-03-11 11:45:23 +000098 logLevel, err := log.StringToLogLevel(config.LogLevel)
99 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000100 logger.Fatalf(ctx, "Cannot setup logging, %s", err)
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800101 }
102
divyadesai81bb7ba2020-03-11 11:45:23 +0000103 // Setup default logger - applies for packages that do not have specific logger set
Andrey Pozolotin536ee582021-05-28 16:31:44 +0300104 if _, err = log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": config.InstanceID}); err != nil {
Girish Kumarbe2ea8a2020-08-19 17:52:55 +0000105 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800106 }
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800107
divyadesai81bb7ba2020-03-11 11:45:23 +0000108 // Update all loggers (provisionned via init) with a common field
Andrey Pozolotin536ee582021-05-28 16:31:44 +0300109 if err = log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil {
Girish Kumarbe2ea8a2020-08-19 17:52:55 +0000110 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800111 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000112
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800113 log.SetAllLogLevel(logLevel)
114
Matteo Scandoloa98f0dc2020-05-13 10:39:29 -0700115 // depending on the build tags start the profiler
116 realMain()
117
divyadesai81bb7ba2020-03-11 11:45:23 +0000118 defer func() {
Andrey Pozolotin536ee582021-05-28 16:31:44 +0300119 err = log.CleanUp()
divyadesai81bb7ba2020-03-11 11:45:23 +0000120 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000121 logger.Errorw(ctx, "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000122 }
123 }()
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800124
Girish Kumarcd402012020-08-18 12:17:38 +0000125 logger.Infow(ctx, "ofagent-config", log.Fields{"config": *config})
126
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800127 /*
128 * Create and start the liveness and readiness container management probes. This
129 * is done in the main function so just in case the main starts multiple other
130 * objects there can be a single probe end point for the process.
131 */
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000132 go p.ListenAndServe(ctx, config.ProbeEndPoint)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800133
Neha Sharma318a1292020-05-14 19:53:26 +0000134 client, err := setLogConfig(ctx, config.KVStoreAddress, config.KVStoreType, config.KVStoreTimeout)
divyadesai81bb7ba2020-03-11 11:45:23 +0000135 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000136 logger.Warnw(ctx, "unable-to-create-kvstore-client", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000137 }
138
Girish Kumarcd402012020-08-18 12:17:38 +0000139 closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(config.TraceEnabled, config.TraceAgentAddress, config.LogCorrelationEnabled)
Girish Kumaradc3ba12020-06-15 14:22:55 +0000140 if err != nil {
141 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
142 } else {
Andrey Pozolotin536ee582021-05-28 16:31:44 +0300143 defer func() {
144 err = closer.Close()
145 if err != nil {
146 logger.Errorf(ctx, "failed to close global LFM: %v", err)
147 }
148 }()
Girish Kumaradc3ba12020-06-15 14:22:55 +0000149 }
150
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000151 ofa, err := ofagent.NewOFAgent(ctx, &ofagent.OFAgent{
Jonathan Hart4b110f62020-03-13 17:36:19 -0700152 OFControllerEndPoints: config.OFControllerEndPoints,
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800153 VolthaApiEndPoint: config.VolthaApiEndPoint,
154 DeviceListRefreshInterval: config.DeviceListRefreshInterval,
155 ConnectionMaxRetries: config.ConnectionMaxRetries,
156 ConnectionRetryDelay: config.ConnectionRetryDelay,
157 })
158 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000159 logger.Fatalw(ctx, "failed-to-create-ofagent",
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800160 log.Fields{
161 "error": err})
162 }
163 ofa.Run(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +0000164 stop(ctx, client)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800165}