blob: 987a8e6c47019642fc0b5bad4ad2f8dba856c63a [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
divyadesai81bb7ba2020-03-11 11:45:23 +0000107 defer func() {
108 err := log.CleanUp()
109 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000110 logger.Errorw("unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000111 }
112 }()
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800113
114 /*
115 * Create and start the liveness and readiness container management probes. This
116 * is done in the main function so just in case the main starts multiple other
117 * objects there can be a single probe end point for the process.
118 */
119 p := &probe.Probe{}
120 go p.ListenAndServe(config.ProbeEndPoint)
121
122 /*
123 * Create a context which to start the services used by the applicaiton
124 * and attach the probe to that context
125 */
126 ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p)
127
divyadesai81bb7ba2020-03-11 11:45:23 +0000128 client, err := setLogConfig(ctx, config.KVStoreHost, config.KVStoreType, config.KVStorePort, config.KVStoreTimeout)
129 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000130 logger.Warnw("unable-to-create-kvstore-client", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000131 }
132
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800133 ofa, err := ofagent.NewOFAgent(&ofagent.OFAgent{
Jonathan Hart4b110f62020-03-13 17:36:19 -0700134 OFControllerEndPoints: config.OFControllerEndPoints,
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800135 VolthaApiEndPoint: config.VolthaApiEndPoint,
136 DeviceListRefreshInterval: config.DeviceListRefreshInterval,
137 ConnectionMaxRetries: config.ConnectionMaxRetries,
138 ConnectionRetryDelay: config.ConnectionRetryDelay,
139 })
140 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000141 logger.Fatalw("failed-to-create-ofagent",
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800142 log.Fields{
143 "error": err})
144 }
145 ofa.Run(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +0000146 stop(ctx, client)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800147}