blob: d395bb2abd6edd83dd4faec3ab1e9f1b79ed6dd8 [file] [log] [blame]
Prince Pereirac1c21d62021-04-22 08:38:15 +00001/*
2 * Copyright 2020-present Open Networking Foundation
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
17// Package config Common Logger initialization
18package config
19
20import (
21 "fmt"
22 "os"
23 "strconv"
24 "time"
25)
26
27// Constants defined for environment variables
28const (
29 envLogLevel = "LOG_LEVEL"
30 envMsgbusEndPoint = "MSGBUS_END_POINT"
31 envMsgbusRetryInterval = "MSGBUS_RETRY_INTERVAL"
32 envDbEndPoint = "DB_END_POINT"
33 envDbTimeout = "DB_TIMEOUT"
34 envGrpcEndPoint = "GRPC_END_POINT"
35 envGrpcRetryInterval = "GRPC_RETRY_INTERVAL"
36 envGrpcBackoffMaxDelay = "GRPC_BACKOFF_MAX_DELAY"
37 envGrpcMaxRetryCount = "GRPC_MAX_RETRY_COUNT"
38 envSecureConnection = "SECURE_GRPC"
39)
40
41// Default values defined if not provided through environment variables
42const (
43 defaultLogLevel = LogLevelDebug
44 defaultMsgbusEndPoint = "127.0.0.1:9092"
45 defaultMsgbusRetryInterval = 10 * time.Second
46 defaultDbEndPoint = "127.0.0.1:2379"
47 defaultDbTimeout = 5 * time.Second
48 defaultGrpcEndPoint = "0.0.0.0:9292"
49 defaultGrpcRetryInterval = 10 * time.Second
50 defaultGrpcBackoffMaxDelay = 30 * time.Second
51 defaultGrpcMaxRetryCount = 5
52 defaultGrpcHostName = "DMI"
53 defaultSecureConnection = false
54)
55
56// Constants defined for certiifcates
57const (
58 pathRootCaCrt = "/etc/Root_CA.crt"
59 pathServerCrt = "/etc/grpc_server.crt"
60 pathServerKey = "/etc/grpc_server.key"
61)
62
63// DB versions
64const (
65 DBVer1 = "v1"
66)
67
68// Constants defined for Db
69const (
70 KVStore = "etcd"
71 DBPrefix = "/OpenDevMgr/"
72 CurDBVer = DBVer1
73)
74
75// Constants defined for Msgbus Topic to receive messages
76const (
77 OpenDevMgrEventsTopic = "dm.events"
78 OpenDevMgrMetricsTopic = "dm.metrics"
79 OpenDevMgrEventsMsgType = "OPEN-DEV-MGR-EVENTS-MSG"
80)
81
82// Log Level Constants
83const (
84 LogLevelDebug = "DEBUG"
85 LogLevelInfo = "INFO"
86 LogLevelWarn = "WARN"
87 LogLevelError = "ERROR"
88)
89
90type correlationIdType int8
91
92// Constants defined for context management
93const (
94 RequestIdKey correlationIdType = iota
95 SessionIdKey
96)
97
98// ContextId constant used to print context id in logs
99const (
100 ContextId = "context-id"
101)
102
103var coreFlags *CoreFlags
104
105// NewCoreFlags creates a new CoreFlag object for you
106func NewCoreFlags() *CoreFlags {
107 if coreFlags != nil {
108 return coreFlags
109 }
110 coreFlags = new(CoreFlags)
111 coreFlags.LogLevel = defaultLogLevel
112 coreFlags.MsgbusEndPoint = defaultMsgbusEndPoint
113 coreFlags.MsgbusRetryInterval = defaultMsgbusRetryInterval
114 coreFlags.DbEndPoint = defaultDbEndPoint
115 coreFlags.DbTimeout = defaultDbTimeout
116 coreFlags.SecureConnection = defaultSecureConnection
117 coreFlags.GrpcFlags.GrpcEndPoint = defaultGrpcEndPoint
118 coreFlags.GrpcFlags.GrpcRetryInterval = defaultGrpcRetryInterval
119 coreFlags.GrpcFlags.GrpcBackoffMaxDelay = defaultGrpcBackoffMaxDelay
120 coreFlags.GrpcFlags.GrpcMaxRetryCount = defaultGrpcMaxRetryCount
121 coreFlags.GrpcFlags.GrpcHostName = defaultGrpcHostName
122 coreFlags.CertsPath.RootCaCrt = pathRootCaCrt
123 coreFlags.CertsPath.ServerCrt = pathServerCrt
124 coreFlags.CertsPath.ServerKey = pathServerKey
125 return coreFlags
126}
127
128// GetCoreFlags returns the entire config values
129func GetCoreFlags() *CoreFlags {
130 if coreFlags != nil {
131 return coreFlags
132 }
133 return nil
134}
135
136// GetGrpcFlags returns the grpc config values
137func GetGrpcFlags() *GrpcFlags {
138 if coreFlags != nil {
139 return &coreFlags.GrpcFlags
140 }
141 return nil
142}
143
144// GrpcFlags is struct defined to store grpc parameters
145type GrpcFlags struct {
146 GrpcEndPoint string
147 GrpcRetryInterval time.Duration
148 GrpcBackoffMaxDelay time.Duration
149 GrpcMaxRetryCount int
150 GrpcHostName string
151}
152
153// CertsPath is struct defined to store certificates path
154type CertsPath struct {
155 RootCaCrt string
156 ServerCrt string
157 ServerKey string
158}
159
160// CoreFlags is a structure defined to store all configurations
161type CoreFlags struct {
162 LogLevel string
163 MsgbusEndPoint string
164 MsgbusRetryInterval time.Duration
165 DbEndPoint string
166 DbTimeout time.Duration
167 SecureConnection bool
168 GrpcFlags
169 CertsPath
170}
171
172// ParseEnv method retrieves environment variables passed and replaces with
173// corresponding default variables stored in the CoreFlags object
174func (cf *CoreFlags) ParseEnv() {
175
176 if env := os.Getenv(envLogLevel); env != "" {
177 cf.LogLevel = env
178 }
179 fmt.Printf("Environment variable '%s' setting to : %s\n", envLogLevel, cf.LogLevel)
180
181 if env := os.Getenv(envMsgbusEndPoint); env != "" {
182 cf.MsgbusEndPoint = env
183 }
184 fmt.Printf("Environment variable '%s' setting to : %s\n", envMsgbusEndPoint, cf.MsgbusEndPoint)
185
186 if env := os.Getenv(envMsgbusRetryInterval); env != "" {
187 interval, err := strconv.Atoi(env)
188 if err == nil {
189 cf.MsgbusRetryInterval = time.Duration(interval) * time.Second
190 } else {
191 fmt.Printf("Invalid value '%s' passed for '%s'. Taking the default value.\n", env, envMsgbusRetryInterval)
192 }
193 }
194 fmt.Printf("Environment variable '%s' setting to : %s\n", envMsgbusRetryInterval, cf.MsgbusRetryInterval)
195
196 if env := os.Getenv(envDbEndPoint); env != "" {
197 cf.DbEndPoint = env
198 }
199 fmt.Printf("Environment variable '%s' setting to : %s\n", envDbEndPoint, cf.DbEndPoint)
200
201 if env := os.Getenv(envDbTimeout); env != "" {
202 interval, err := strconv.Atoi(env)
203 if err == nil {
204 cf.DbTimeout = time.Duration(interval) * time.Second
205 } else {
206 fmt.Printf("Invalid value '%s' passed for '%s'. Taking the default value.\n", env, envDbTimeout)
207 }
208 }
209 fmt.Printf("Environment variable '%s' setting to : %s\n", envDbTimeout, cf.DbTimeout)
210
211 if env := os.Getenv(envGrpcEndPoint); env != "" {
212 cf.GrpcFlags.GrpcEndPoint = env
213 }
214 fmt.Printf("Environment variable '%s' setting to : %s\n", envGrpcEndPoint, cf.GrpcFlags.GrpcEndPoint)
215
216 if env := os.Getenv(envGrpcRetryInterval); env != "" {
217 interval, err := strconv.Atoi(env)
218 if err == nil {
219 cf.GrpcFlags.GrpcRetryInterval = time.Duration(interval) * time.Second
220 } else {
221 fmt.Printf("Invalid value '%s' passed for '%s'. Taking the default value.\n", env, envGrpcRetryInterval)
222 }
223 }
224 fmt.Printf("Environment variable '%s' setting to : %s\n", envGrpcRetryInterval, cf.GrpcFlags.GrpcRetryInterval)
225
226 if env := os.Getenv(envGrpcBackoffMaxDelay); env != "" {
227 interval, err := strconv.Atoi(env)
228 if err == nil {
229 cf.GrpcFlags.GrpcBackoffMaxDelay = time.Duration(interval) * time.Second
230 } else {
231 fmt.Printf("Invalid value '%s' passed for '%s'. Taking the default value.\n", env, envGrpcBackoffMaxDelay)
232 }
233 }
234 fmt.Printf("Environment variable '%s' setting to : %s\n", envGrpcBackoffMaxDelay, cf.GrpcFlags.GrpcBackoffMaxDelay)
235
236 if env := os.Getenv(envGrpcMaxRetryCount); env != "" {
237 maxRetry, err := strconv.Atoi(env)
238 if err == nil {
239 cf.GrpcFlags.GrpcMaxRetryCount = maxRetry
240 } else {
241 fmt.Printf("Invalid value '%s' passed for '%s'. Taking the default value.\n", env, envGrpcMaxRetryCount)
242 }
243 }
244 fmt.Printf("Environment variable '%s' setting to : %v\n", envGrpcMaxRetryCount, cf.GrpcFlags.GrpcMaxRetryCount)
245
246 if env := os.Getenv(envSecureConnection); env != "" {
247 secureCon, err := strconv.ParseBool(env)
248 if err == nil {
249 cf.SecureConnection = secureCon
250 } else {
251 fmt.Printf("Invalid value '%s' passed for '%s'. Taking the default value.\n", env, envSecureConnection)
252 }
253 }
254 fmt.Printf("Environment variable '%s' setting to : %v\n", envSecureConnection, cf.SecureConnection)
255
256}