blob: b0d107b6b125de92981cb7d35bccfa2216bcec81 [file] [log] [blame]
David K. Bainbridge157bdab2020-01-16 14:38:05 -08001/*
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
17package main
18
19import (
20 "context"
21 "flag"
22 "fmt"
23 "github.com/opencord/ofagent-go/internal/pkg/ofagent"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080024 "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. Bainbridge157bdab2020-01-16 14:38:05 -080027 "os"
28 "strings"
29)
30
31func printBanner() {
32 fmt.Println(` ___ _____ _ _ `)
33 fmt.Println(` / _ \| ___/ \ __ _ ___ _ __ | |_ `)
34 fmt.Println(` | | | | |_ / _ \ / _' |/ _ \ '_ \| __|`)
35 fmt.Println(` | |_| | _/ ___ \ (_| | __/ | | | |_ `)
36 fmt.Println(` \___/|_|/_/ \_\__, |\___|_| |_|\__|`)
37 fmt.Println(` |___/ `)
38}
39
40func printVersion() {
41 fmt.Println("OFAgent")
42 fmt.Println(version.VersionInfo.String(" "))
43}
44
David K. Bainbridge884de612020-02-19 14:14:38 -080045func 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. Bainbridge157bdab2020-01-16 14:38:05 -080052func 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. Bainbridgecac73ac2020-02-19 07:00:12 -080066 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. Bainbridge157bdab2020-01-16 14:38:05 -080075
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080076 }
77
78 var logLevel int
David K. Bainbridge157bdab2020-01-16 14:38:05 -080079 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. Bainbridgecac73ac2020-02-19 07:00:12 -080098 logLevel = log.WarnLevel
David K. Bainbridge157bdab2020-01-16 14:38:05 -080099 }
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800100 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. Bainbridge157bdab2020-01-16 14:38:05 -0800106 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}