blob: 8a769be58d8d88b07e96f3667788e4c6595b123d [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"
23 "github.com/opencord/ofagent-go/internal/pkg/ofagent"
Maninder12b909f2020-10-23 14:23:36 +053024 conf "github.com/opencord/voltha-lib-go/v4/pkg/config"
25 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
26 "github.com/opencord/voltha-lib-go/v4/pkg/log"
27 "github.com/opencord/voltha-lib-go/v4/pkg/probe"
28 "github.com/opencord/voltha-lib-go/v4/pkg/version"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080029 "os"
Neha Sharma87d43d72020-04-08 14:10:40 +000030 "time"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080031)
32
33func printBanner() {
34 fmt.Println(` ___ _____ _ _ `)
35 fmt.Println(` / _ \| ___/ \ __ _ ___ _ __ | |_ `)
36 fmt.Println(` | | | | |_ / _ \ / _' |/ _ \ '_ \| __|`)
37 fmt.Println(` | |_| | _/ ___ \ (_| | __/ | | | |_ `)
38 fmt.Println(` \___/|_|/_/ \_\__, |\___|_| |_|\__|`)
39 fmt.Println(` |___/ `)
40}
41
42func printVersion() {
43 fmt.Println("OFAgent")
44 fmt.Println(version.VersionInfo.String(" "))
45}
46
Neha Sharma318a1292020-05-14 19:53:26 +000047func setLogConfig(ctx context.Context, kvStoreAddress, kvStoreType string, kvStoreTimeout time.Duration) (kvstore.Client, error) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000048 client, err := kvstore.NewEtcdClient(ctx, kvStoreAddress, kvStoreTimeout, log.WarnLevel)
Neha Sharma87d43d72020-04-08 14:10:40 +000049
divyadesai81bb7ba2020-03-11 11:45:23 +000050 if err != nil {
51 return nil, err
52 }
53
Rohan Agrawalc32d9932020-06-15 11:01:47 +000054 cm := conf.NewConfigManager(ctx, client, kvStoreType, kvStoreAddress, kvStoreTimeout)
divyadesai52dc0882020-03-19 06:38:11 +000055 go conf.StartLogLevelConfigProcessing(cm, ctx)
Girish Kumarcd402012020-08-18 12:17:38 +000056 go conf.StartLogFeaturesConfigProcessing(cm, ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +000057 return client, nil
58}
59
60func stop(ctx context.Context, kvClient kvstore.Client) {
61
62 // Cleanup - applies only if we had a kvClient
63 if kvClient != nil {
64 // Release all reservations
65 if err := kvClient.ReleaseAllReservations(ctx); err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000066 logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +000067 }
68 // Close the DB connection
Rohan Agrawalc32d9932020-06-15 11:01:47 +000069 kvClient.Close(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +000070 }
71
72}
73
David K. Bainbridge157bdab2020-01-16 14:38:05 -080074func main() {
75
76 config, _ := parseCommandLineArguments()
77 flag.Parse()
78
79 if config.Version {
80 printVersion()
81 os.Exit(0)
82 }
83
84 if config.Banner {
85 printBanner()
86 }
87
Rohan Agrawalc32d9932020-06-15 11:01:47 +000088 /*
89 * Create a context which to start the services used by the applicaiton
90 * and attach the probe to that context
91 */
92 p := &probe.Probe{}
93 ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p)
94
divyadesai81bb7ba2020-03-11 11:45:23 +000095 // Setup logging
David K. Bainbridge157bdab2020-01-16 14:38:05 -080096
divyadesai81bb7ba2020-03-11 11:45:23 +000097 logLevel, err := log.StringToLogLevel(config.LogLevel)
98 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000099 logger.Fatalf(ctx, "Cannot setup logging, %s", err)
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800100 }
101
divyadesai81bb7ba2020-03-11 11:45:23 +0000102 // Setup default logger - applies for packages that do not have specific logger set
103 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": config.InstanceID}); err != nil {
Girish Kumarbe2ea8a2020-08-19 17:52:55 +0000104 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800105 }
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800106
divyadesai81bb7ba2020-03-11 11:45:23 +0000107 // Update all loggers (provisionned via init) with a common field
108 if err := log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil {
Girish Kumarbe2ea8a2020-08-19 17:52:55 +0000109 logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging")
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800110 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000111
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800112 log.SetAllLogLevel(logLevel)
113
Matteo Scandoloa98f0dc2020-05-13 10:39:29 -0700114 // depending on the build tags start the profiler
115 realMain()
116
divyadesai81bb7ba2020-03-11 11:45:23 +0000117 defer func() {
118 err := log.CleanUp()
119 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000120 logger.Errorw(ctx, "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000121 }
122 }()
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800123
Girish Kumarcd402012020-08-18 12:17:38 +0000124 logger.Infow(ctx, "ofagent-config", log.Fields{"config": *config})
125
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800126 /*
127 * Create and start the liveness and readiness container management probes. This
128 * is done in the main function so just in case the main starts multiple other
129 * objects there can be a single probe end point for the process.
130 */
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000131 go p.ListenAndServe(ctx, config.ProbeEndPoint)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800132
Neha Sharma318a1292020-05-14 19:53:26 +0000133 client, err := setLogConfig(ctx, config.KVStoreAddress, config.KVStoreType, config.KVStoreTimeout)
divyadesai81bb7ba2020-03-11 11:45:23 +0000134 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000135 logger.Warnw(ctx, "unable-to-create-kvstore-client", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000136 }
137
Girish Kumarcd402012020-08-18 12:17:38 +0000138 closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(config.TraceEnabled, config.TraceAgentAddress, config.LogCorrelationEnabled)
Girish Kumaradc3ba12020-06-15 14:22:55 +0000139 if err != nil {
140 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
141 } else {
142 defer closer.Close()
143 }
144
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000145 ofa, err := ofagent.NewOFAgent(ctx, &ofagent.OFAgent{
Jonathan Hart4b110f62020-03-13 17:36:19 -0700146 OFControllerEndPoints: config.OFControllerEndPoints,
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800147 VolthaApiEndPoint: config.VolthaApiEndPoint,
148 DeviceListRefreshInterval: config.DeviceListRefreshInterval,
149 ConnectionMaxRetries: config.ConnectionMaxRetries,
150 ConnectionRetryDelay: config.ConnectionRetryDelay,
151 })
152 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000153 logger.Fatalw(ctx, "failed-to-create-ofagent",
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800154 log.Fields{
155 "error": err})
156 }
157 ofa.Run(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +0000158 stop(ctx, client)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800159}