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" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 23 | "os" |
Neha Sharma | 87d43d7 | 2020-04-08 14:10:40 +0000 | [diff] [blame] | 24 | "time" |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 25 | |
| 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. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | func printBanner() { |
| 35 | fmt.Println(` ___ _____ _ _ `) |
| 36 | fmt.Println(` / _ \| ___/ \ __ _ ___ _ __ | |_ `) |
| 37 | fmt.Println(` | | | | |_ / _ \ / _' |/ _ \ '_ \| __|`) |
| 38 | fmt.Println(` | |_| | _/ ___ \ (_| | __/ | | | |_ `) |
| 39 | fmt.Println(` \___/|_|/_/ \_\__, |\___|_| |_|\__|`) |
| 40 | fmt.Println(` |___/ `) |
| 41 | } |
| 42 | |
| 43 | func printVersion() { |
| 44 | fmt.Println("OFAgent") |
| 45 | fmt.Println(version.VersionInfo.String(" ")) |
| 46 | } |
| 47 | |
Neha Sharma | 318a129 | 2020-05-14 19:53:26 +0000 | [diff] [blame] | 48 | 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] | 49 | client, err := kvstore.NewEtcdClient(ctx, kvStoreAddress, kvStoreTimeout, log.WarnLevel) |
Neha Sharma | 87d43d7 | 2020-04-08 14:10:40 +0000 | [diff] [blame] | 50 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 55 | cm := conf.NewConfigManager(ctx, client, kvStoreType, kvStoreAddress, kvStoreTimeout) |
divyadesai | 52dc088 | 2020-03-19 06:38:11 +0000 | [diff] [blame] | 56 | go conf.StartLogLevelConfigProcessing(cm, ctx) |
Girish Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 57 | go conf.StartLogFeaturesConfigProcessing(cm, ctx) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 58 | return client, nil |
| 59 | } |
| 60 | |
| 61 | func 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 Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 67 | logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err}) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 68 | } |
| 69 | // Close the DB connection |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 70 | kvClient.Close(ctx) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | } |
| 74 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 75 | func 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 Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 89 | /* |
| 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 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 96 | // Setup logging |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 97 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 98 | logLevel, err := log.StringToLogLevel(config.LogLevel) |
| 99 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 100 | logger.Fatalf(ctx, "Cannot setup logging, %s", err) |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 101 | } |
| 102 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 103 | // Setup default logger - applies for packages that do not have specific logger set |
Andrey Pozolotin | 536ee58 | 2021-05-28 16:31:44 +0300 | [diff] [blame] | 104 | 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] | 105 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 106 | } |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 107 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 108 | // Update all loggers (provisionned via init) with a common field |
Andrey Pozolotin | 536ee58 | 2021-05-28 16:31:44 +0300 | [diff] [blame] | 109 | if err = log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil { |
Girish Kumar | be2ea8a | 2020-08-19 17:52:55 +0000 | [diff] [blame] | 110 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 111 | } |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 112 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 113 | log.SetAllLogLevel(logLevel) |
| 114 | |
Matteo Scandolo | a98f0dc | 2020-05-13 10:39:29 -0700 | [diff] [blame] | 115 | // depending on the build tags start the profiler |
| 116 | realMain() |
| 117 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 118 | defer func() { |
Andrey Pozolotin | 536ee58 | 2021-05-28 16:31:44 +0300 | [diff] [blame] | 119 | err = log.CleanUp() |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 120 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 121 | 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] | 122 | } |
| 123 | }() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 124 | |
Girish Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 125 | logger.Infow(ctx, "ofagent-config", log.Fields{"config": *config}) |
| 126 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 127 | /* |
| 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 Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 132 | go p.ListenAndServe(ctx, config.ProbeEndPoint) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 133 | |
Neha Sharma | 318a129 | 2020-05-14 19:53:26 +0000 | [diff] [blame] | 134 | client, err := setLogConfig(ctx, config.KVStoreAddress, config.KVStoreType, config.KVStoreTimeout) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 135 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 136 | logger.Warnw(ctx, "unable-to-create-kvstore-client", log.Fields{"error": err}) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Girish Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 139 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(config.TraceEnabled, config.TraceAgentAddress, config.LogCorrelationEnabled) |
Girish Kumar | adc3ba1 | 2020-06-15 14:22:55 +0000 | [diff] [blame] | 140 | if err != nil { |
| 141 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 142 | } else { |
Andrey Pozolotin | 536ee58 | 2021-05-28 16:31:44 +0300 | [diff] [blame] | 143 | defer func() { |
| 144 | err = closer.Close() |
| 145 | if err != nil { |
| 146 | logger.Errorf(ctx, "failed to close global LFM: %v", err) |
| 147 | } |
| 148 | }() |
Girish Kumar | adc3ba1 | 2020-06-15 14:22:55 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 151 | ofa, err := ofagent.NewOFAgent(ctx, &ofagent.OFAgent{ |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 152 | OFControllerEndPoints: config.OFControllerEndPoints, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 153 | VolthaApiEndPoint: config.VolthaApiEndPoint, |
| 154 | DeviceListRefreshInterval: config.DeviceListRefreshInterval, |
| 155 | ConnectionMaxRetries: config.ConnectionMaxRetries, |
| 156 | ConnectionRetryDelay: config.ConnectionRetryDelay, |
| 157 | }) |
| 158 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 159 | logger.Fatalw(ctx, "failed-to-create-ofagent", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 160 | log.Fields{ |
| 161 | "error": err}) |
| 162 | } |
| 163 | ofa.Run(ctx) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 164 | stop(ctx, client) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 165 | } |