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" |
| 24 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
| 25 | "github.com/opencord/voltha-lib-go/v2/pkg/probe" |
| 26 | "github.com/opencord/voltha-lib-go/v2/pkg/version" |
| 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 | |
| 59 | log.SetLogLevel(log.DebugLevel) |
| 60 | log.SetDefaultLogger(log.JSON, log.DebugLevel, |
| 61 | log.Fields{ |
| 62 | "component": "ofagent", |
| 63 | }) |
| 64 | |
| 65 | logLevel := log.WarnLevel |
| 66 | switch strings.ToLower(config.LogLevel) { |
| 67 | case "fatal": |
| 68 | logLevel = log.FatalLevel |
| 69 | case "error": |
| 70 | logLevel = log.ErrorLevel |
| 71 | case "warn": |
| 72 | logLevel = log.WarnLevel |
| 73 | case "info": |
| 74 | logLevel = log.InfoLevel |
| 75 | case "debug": |
| 76 | logLevel = log.DebugLevel |
| 77 | default: |
| 78 | log.Warn("unrecognized-log-level", |
| 79 | log.Fields{ |
| 80 | "msg": "Unrecognized log level setting defaulting to 'WARN'", |
| 81 | "component": "ofagent", |
| 82 | "value": config.LogLevel, |
| 83 | }) |
| 84 | config.LogLevel = "WARN" |
| 85 | logLevel = log.FatalLevel |
| 86 | } |
| 87 | log.SetDefaultLogger(log.JSON, logLevel, |
| 88 | log.Fields{ |
| 89 | "component": "ofagent", |
| 90 | }) |
| 91 | log.SetAllLogLevel(logLevel) |
| 92 | |
| 93 | log.Infow("ofagent-startup-configuration", |
| 94 | log.Fields{ |
| 95 | "configuration": fmt.Sprintf("%+v", *config), |
| 96 | }) |
| 97 | |
| 98 | _, err := log.AddPackage(log.JSON, logLevel, nil) |
| 99 | if err != nil { |
| 100 | log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err}) |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Create and start the liveness and readiness container management probes. This |
| 105 | * is done in the main function so just in case the main starts multiple other |
| 106 | * objects there can be a single probe end point for the process. |
| 107 | */ |
| 108 | p := &probe.Probe{} |
| 109 | go p.ListenAndServe(config.ProbeEndPoint) |
| 110 | |
| 111 | /* |
| 112 | * Create a context which to start the services used by the applicaiton |
| 113 | * and attach the probe to that context |
| 114 | */ |
| 115 | ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p) |
| 116 | |
| 117 | ofa, err := ofagent.NewOFAgent(&ofagent.OFAgent{ |
| 118 | OFControllerEndPoint: config.OFControllerEndPoint, |
| 119 | VolthaApiEndPoint: config.VolthaApiEndPoint, |
| 120 | DeviceListRefreshInterval: config.DeviceListRefreshInterval, |
| 121 | ConnectionMaxRetries: config.ConnectionMaxRetries, |
| 122 | ConnectionRetryDelay: config.ConnectionRetryDelay, |
| 123 | }) |
| 124 | if err != nil { |
| 125 | log.Fatalw("failed-to-create-ofagent", |
| 126 | log.Fields{ |
| 127 | "error": err}) |
| 128 | } |
| 129 | ofa.Run(ctx) |
| 130 | } |