blob: d32fecc42a9554ccc70d132b2781026d1013fde1 [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"
Neha Sharma87d43d72020-04-08 14:10:40 +000031 "time"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080032)
33
34func printBanner() {
35 fmt.Println(` ___ _____ _ _ `)
36 fmt.Println(` / _ \| ___/ \ __ _ ___ _ __ | |_ `)
37 fmt.Println(` | | | | |_ / _ \ / _' |/ _ \ '_ \| __|`)
38 fmt.Println(` | |_| | _/ ___ \ (_| | __/ | | | |_ `)
39 fmt.Println(` \___/|_|/_/ \_\__, |\___|_| |_|\__|`)
40 fmt.Println(` |___/ `)
41}
42
43func printVersion() {
44 fmt.Println("OFAgent")
45 fmt.Println(version.VersionInfo.String(" "))
46}
47
Neha Sharma87d43d72020-04-08 14:10:40 +000048func setLogConfig(ctx context.Context, kvStoreHost, kvStoreType string, kvStorePort int, kvStoreTimeout time.Duration) (kvstore.Client, error) {
Rohan Agrawal00d3a412020-04-22 10:51:39 +000049 client, err := kvstore.NewEtcdClient(kvStoreHost+":"+strconv.Itoa(kvStorePort), kvStoreTimeout, log.WarnLevel)
Neha Sharma87d43d72020-04-08 14:10:40 +000050
divyadesai81bb7ba2020-03-11 11:45:23 +000051 if err != nil {
52 return nil, err
53 }
54
55 cm := conf.NewConfigManager(client, kvStoreType, kvStoreHost, kvStorePort, kvStoreTimeout)
divyadesai52dc0882020-03-19 06:38:11 +000056 go conf.StartLogLevelConfigProcessing(cm, ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +000057 return client, nil
58}
59
60func stop(ctx context.Context, kvClient kvstore.Client) {
61
62 // Cleanup - applies only if we had a kvClient
63 if kvClient != nil {
64 // Release all reservations
65 if err := kvClient.ReleaseAllReservations(ctx); err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +000066 logger.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +000067 }
68 // Close the DB connection
69 kvClient.Close()
70 }
71
72}
73
David K. Bainbridge157bdab2020-01-16 14:38:05 -080074func main() {
75
76 config, _ := parseCommandLineArguments()
77 flag.Parse()
78
79 if config.Version {
80 printVersion()
81 os.Exit(0)
82 }
83
84 if config.Banner {
85 printBanner()
86 }
87
divyadesai81bb7ba2020-03-11 11:45:23 +000088 // Setup logging
David K. Bainbridge157bdab2020-01-16 14:38:05 -080089
divyadesai81bb7ba2020-03-11 11:45:23 +000090 logLevel, err := log.StringToLogLevel(config.LogLevel)
91 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +000092 logger.Fatalf("Cannot setup logging, %s", err)
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080093 }
94
divyadesai81bb7ba2020-03-11 11:45:23 +000095 // Setup default logger - applies for packages that do not have specific logger set
96 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": config.InstanceID}); err != nil {
97 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
David K. Bainbridge157bdab2020-01-16 14:38:05 -080098 }
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080099
divyadesai81bb7ba2020-03-11 11:45:23 +0000100 // Update all loggers (provisionned via init) with a common field
101 if err := log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil {
102 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800103 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000104
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800105 log.SetAllLogLevel(logLevel)
106
Matteo Scandoloa98f0dc2020-05-13 10:39:29 -0700107 // depending on the build tags start the profiler
108 realMain()
109
divyadesai81bb7ba2020-03-11 11:45:23 +0000110 defer func() {
111 err := log.CleanUp()
112 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000113 logger.Errorw("unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000114 }
115 }()
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800116
117 /*
118 * Create and start the liveness and readiness container management probes. This
119 * is done in the main function so just in case the main starts multiple other
120 * objects there can be a single probe end point for the process.
121 */
122 p := &probe.Probe{}
123 go p.ListenAndServe(config.ProbeEndPoint)
124
125 /*
126 * Create a context which to start the services used by the applicaiton
127 * and attach the probe to that context
128 */
129 ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p)
130
divyadesai81bb7ba2020-03-11 11:45:23 +0000131 client, err := setLogConfig(ctx, config.KVStoreHost, config.KVStoreType, config.KVStorePort, config.KVStoreTimeout)
132 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000133 logger.Warnw("unable-to-create-kvstore-client", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000134 }
135
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800136 ofa, err := ofagent.NewOFAgent(&ofagent.OFAgent{
Jonathan Hart4b110f62020-03-13 17:36:19 -0700137 OFControllerEndPoints: config.OFControllerEndPoints,
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800138 VolthaApiEndPoint: config.VolthaApiEndPoint,
139 DeviceListRefreshInterval: config.DeviceListRefreshInterval,
140 ConnectionMaxRetries: config.ConnectionMaxRetries,
141 ConnectionRetryDelay: config.ConnectionRetryDelay,
142 })
143 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000144 logger.Fatalw("failed-to-create-ofagent",
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800145 log.Fields{
146 "error": err})
147 }
148 ofa.Run(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +0000149 stop(ctx, client)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800150}