David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "flag" |
| 22 | "fmt" |
| 23 | "github.com/opencord/ofagent-go/internal/pkg/ofagent" |
Maninder | 12b909f | 2020-10-23 14:23:36 +0530 | [diff] [blame] | 24 | 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. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 29 | "os" |
Neha Sharma | 87d43d7 | 2020-04-08 14:10:40 +0000 | [diff] [blame] | 30 | "time" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | func printBanner() { |
| 34 | fmt.Println(` ___ _____ _ _ `) |
| 35 | fmt.Println(` / _ \| ___/ \ __ _ ___ _ __ | |_ `) |
| 36 | fmt.Println(` | | | | |_ / _ \ / _' |/ _ \ '_ \| __|`) |
| 37 | fmt.Println(` | |_| | _/ ___ \ (_| | __/ | | | |_ `) |
| 38 | fmt.Println(` \___/|_|/_/ \_\__, |\___|_| |_|\__|`) |
| 39 | fmt.Println(` |___/ `) |
| 40 | } |
| 41 | |
| 42 | func printVersion() { |
| 43 | fmt.Println("OFAgent") |
| 44 | fmt.Println(version.VersionInfo.String(" ")) |
| 45 | } |
| 46 | |
Neha Sharma | 318a129 | 2020-05-14 19:53:26 +0000 | [diff] [blame] | 47 | func setLogConfig(ctx context.Context, kvStoreAddress, kvStoreType string, kvStoreTimeout time.Duration) (kvstore.Client, error) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 48 | client, err := kvstore.NewEtcdClient(ctx, kvStoreAddress, kvStoreTimeout, log.WarnLevel) |
Neha Sharma | 87d43d7 | 2020-04-08 14:10:40 +0000 | [diff] [blame] | 49 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 54 | cm := conf.NewConfigManager(ctx, client, kvStoreType, kvStoreAddress, kvStoreTimeout) |
divyadesai | 52dc088 | 2020-03-19 06:38:11 +0000 | [diff] [blame] | 55 | go conf.StartLogLevelConfigProcessing(cm, ctx) |
Girish Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 56 | go conf.StartLogFeaturesConfigProcessing(cm, ctx) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 57 | return client, nil |
| 58 | } |
| 59 | |
| 60 | func 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 Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 66 | logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err}) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 67 | } |
| 68 | // Close the DB connection |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 69 | kvClient.Close(ctx) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | } |
| 73 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 74 | func 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 Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 88 | /* |
| 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 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 95 | // Setup logging |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 96 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 97 | logLevel, err := log.StringToLogLevel(config.LogLevel) |
| 98 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 99 | logger.Fatalf(ctx, "Cannot setup logging, %s", err) |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 100 | } |
| 101 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 102 | // 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 Kumar | be2ea8a | 2020-08-19 17:52:55 +0000 | [diff] [blame] | 104 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 105 | } |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 106 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 107 | // Update all loggers (provisionned via init) with a common field |
| 108 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil { |
Girish Kumar | be2ea8a | 2020-08-19 17:52:55 +0000 | [diff] [blame] | 109 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 110 | } |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 111 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 112 | log.SetAllLogLevel(logLevel) |
| 113 | |
Matteo Scandolo | a98f0dc | 2020-05-13 10:39:29 -0700 | [diff] [blame] | 114 | // depending on the build tags start the profiler |
| 115 | realMain() |
| 116 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 117 | defer func() { |
| 118 | err := log.CleanUp() |
| 119 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 120 | logger.Errorw(ctx, "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 121 | } |
| 122 | }() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 123 | |
Girish Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 124 | logger.Infow(ctx, "ofagent-config", log.Fields{"config": *config}) |
| 125 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 126 | /* |
| 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 Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 131 | go p.ListenAndServe(ctx, config.ProbeEndPoint) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 132 | |
Neha Sharma | 318a129 | 2020-05-14 19:53:26 +0000 | [diff] [blame] | 133 | client, err := setLogConfig(ctx, config.KVStoreAddress, config.KVStoreType, config.KVStoreTimeout) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 134 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 135 | logger.Warnw(ctx, "unable-to-create-kvstore-client", log.Fields{"error": err}) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Girish Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 138 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(config.TraceEnabled, config.TraceAgentAddress, config.LogCorrelationEnabled) |
Girish Kumar | adc3ba1 | 2020-06-15 14:22:55 +0000 | [diff] [blame] | 139 | 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 Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 145 | ofa, err := ofagent.NewOFAgent(ctx, &ofagent.OFAgent{ |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 146 | OFControllerEndPoints: config.OFControllerEndPoints, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 147 | VolthaApiEndPoint: config.VolthaApiEndPoint, |
| 148 | DeviceListRefreshInterval: config.DeviceListRefreshInterval, |
| 149 | ConnectionMaxRetries: config.ConnectionMaxRetries, |
| 150 | ConnectionRetryDelay: config.ConnectionRetryDelay, |
| 151 | }) |
| 152 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 153 | logger.Fatalw(ctx, "failed-to-create-ofagent", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 154 | log.Fields{ |
| 155 | "error": err}) |
| 156 | } |
| 157 | ofa.Run(ctx) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 158 | stop(ctx, client) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 159 | } |