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" |
David K. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 25 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
| 26 | "github.com/opencord/voltha-lib-go/v3/pkg/version" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 27 | "os" |
| 28 | "strings" |
| 29 | ) |
| 30 | |
| 31 | func printBanner() { |
| 32 | fmt.Println(` ___ _____ _ _ `) |
| 33 | fmt.Println(` / _ \| ___/ \ __ _ ___ _ __ | |_ `) |
| 34 | fmt.Println(` | | | | |_ / _ \ / _' |/ _ \ '_ \| __|`) |
| 35 | fmt.Println(` | |_| | _/ ___ \ (_| | __/ | | | |_ `) |
| 36 | fmt.Println(` \___/|_|/_/ \_\__, |\___|_| |_|\__|`) |
| 37 | fmt.Println(` |___/ `) |
| 38 | } |
| 39 | |
| 40 | func printVersion() { |
| 41 | fmt.Println("OFAgent") |
| 42 | fmt.Println(version.VersionInfo.String(" ")) |
| 43 | } |
| 44 | |
David K. Bainbridge | 884de61 | 2020-02-19 14:14:38 -0800 | [diff] [blame] | 45 | func init() { |
| 46 | _, err := log.AddPackage(log.JSON, log.DebugLevel, nil) |
| 47 | if err != nil { |
| 48 | log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err}) |
| 49 | } |
| 50 | } |
| 51 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 52 | func main() { |
| 53 | |
| 54 | config, _ := parseCommandLineArguments() |
| 55 | flag.Parse() |
| 56 | |
| 57 | if config.Version { |
| 58 | printVersion() |
| 59 | os.Exit(0) |
| 60 | } |
| 61 | |
| 62 | if config.Banner { |
| 63 | printBanner() |
| 64 | } |
| 65 | |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 66 | if err := log.SetLogLevel(log.DebugLevel); err != nil { |
| 67 | log.Errorw("set-log-level", log.Fields{ |
| 68 | "level": log.DebugLevel, |
| 69 | "error": err}) |
| 70 | } |
| 71 | if _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"component": "ofagent"}); err != nil { |
| 72 | log.Errorw("set-default-log-level", log.Fields{ |
| 73 | "level": log.DebugLevel, |
| 74 | "error": err}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 75 | |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | var logLevel int |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 79 | switch strings.ToLower(config.LogLevel) { |
| 80 | case "fatal": |
| 81 | logLevel = log.FatalLevel |
| 82 | case "error": |
| 83 | logLevel = log.ErrorLevel |
| 84 | case "warn": |
| 85 | logLevel = log.WarnLevel |
| 86 | case "info": |
| 87 | logLevel = log.InfoLevel |
| 88 | case "debug": |
| 89 | logLevel = log.DebugLevel |
| 90 | default: |
| 91 | log.Warn("unrecognized-log-level", |
| 92 | log.Fields{ |
| 93 | "msg": "Unrecognized log level setting defaulting to 'WARN'", |
| 94 | "component": "ofagent", |
| 95 | "value": config.LogLevel, |
| 96 | }) |
| 97 | config.LogLevel = "WARN" |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 98 | logLevel = log.WarnLevel |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 99 | } |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 100 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"component": "ofagent"}); err != nil { |
| 101 | log.Errorw("set-default-log-level", log.Fields{ |
| 102 | "level": logLevel, |
| 103 | "error": err}) |
| 104 | |
| 105 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 106 | log.SetAllLogLevel(logLevel) |
| 107 | |
| 108 | log.Infow("ofagent-startup-configuration", |
| 109 | log.Fields{ |
| 110 | "configuration": fmt.Sprintf("%+v", *config), |
| 111 | }) |
| 112 | |
| 113 | _, err := log.AddPackage(log.JSON, logLevel, nil) |
| 114 | if err != nil { |
| 115 | log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err}) |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Create and start the liveness and readiness container management probes. This |
| 120 | * is done in the main function so just in case the main starts multiple other |
| 121 | * objects there can be a single probe end point for the process. |
| 122 | */ |
| 123 | p := &probe.Probe{} |
| 124 | go p.ListenAndServe(config.ProbeEndPoint) |
| 125 | |
| 126 | /* |
| 127 | * Create a context which to start the services used by the applicaiton |
| 128 | * and attach the probe to that context |
| 129 | */ |
| 130 | ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p) |
| 131 | |
| 132 | ofa, err := ofagent.NewOFAgent(&ofagent.OFAgent{ |
| 133 | OFControllerEndPoint: config.OFControllerEndPoint, |
| 134 | VolthaApiEndPoint: config.VolthaApiEndPoint, |
| 135 | DeviceListRefreshInterval: config.DeviceListRefreshInterval, |
| 136 | ConnectionMaxRetries: config.ConnectionMaxRetries, |
| 137 | ConnectionRetryDelay: config.ConnectionRetryDelay, |
| 138 | }) |
| 139 | if err != nil { |
| 140 | log.Fatalw("failed-to-create-ofagent", |
| 141 | log.Fields{ |
| 142 | "error": err}) |
| 143 | } |
| 144 | ofa.Run(ctx) |
| 145 | } |