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" |
Neha Sharma | 87d43d7 | 2020-04-08 14:10:40 +0000 | [diff] [blame^] | 31 | "time" |
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 | 87d43d7 | 2020-04-08 14:10:40 +0000 | [diff] [blame^] | 48 | func setLogConfig(ctx context.Context, kvStoreHost, kvStoreType string, kvStorePort int, kvStoreTimeout time.Duration) (kvstore.Client, error) { |
Rohan Agrawal | 00d3a41 | 2020-04-22 10:51:39 +0000 | [diff] [blame] | 49 | client, err := kvstore.NewEtcdClient(kvStoreHost+":"+strconv.Itoa(kvStorePort), 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 | |
| 55 | cm := conf.NewConfigManager(client, kvStoreType, kvStoreHost, kvStorePort, kvStoreTimeout) |
divyadesai | 52dc088 | 2020-03-19 06:38:11 +0000 | [diff] [blame] | 56 | go conf.StartLogLevelConfigProcessing(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 { |
Girish Kumar | e9d7617 | 2020-03-20 20:26:04 +0000 | [diff] [blame] | 66 | logger.Infow("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 |
| 69 | kvClient.Close() |
| 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 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 88 | // Setup logging |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 89 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 90 | logLevel, err := log.StringToLogLevel(config.LogLevel) |
| 91 | if err != nil { |
Girish Kumar | e9d7617 | 2020-03-20 20:26:04 +0000 | [diff] [blame] | 92 | logger.Fatalf("Cannot setup logging, %s", err) |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 93 | } |
| 94 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 95 | // Setup default logger - applies for packages that do not have specific logger set |
| 96 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": config.InstanceID}); err != nil { |
| 97 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 98 | } |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 99 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 100 | // Update all loggers (provisionned via init) with a common field |
| 101 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil { |
| 102 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 103 | } |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 104 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 105 | log.SetAllLogLevel(logLevel) |
| 106 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 107 | defer func() { |
| 108 | err := log.CleanUp() |
| 109 | if err != nil { |
Girish Kumar | e9d7617 | 2020-03-20 20:26:04 +0000 | [diff] [blame] | 110 | logger.Errorw("unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 111 | } |
| 112 | }() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * Create and start the liveness and readiness container management probes. This |
| 116 | * is done in the main function so just in case the main starts multiple other |
| 117 | * objects there can be a single probe end point for the process. |
| 118 | */ |
| 119 | p := &probe.Probe{} |
| 120 | go p.ListenAndServe(config.ProbeEndPoint) |
| 121 | |
| 122 | /* |
| 123 | * Create a context which to start the services used by the applicaiton |
| 124 | * and attach the probe to that context |
| 125 | */ |
| 126 | ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p) |
| 127 | |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 128 | client, err := setLogConfig(ctx, config.KVStoreHost, config.KVStoreType, config.KVStorePort, config.KVStoreTimeout) |
| 129 | if err != nil { |
Girish Kumar | e9d7617 | 2020-03-20 20:26:04 +0000 | [diff] [blame] | 130 | logger.Warnw("unable-to-create-kvstore-client", log.Fields{"error": err}) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 131 | } |
| 132 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 133 | ofa, err := ofagent.NewOFAgent(&ofagent.OFAgent{ |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 134 | OFControllerEndPoints: config.OFControllerEndPoints, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 135 | VolthaApiEndPoint: config.VolthaApiEndPoint, |
| 136 | DeviceListRefreshInterval: config.DeviceListRefreshInterval, |
| 137 | ConnectionMaxRetries: config.ConnectionMaxRetries, |
| 138 | ConnectionRetryDelay: config.ConnectionRetryDelay, |
| 139 | }) |
| 140 | if err != nil { |
Girish Kumar | e9d7617 | 2020-03-20 20:26:04 +0000 | [diff] [blame] | 141 | logger.Fatalw("failed-to-create-ofagent", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 142 | log.Fields{ |
| 143 | "error": err}) |
| 144 | } |
| 145 | ofa.Run(ctx) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 146 | stop(ctx, client) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 147 | } |