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