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" |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 24 | conf "github.com/opencord/voltha-lib-go/v3/pkg/config" |
| 25 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
David K. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 26 | "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. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 29 | "os" |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 30 | "strconv" |
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 | |
David K. Bainbridge | 884de61 | 2020-02-19 14:14:38 -0800 | [diff] [blame] | 47 | func init() { |
| 48 | _, err := log.AddPackage(log.JSON, log.DebugLevel, nil) |
| 49 | if err != nil { |
| 50 | log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err}) |
| 51 | } |
| 52 | } |
| 53 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 54 | func setLogConfig(ctx context.Context, kvStoreHost, kvStoreType string, kvStorePort, kvStoreTimeout int) (kvstore.Client, error) { |
| 55 | client, err := kvstore.NewEtcdClient(kvStoreHost+":"+strconv.Itoa(kvStorePort), kvStoreTimeout) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | |
| 60 | cm := conf.NewConfigManager(client, kvStoreType, kvStoreHost, kvStorePort, kvStoreTimeout) |
| 61 | go conf.ProcessLogConfigChange(cm, ctx) |
| 62 | return client, nil |
| 63 | } |
| 64 | |
| 65 | func stop(ctx context.Context, kvClient kvstore.Client) { |
| 66 | |
| 67 | // Cleanup - applies only if we had a kvClient |
| 68 | if kvClient != nil { |
| 69 | // Release all reservations |
| 70 | if err := kvClient.ReleaseAllReservations(ctx); err != nil { |
| 71 | log.Infow("fail-to-release-all-reservations", log.Fields{"error": err}) |
| 72 | } |
| 73 | // Close the DB connection |
| 74 | kvClient.Close() |
| 75 | } |
| 76 | |
| 77 | } |
| 78 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 79 | func main() { |
| 80 | |
| 81 | config, _ := parseCommandLineArguments() |
| 82 | flag.Parse() |
| 83 | |
| 84 | if config.Version { |
| 85 | printVersion() |
| 86 | os.Exit(0) |
| 87 | } |
| 88 | |
| 89 | if config.Banner { |
| 90 | printBanner() |
| 91 | } |
| 92 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 93 | // Setup logging |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 94 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 95 | logLevel, err := log.StringToLogLevel(config.LogLevel) |
| 96 | if err != nil { |
| 97 | log.Fatalf("Cannot setup logging, %s", err) |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 98 | } |
| 99 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 100 | // Setup default logger - applies for packages that do not have specific logger set |
| 101 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": config.InstanceID}); err != nil { |
| 102 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 103 | } |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 104 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 105 | // Update all loggers (provisionned via init) with a common field |
| 106 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil { |
| 107 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 108 | } |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 109 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 110 | log.SetAllLogLevel(logLevel) |
| 111 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 112 | defer func() { |
| 113 | err := log.CleanUp() |
| 114 | if err != nil { |
| 115 | log.Errorw("unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
| 116 | } |
| 117 | }() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * Create and start the liveness and readiness container management probes. This |
| 121 | * is done in the main function so just in case the main starts multiple other |
| 122 | * objects there can be a single probe end point for the process. |
| 123 | */ |
| 124 | p := &probe.Probe{} |
| 125 | go p.ListenAndServe(config.ProbeEndPoint) |
| 126 | |
| 127 | /* |
| 128 | * Create a context which to start the services used by the applicaiton |
| 129 | * and attach the probe to that context |
| 130 | */ |
| 131 | ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p) |
| 132 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 133 | client, err := setLogConfig(ctx, config.KVStoreHost, config.KVStoreType, config.KVStorePort, config.KVStoreTimeout) |
| 134 | if err != nil { |
| 135 | log.Warnw("unable-to-create-kvstore-client", log.Fields{"error": err}) |
| 136 | } |
| 137 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 138 | ofa, err := ofagent.NewOFAgent(&ofagent.OFAgent{ |
| 139 | OFControllerEndPoint: config.OFControllerEndPoint, |
| 140 | VolthaApiEndPoint: config.VolthaApiEndPoint, |
| 141 | DeviceListRefreshInterval: config.DeviceListRefreshInterval, |
| 142 | ConnectionMaxRetries: config.ConnectionMaxRetries, |
| 143 | ConnectionRetryDelay: config.ConnectionRetryDelay, |
| 144 | }) |
| 145 | if err != nil { |
| 146 | log.Fatalw("failed-to-create-ofagent", |
| 147 | log.Fields{ |
| 148 | "error": err}) |
| 149 | } |
| 150 | ofa.Run(ctx) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 151 | stop(ctx, client) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 152 | } |