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