blob: 991a221084182a3014a5421e9a804124b6808317 [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
divyadesai81bb7ba2020-03-11 11:45:23 +000047func setLogConfig(ctx context.Context, kvStoreHost, kvStoreType string, kvStorePort, kvStoreTimeout int) (kvstore.Client, error) {
Rohan Agrawal00d3a412020-04-22 10:51:39 +000048 client, err := kvstore.NewEtcdClient(kvStoreHost+":"+strconv.Itoa(kvStorePort), kvStoreTimeout, log.WarnLevel)
divyadesai81bb7ba2020-03-11 11:45:23 +000049 if err != nil {
50 return nil, err
51 }
52
53 cm := conf.NewConfigManager(client, kvStoreType, kvStoreHost, kvStorePort, kvStoreTimeout)
divyadesai52dc0882020-03-19 06:38:11 +000054 go conf.StartLogLevelConfigProcessing(cm, ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +000055 return client, nil
56}
57
58func stop(ctx context.Context, kvClient kvstore.Client) {
59
60 // Cleanup - applies only if we had a kvClient
61 if kvClient != nil {
62 // Release all reservations
63 if err := kvClient.ReleaseAllReservations(ctx); err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +000064 logger.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +000065 }
66 // Close the DB connection
67 kvClient.Close()
68 }
69
70}
71
David K. Bainbridge157bdab2020-01-16 14:38:05 -080072func main() {
73
74 config, _ := parseCommandLineArguments()
75 flag.Parse()
76
77 if config.Version {
78 printVersion()
79 os.Exit(0)
80 }
81
82 if config.Banner {
83 printBanner()
84 }
85
divyadesai81bb7ba2020-03-11 11:45:23 +000086 // Setup logging
David K. Bainbridge157bdab2020-01-16 14:38:05 -080087
divyadesai81bb7ba2020-03-11 11:45:23 +000088 logLevel, err := log.StringToLogLevel(config.LogLevel)
89 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +000090 logger.Fatalf("Cannot setup logging, %s", err)
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080091 }
92
divyadesai81bb7ba2020-03-11 11:45:23 +000093 // Setup default logger - applies for packages that do not have specific logger set
94 if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": config.InstanceID}); err != nil {
95 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
David K. Bainbridge157bdab2020-01-16 14:38:05 -080096 }
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080097
divyadesai81bb7ba2020-03-11 11:45:23 +000098 // Update all loggers (provisionned via init) with a common field
99 if err := log.UpdateAllLoggers(log.Fields{"instanceId": config.InstanceID}); err != nil {
100 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800101 }
divyadesai81bb7ba2020-03-11 11:45:23 +0000102
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800103 log.SetAllLogLevel(logLevel)
104
divyadesai81bb7ba2020-03-11 11:45:23 +0000105 defer func() {
106 err := log.CleanUp()
107 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000108 logger.Errorw("unable-to-flush-any-buffered-log-entries", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000109 }
110 }()
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800111
112 /*
113 * Create and start the liveness and readiness container management probes. This
114 * is done in the main function so just in case the main starts multiple other
115 * objects there can be a single probe end point for the process.
116 */
117 p := &probe.Probe{}
118 go p.ListenAndServe(config.ProbeEndPoint)
119
120 /*
121 * Create a context which to start the services used by the applicaiton
122 * and attach the probe to that context
123 */
124 ctx := context.WithValue(context.Background(), probe.ProbeContextKey, p)
125
divyadesai81bb7ba2020-03-11 11:45:23 +0000126 client, err := setLogConfig(ctx, config.KVStoreHost, config.KVStoreType, config.KVStorePort, config.KVStoreTimeout)
127 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000128 logger.Warnw("unable-to-create-kvstore-client", log.Fields{"error": err})
divyadesai81bb7ba2020-03-11 11:45:23 +0000129 }
130
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800131 ofa, err := ofagent.NewOFAgent(&ofagent.OFAgent{
Jonathan Hart4b110f62020-03-13 17:36:19 -0700132 OFControllerEndPoints: config.OFControllerEndPoints,
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800133 VolthaApiEndPoint: config.VolthaApiEndPoint,
134 DeviceListRefreshInterval: config.DeviceListRefreshInterval,
135 ConnectionMaxRetries: config.ConnectionMaxRetries,
136 ConnectionRetryDelay: config.ConnectionRetryDelay,
137 })
138 if err != nil {
Girish Kumare9d76172020-03-20 20:26:04 +0000139 logger.Fatalw("failed-to-create-ofagent",
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800140 log.Fields{
141 "error": err})
142 }
143 ofa.Run(ctx)
divyadesai81bb7ba2020-03-11 11:45:23 +0000144 stop(ctx, client)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800145}