blob: 1dd05fb429b920d3cc809adf3c2d0865237acd20 [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"
divyadesai81bb7ba2020-03-11 11:45:23 +000024 conf "github.com/opencord/voltha-lib-go/v3/pkg/config"
25 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080026 "github.com/opencord/voltha-lib-go/v3/pkg/log"
27 "github.com/opencord/voltha-lib-go/v3/pkg/probe"
28 "github.com/opencord/voltha-lib-go/v3/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)
divyadesai81bb7ba2020-03-11 11:45:23 +000056 return client, nil
57}
58
59func stop(ctx context.Context, kvClient kvstore.Client) {
60
61 // Cleanup - applies only if we had a kvClient
62 if kvClient != nil {
63 // Release all reservations
64 if err := kvClient.ReleaseAllReservations(ctx); err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000065 logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +000066 }
67 // Close the DB connection
Rohan Agrawalc32d9932020-06-15 11:01:47 +000068 kvClient.Close(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +000069 }
70
71}
72
David K. Bainbridge157bdab2020-01-16 14:38:05 -080073func main() {
74
75 config, _ := parseCommandLineArguments()
76 flag.Parse()
77
78 if config.Version {
79 printVersion()
80 os.Exit(0)
81 }
82
83 if config.Banner {
84 printBanner()
85 }
86
Rohan Agrawalc32d9932020-06-15 11:01:47 +000087 /*
88 * Create a context which to start the services used by the applicaiton
89 * and attach the probe to that context
90 */
91 p := &probe.Probe{}
92 ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p)
93
divyadesai81bb7ba2020-03-11 11:45:23 +000094 // Setup logging
David K. Bainbridge157bdab2020-01-16 14:38:05 -080095
divyadesai81bb7ba2020-03-11 11:45:23 +000096 logLevel, err := log.StringToLogLevel(config.LogLevel)
97 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000098 logger.Fatalf(ctx, "Cannot setup logging, %s", err)
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080099 }
100
divyadesai81bb7ba2020-03-11 11:45:23 +0000101 // Setup default logger - applies for packages that do not have specific logger set
102 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": config.InstanceID}); err != nil {
103 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800104 }
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800105
divyadesai81bb7ba2020-03-11 11:45:23 +0000106 // Update all loggers (provisionned via init) with a common field
107 if err := log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil {
108 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800109 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000110
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800111 log.SetAllLogLevel(logLevel)
112
Matteo Scandoloa98f0dc2020-05-13 10:39:29 -0700113 // depending on the build tags start the profiler
114 realMain()
115
divyadesai81bb7ba2020-03-11 11:45:23 +0000116 defer func() {
117 err := log.CleanUp()
118 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000119 logger.Errorw(ctx, "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000120 }
121 }()
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800122
123 /*
124 * Create and start the liveness and readiness container management probes. This
125 * is done in the main function so just in case the main starts multiple other
126 * objects there can be a single probe end point for the process.
127 */
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000128 go p.ListenAndServe(ctx, config.ProbeEndPoint)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800129
Neha Sharma318a1292020-05-14 19:53:26 +0000130 client, err := setLogConfig(ctx, config.KVStoreAddress, config.KVStoreType, config.KVStoreTimeout)
divyadesai81bb7ba2020-03-11 11:45:23 +0000131 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000132 logger.Warnw(ctx, "unable-to-create-kvstore-client", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000133 }
134
Girish Kumaradc3ba12020-06-15 14:22:55 +0000135 closer, err := log.InitTracingAndLogCorrelation(config.TraceEnabled, config.TraceAgentAddress, config.LogCorrelationEnabled)
136 if err != nil {
137 logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err})
138 } else {
139 defer closer.Close()
140 }
141
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000142 ofa, err := ofagent.NewOFAgent(ctx, &ofagent.OFAgent{
Jonathan Hart4b110f62020-03-13 17:36:19 -0700143 OFControllerEndPoints: config.OFControllerEndPoints,
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800144 VolthaApiEndPoint: config.VolthaApiEndPoint,
145 DeviceListRefreshInterval: config.DeviceListRefreshInterval,
146 ConnectionMaxRetries: config.ConnectionMaxRetries,
147 ConnectionRetryDelay: config.ConnectionRetryDelay,
148 })
149 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000150 logger.Fatalw(ctx, "failed-to-create-ofagent",
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800151 log.Fields{
152 "error": err})
153 }
154 ofa.Run(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +0000155 stop(ctx, client)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800156}