blob: 14b38ed193c8ba6f74aa7f6ed9d9a9dfce1bd2e9 [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"
Neha Sharma87d43d72020-04-08 14:10:40 +000030 "time"
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
Neha Sharma318a1292020-05-14 19:53:26 +000047func setLogConfig(ctx context.Context, kvStoreAddress, kvStoreType string, kvStoreTimeout time.Duration) (kvstore.Client, error) {
48 client, err := kvstore.NewEtcdClient(kvStoreAddress, kvStoreTimeout, log.WarnLevel)
Neha Sharma87d43d72020-04-08 14:10:40 +000049
divyadesai81bb7ba2020-03-11 11:45:23 +000050 if err != nil {
51 return nil, err
52 }
53
Neha Sharma318a1292020-05-14 19:53:26 +000054 cm := conf.NewConfigManager(client, kvStoreType, kvStoreAddress, kvStoreTimeout)
divyadesai52dc0882020-03-19 06:38:11 +000055 go conf.StartLogLevelConfigProcessing(cm, ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +000056 return client, nil
57}
58
59func stop(ctx context.Context, kvClient kvstore.Client) {
60
61 // Cleanup - applies only if we had a kvClient
62 if kvClient != nil {
63 // Release all reservations
64 if err := kvClient.ReleaseAllReservations(ctx); err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +000065 logger.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +000066 }
67 // Close the DB connection
68 kvClient.Close()
69 }
70
71}
72
David K. Bainbridge157bdab2020-01-16 14:38:05 -080073func main() {
74
75 config, _ := parseCommandLineArguments()
76 flag.Parse()
77
78 if config.Version {
79 printVersion()
80 os.Exit(0)
81 }
82
83 if config.Banner {
84 printBanner()
85 }
86
divyadesai81bb7ba2020-03-11 11:45:23 +000087 // Setup logging
David K. Bainbridge157bdab2020-01-16 14:38:05 -080088
divyadesai81bb7ba2020-03-11 11:45:23 +000089 logLevel, err := log.StringToLogLevel(config.LogLevel)
90 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +000091 logger.Fatalf("Cannot setup logging, %s", err)
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080092 }
93
divyadesai81bb7ba2020-03-11 11:45:23 +000094 // Setup default logger - applies for packages that do not have specific logger set
95 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": config.InstanceID}); err != nil {
96 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
David K. Bainbridge157bdab2020-01-16 14:38:05 -080097 }
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080098
divyadesai81bb7ba2020-03-11 11:45:23 +000099 // Update all loggers (provisionned via init) with a common field
100 if err := log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil {
101 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800102 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000103
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800104 log.SetAllLogLevel(logLevel)
105
Matteo Scandoloa98f0dc2020-05-13 10:39:29 -0700106 // depending on the build tags start the profiler
107 realMain()
108
divyadesai81bb7ba2020-03-11 11:45:23 +0000109 defer func() {
110 err := log.CleanUp()
111 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000112 logger.Errorw("unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000113 }
114 }()
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800115
116 /*
117 * Create and start the liveness and readiness container management probes. This
118 * is done in the main function so just in case the main starts multiple other
119 * objects there can be a single probe end point for the process.
120 */
121 p := &probe.Probe{}
122 go p.ListenAndServe(config.ProbeEndPoint)
123
124 /*
125 * Create a context which to start the services used by the applicaiton
126 * and attach the probe to that context
127 */
128 ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p)
129
Neha Sharma318a1292020-05-14 19:53:26 +0000130 client, err := setLogConfig(ctx, config.KVStoreAddress, config.KVStoreType, config.KVStoreTimeout)
divyadesai81bb7ba2020-03-11 11:45:23 +0000131 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000132 logger.Warnw("unable-to-create-kvstore-client", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000133 }
134
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800135 ofa, err := ofagent.NewOFAgent(&ofagent.OFAgent{
Jonathan Hart4b110f62020-03-13 17:36:19 -0700136 OFControllerEndPoints: config.OFControllerEndPoints,
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800137 VolthaApiEndPoint: config.VolthaApiEndPoint,
138 DeviceListRefreshInterval: config.DeviceListRefreshInterval,
139 ConnectionMaxRetries: config.ConnectionMaxRetries,
140 ConnectionRetryDelay: config.ConnectionRetryDelay,
141 })
142 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000143 logger.Fatalw("failed-to-create-ofagent",
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800144 log.Fields{
145 "error": err})
146 }
147 ofa.Run(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +0000148 stop(ctx, client)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800149}