blob: 2949eaac4fbf6d10b4c1815ea40ee219fc2fabe6 [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"
divyadesai81bb7ba2020-03-11 11:45:23 +000024 conf "github.com/opencord/voltha-lib-go/v3/pkg/config"
25 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080026 "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. Bainbridge157bdab2020-01-16 14:38:05 -080029 "os"
divyadesai81bb7ba2020-03-11 11:45:23 +000030 "strconv"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080031)
32
33func printBanner() {
34 fmt.Println(` ___ _____ _ _ `)
35 fmt.Println(` / _ \| ___/ \ __ _ ___ _ __ | |_ `)
36 fmt.Println(` | | | | |_ / _ \ / _' |/ _ \ '_ \| __|`)
37 fmt.Println(` | |_| | _/ ___ \ (_| | __/ | | | |_ `)
38 fmt.Println(` \___/|_|/_/ \_\__, |\___|_| |_|\__|`)
39 fmt.Println(` |___/ `)
40}
41
42func printVersion() {
43 fmt.Println("OFAgent")
44 fmt.Println(version.VersionInfo.String(" "))
45}
46
David K. Bainbridge884de612020-02-19 14:14:38 -080047func init() {
48 _, err := log.AddPackage(log.JSON, log.DebugLevel, nil)
49 if err != nil {
50 log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err})
51 }
52}
53
divyadesai81bb7ba2020-03-11 11:45:23 +000054func setLogConfig(ctx context.Context, kvStoreHost, kvStoreType string, kvStorePort, kvStoreTimeout int) (kvstore.Client, error) {
55 client, err := kvstore.NewEtcdClient(kvStoreHost+":"+strconv.Itoa(kvStorePort), kvStoreTimeout)
56 if err != nil {
57 return nil, err
58 }
59
60 cm := conf.NewConfigManager(client, kvStoreType, kvStoreHost, kvStorePort, kvStoreTimeout)
divyadesai52dc0882020-03-19 06:38:11 +000061 go conf.StartLogLevelConfigProcessing(cm, ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +000062 return client, nil
63}
64
65func stop(ctx context.Context, kvClient kvstore.Client) {
66
67 // Cleanup - applies only if we had a kvClient
68 if kvClient != nil {
69 // Release all reservations
70 if err := kvClient.ReleaseAllReservations(ctx); err != nil {
71 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
72 }
73 // Close the DB connection
74 kvClient.Close()
75 }
76
77}
78
David K. Bainbridge157bdab2020-01-16 14:38:05 -080079func main() {
80
81 config, _ := parseCommandLineArguments()
82 flag.Parse()
83
84 if config.Version {
85 printVersion()
86 os.Exit(0)
87 }
88
89 if config.Banner {
90 printBanner()
91 }
92
divyadesai81bb7ba2020-03-11 11:45:23 +000093 // Setup logging
David K. Bainbridge157bdab2020-01-16 14:38:05 -080094
divyadesai81bb7ba2020-03-11 11:45:23 +000095 logLevel, err := log.StringToLogLevel(config.LogLevel)
96 if err != nil {
97 log.Fatalf("Cannot setup logging, %s", err)
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080098 }
99
divyadesai81bb7ba2020-03-11 11:45:23 +0000100 // Setup default logger - applies for packages that do not have specific logger set
101 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": config.InstanceID}); err != nil {
102 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800103 }
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800104
divyadesai81bb7ba2020-03-11 11:45:23 +0000105 // Update all loggers (provisionned via init) with a common field
106 if err := log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil {
107 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800108 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000109
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800110 log.SetAllLogLevel(logLevel)
111
divyadesai81bb7ba2020-03-11 11:45:23 +0000112 defer func() {
113 err := log.CleanUp()
114 if err != nil {
115 log.Errorw("unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
116 }
117 }()
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800118
119 /*
120 * Create and start the liveness and readiness container management probes. This
121 * is done in the main function so just in case the main starts multiple other
122 * objects there can be a single probe end point for the process.
123 */
124 p := &probe.Probe{}
125 go p.ListenAndServe(config.ProbeEndPoint)
126
127 /*
128 * Create a context which to start the services used by the applicaiton
129 * and attach the probe to that context
130 */
131 ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p)
132
divyadesai81bb7ba2020-03-11 11:45:23 +0000133 client, err := setLogConfig(ctx, config.KVStoreHost, config.KVStoreType, config.KVStorePort, config.KVStoreTimeout)
134 if err != nil {
135 log.Warnw("unable-to-create-kvstore-client", log.Fields{"error": err})
136 }
137
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800138 ofa, err := ofagent.NewOFAgent(&ofagent.OFAgent{
139 OFControllerEndPoint: config.OFControllerEndPoint,
140 VolthaApiEndPoint: config.VolthaApiEndPoint,
141 DeviceListRefreshInterval: config.DeviceListRefreshInterval,
142 ConnectionMaxRetries: config.ConnectionMaxRetries,
143 ConnectionRetryDelay: config.ConnectionRetryDelay,
144 })
145 if err != nil {
146 log.Fatalw("failed-to-create-ofagent",
147 log.Fields{
148 "error": err})
149 }
150 ofa.Run(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +0000151 stop(ctx, client)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800152}