blob: 50841308b9c1484deb6bd03a689b6e20a8e3c7c5 [file] [log] [blame]
Stephane Barbariea75791c2019-01-24 10:58:06 -05001/*
2 * Copyright 2018-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 */
npujar03b018e2019-11-13 15:29:36 +053016
Stephane Barbariea75791c2019-01-24 10:58:06 -050017package config
18
19import (
20 "flag"
21 "fmt"
Stephane Barbariea75791c2019-01-24 10:58:06 -050022 "os"
Girish Kumar91482642019-11-08 11:38:03 +000023 "time"
npujar03b018e2019-11-13 15:29:36 +053024
25 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Stephane Barbariea75791c2019-01-24 10:58:06 -050026)
27
28// RO Core service default constants
29const (
npujar03b018e2019-11-13 15:29:36 +053030 ConsulStoreName = "consul"
31 EtcdStoreName = "etcd"
32 defaultInstanceID = "rocore001"
33 defaultGrpcPort = 50057
34 defaultGrpcHost = ""
35 defaultKVStoreType = EtcdStoreName
36 defaultKVStoreTimeout = 5 //in seconds
37 defaultKVStoreHost = "127.0.0.1"
38 defaultKVStorePort = 2379 // Consul = 8500; Etcd = 2379
39 defaultKVTxnKeyDelTime = 60
40 defaultLogLevel = 0
41 defaultBanner = false
42 defaultDisplayVersionOnly = false
43 defaultCoreTopic = "rocore"
44 defaultROCoreEndpoint = "rocore"
45 defaultROCoreKey = "pki/voltha.key"
46 defaultROCoreCert = "pki/voltha.crt"
47 defaultROCoreCA = "pki/voltha-CA.pem"
48 defaultAffinityRouterTopic = "affinityRouter"
49 defaultProbeHost = ""
50 defaultProbePort = 8080
51 defaultLiveProbeInterval = 60 * time.Second
52 defaultNotLiveProbeInterval = 5 * time.Second // Probe more frequently to detect Recovery early
53 defaultCoreTimeout = 59 * time.Second
54 defaultMaxConnectionRetries = -1 // retries forever
55 defaultConnectionRetryInterval = 2 * time.Second // in seconds
Stephane Barbariea75791c2019-01-24 10:58:06 -050056)
57
58// ROCoreFlags represents the set of configurations used by the read-only core service
59type ROCoreFlags struct {
60 // Command line parameters
Divya Desai660dbba2019-10-16 07:06:49 +000061 InstanceID string
62 ROCoreEndpoint string
63 GrpcHost string
64 GrpcPort int
65 KVStoreType string
66 KVStoreTimeout int // in seconds
67 KVStoreHost string
68 KVStorePort int
69 KVTxnKeyDelTime int
70 CoreTopic string
71 LogLevel int
72 Banner bool
73 DisplayVersionOnly bool
74 ROCoreKey string
75 ROCoreCert string
76 ROCoreCA string
77 AffinityRouterTopic string
78 ProbeHost string
79 ProbePort int
80 LiveProbeInterval time.Duration
81 NotLiveProbeInterval time.Duration
82 CoreTimeout time.Duration
83 MaxConnectionRetries int
84 ConnectionRetryInterval time.Duration
Stephane Barbariea75791c2019-01-24 10:58:06 -050085}
86
87func init() {
npujar03b018e2019-11-13 15:29:36 +053088 _, err := log.AddPackage(log.JSON, log.WarnLevel, nil)
89 if err != nil {
90 log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err})
91 }
Stephane Barbariea75791c2019-01-24 10:58:06 -050092}
93
94// NewROCoreFlags returns a new ROCore config
95func NewROCoreFlags() *ROCoreFlags {
96 var roCoreFlag = ROCoreFlags{ // Default values
npujar03b018e2019-11-13 15:29:36 +053097 InstanceID: defaultInstanceID,
98 ROCoreEndpoint: defaultROCoreEndpoint,
99 GrpcHost: defaultGrpcHost,
100 GrpcPort: defaultGrpcPort,
101 KVStoreType: defaultKVStoreType,
102 KVStoreTimeout: defaultKVStoreTimeout,
103 KVStoreHost: defaultKVStoreHost,
104 KVStorePort: defaultKVStorePort,
105 KVTxnKeyDelTime: defaultKVTxnKeyDelTime,
106 CoreTopic: defaultCoreTopic,
107 LogLevel: defaultLogLevel,
108 Banner: defaultBanner,
109 DisplayVersionOnly: defaultDisplayVersionOnly,
110 ROCoreKey: defaultROCoreKey,
111 ROCoreCert: defaultROCoreCert,
112 ROCoreCA: defaultROCoreCA,
113 AffinityRouterTopic: defaultAffinityRouterTopic,
114 ProbeHost: defaultProbeHost,
115 ProbePort: defaultProbePort,
116 LiveProbeInterval: defaultLiveProbeInterval,
117 NotLiveProbeInterval: defaultNotLiveProbeInterval,
118 CoreTimeout: defaultCoreTimeout,
119 MaxConnectionRetries: defaultMaxConnectionRetries,
120 ConnectionRetryInterval: defaultConnectionRetryInterval,
Stephane Barbariea75791c2019-01-24 10:58:06 -0500121 }
122 return &roCoreFlag
123}
124
125// ParseCommandArguments parses the arguments when running read-only core service
126func (cf *ROCoreFlags) ParseCommandArguments() {
127
npujar03b018e2019-11-13 15:29:36 +0530128 help := fmt.Sprintf("RO core endpoint address")
129 flag.StringVar(&(cf.ROCoreEndpoint), "vcore-endpoint", defaultROCoreEndpoint, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500130
131 help = fmt.Sprintf("GRPC server - host")
npujar03b018e2019-11-13 15:29:36 +0530132 flag.StringVar(&(cf.GrpcHost), "grpc_host", defaultGrpcHost, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500133
134 help = fmt.Sprintf("GRPC server - port")
npujar03b018e2019-11-13 15:29:36 +0530135 flag.IntVar(&(cf.GrpcPort), "grpc_port", defaultGrpcPort, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500136
137 help = fmt.Sprintf("RO Core topic")
npujar03b018e2019-11-13 15:29:36 +0530138 flag.StringVar(&(cf.CoreTopic), "ro_core_topic", defaultCoreTopic, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500139
140 help = fmt.Sprintf("Affinity Router topic")
npujar03b018e2019-11-13 15:29:36 +0530141 flag.StringVar(&(cf.AffinityRouterTopic), "affinity_router_topic", defaultAffinityRouterTopic, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500142
143 help = fmt.Sprintf("KV store type")
npujar03b018e2019-11-13 15:29:36 +0530144 flag.StringVar(&(cf.KVStoreType), "kv_store_type", defaultKVStoreType, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500145
146 help = fmt.Sprintf("The default timeout when making a kv store request")
npujar03b018e2019-11-13 15:29:36 +0530147 flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", defaultKVStoreTimeout, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500148
149 help = fmt.Sprintf("KV store host")
npujar03b018e2019-11-13 15:29:36 +0530150 flag.StringVar(&(cf.KVStoreHost), "kv_store_host", defaultKVStoreHost, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500151
152 help = fmt.Sprintf("KV store port")
npujar03b018e2019-11-13 15:29:36 +0530153 flag.IntVar(&(cf.KVStorePort), "kv_store_port", defaultKVStorePort, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500154
155 help = fmt.Sprintf("The time to wait before deleting a completed transaction key")
npujar03b018e2019-11-13 15:29:36 +0530156 flag.IntVar(&(cf.KVTxnKeyDelTime), "kv_txn_delete_time", defaultKVTxnKeyDelTime, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500157
158 help = fmt.Sprintf("Log level")
npujar03b018e2019-11-13 15:29:36 +0530159 flag.IntVar(&(cf.LogLevel), "log_level", defaultLogLevel, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500160
161 help = fmt.Sprintf("Show startup banner log lines")
npujar03b018e2019-11-13 15:29:36 +0530162 flag.BoolVar(&cf.Banner, "banner", defaultBanner, help)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500163
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700164 help = fmt.Sprintf("Show version information and exit")
npujar03b018e2019-11-13 15:29:36 +0530165 flag.BoolVar(&cf.DisplayVersionOnly, "version", defaultDisplayVersionOnly, help)
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700166
Kent Hagermanc4618832019-10-07 12:24:36 -0400167 help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.")
npujar03b018e2019-11-13 15:29:36 +0530168 flag.StringVar(&(cf.ProbeHost), "probe_host", defaultProbeHost, help)
Kent Hagermanc4618832019-10-07 12:24:36 -0400169
Hardik Windlassdc63dde2019-09-30 07:15:13 +0000170 help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.")
npujar03b018e2019-11-13 15:29:36 +0530171 flag.IntVar(&(cf.ProbePort), "probe_port", defaultProbePort, help)
Hardik Windlassdc63dde2019-09-30 07:15:13 +0000172
Girish Kumar91482642019-11-08 11:38:03 +0000173 help = fmt.Sprintf("Time interval between liveness probes while in a live state")
npujar03b018e2019-11-13 15:29:36 +0530174 flag.DurationVar(&(cf.LiveProbeInterval), "live_probe_interval", defaultLiveProbeInterval, help)
Girish Kumar91482642019-11-08 11:38:03 +0000175
176 help = fmt.Sprintf("Time interval between liveness probes while in a not live state")
npujar03b018e2019-11-13 15:29:36 +0530177 flag.DurationVar(&(cf.NotLiveProbeInterval), "not_live_probe_interval", defaultNotLiveProbeInterval, help)
Girish Kumar91482642019-11-08 11:38:03 +0000178
Divya Desai660dbba2019-10-16 07:06:49 +0000179 help = fmt.Sprintf("The maximum time the core will wait while attempting to connect to a dependent component duration")
npujar03b018e2019-11-13 15:29:36 +0530180 flag.DurationVar(&(cf.CoreTimeout), "core_timeout", defaultCoreTimeout, help)
Divya Desai660dbba2019-10-16 07:06:49 +0000181
182 help = fmt.Sprintf("The number of retries to connect to a dependent component")
npujar03b018e2019-11-13 15:29:36 +0530183 flag.IntVar(&(cf.MaxConnectionRetries), "max_connection_retries", defaultMaxConnectionRetries, help)
Divya Desai660dbba2019-10-16 07:06:49 +0000184
185 help = fmt.Sprintf("The duration between each connection retry attempt ")
npujar03b018e2019-11-13 15:29:36 +0530186 flag.DurationVar(&(cf.ConnectionRetryInterval), "connection_retry_interval", defaultConnectionRetryInterval, help)
Divya Desai660dbba2019-10-16 07:06:49 +0000187
Stephane Barbariea75791c2019-01-24 10:58:06 -0500188 flag.Parse()
189
190 containerName := getContainerInfo()
191 if len(containerName) > 0 {
192 cf.InstanceID = containerName
193 }
194
195}
196
197func getContainerInfo() string {
198 return os.Getenv("HOSTNAME")
199}