khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 16 | package config |
| 17 | |
| 18 | import ( |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 19 | "flag" |
| 20 | "fmt" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 22 | "time" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 23 | ) |
| 24 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 25 | // RW Core service default constants |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 26 | const ( |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 27 | ConsulStoreName = "consul" |
| 28 | EtcdStoreName = "etcd" |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 29 | default_GrpcPort = 50057 |
| 30 | default_GrpcHost = "" |
| 31 | default_KafkaAdapterHost = "127.0.0.1" |
| 32 | default_KafkaAdapterPort = 9092 |
| 33 | default_KafkaClusterHost = "127.0.0.1" |
| 34 | default_KafkaClusterPort = 9094 |
| 35 | default_KVStoreType = EtcdStoreName |
| 36 | default_KVStoreTimeout = 5 //in seconds |
| 37 | default_KVStoreHost = "127.0.0.1" |
| 38 | default_KVStorePort = 2379 // Consul = 8500; Etcd = 2379 |
| 39 | default_KVTxnKeyDelTime = 60 |
| 40 | default_KVStoreDataPrefix = "service/voltha" |
| 41 | default_LogLevel = 0 |
| 42 | default_Banner = false |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 43 | default_DisplayVersionOnly = false |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 44 | default_CoreTopic = "rwcore" |
| 45 | default_RWCoreEndpoint = "rwcore" |
| 46 | default_RWCoreKey = "pki/voltha.key" |
| 47 | default_RWCoreCert = "pki/voltha.crt" |
| 48 | default_RWCoreCA = "pki/voltha-CA.pem" |
| 49 | default_AffinityRouterTopic = "affinityRouter" |
| 50 | default_InCompetingMode = true |
khenaidoo | b608032 | 2019-01-29 21:47:38 -0500 | [diff] [blame] | 51 | default_LongRunningRequestTimeout = int64(2000) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 52 | default_DefaultRequestTimeout = int64(500) |
| 53 | default_CoreTimeout = int64(500) |
| 54 | default_CoreBindingKey = "voltha_backend_name" |
Kent Hagerman | a6d0c36 | 2019-07-30 12:50:21 -0400 | [diff] [blame] | 55 | default_CorePairTopic = "rwcore_1" |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 56 | default_MaxConnectionRetries = -1 // retries forever |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 57 | default_ConnectionRetryInterval = 2 * time.Second |
| 58 | default_LiveProbeInterval = 60 * time.Second |
| 59 | default_NotLiveProbeInterval = 5 * time.Second // Probe more frequently when not alive |
Kent Hagerman | c461883 | 2019-10-07 12:24:36 -0400 | [diff] [blame] | 60 | default_ProbeHost = "" |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 61 | default_ProbePort = 8080 |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 62 | ) |
| 63 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 64 | // RWCoreFlags represents the set of configurations used by the read-write core service |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 65 | type RWCoreFlags struct { |
| 66 | // Command line parameters |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 67 | RWCoreEndpoint string |
| 68 | GrpcHost string |
| 69 | GrpcPort int |
| 70 | KafkaAdapterHost string |
| 71 | KafkaAdapterPort int |
| 72 | KafkaClusterHost string |
| 73 | KafkaClusterPort int |
| 74 | KVStoreType string |
| 75 | KVStoreTimeout int // in seconds |
| 76 | KVStoreHost string |
| 77 | KVStorePort int |
| 78 | KVTxnKeyDelTime int |
| 79 | KVStoreDataPrefix string |
| 80 | CoreTopic string |
| 81 | LogLevel int |
| 82 | Banner bool |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 83 | DisplayVersionOnly bool |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 84 | RWCoreKey string |
| 85 | RWCoreCert string |
| 86 | RWCoreCA string |
| 87 | AffinityRouterTopic string |
| 88 | InCompetingMode bool |
khenaidoo | b608032 | 2019-01-29 21:47:38 -0500 | [diff] [blame] | 89 | LongRunningRequestTimeout int64 |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 90 | DefaultRequestTimeout int64 |
| 91 | DefaultCoreTimeout int64 |
| 92 | CoreBindingKey string |
Kent Hagerman | a6d0c36 | 2019-07-30 12:50:21 -0400 | [diff] [blame] | 93 | CorePairTopic string |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 94 | MaxConnectionRetries int |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 95 | ConnectionRetryInterval time.Duration |
| 96 | LiveProbeInterval time.Duration |
| 97 | NotLiveProbeInterval time.Duration |
Kent Hagerman | c461883 | 2019-10-07 12:24:36 -0400 | [diff] [blame] | 98 | ProbeHost string |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 99 | ProbePort int |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 100 | } |
| 101 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 102 | func init() { |
| 103 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 104 | } |
| 105 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 106 | // NewRWCoreFlags returns a new RWCore config |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 107 | func NewRWCoreFlags() *RWCoreFlags { |
| 108 | var rwCoreFlag = RWCoreFlags{ // Default values |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 109 | RWCoreEndpoint: default_RWCoreEndpoint, |
| 110 | GrpcHost: default_GrpcHost, |
| 111 | GrpcPort: default_GrpcPort, |
| 112 | KafkaAdapterHost: default_KafkaAdapterHost, |
| 113 | KafkaAdapterPort: default_KafkaAdapterPort, |
| 114 | KafkaClusterHost: default_KafkaClusterHost, |
| 115 | KafkaClusterPort: default_KafkaClusterPort, |
| 116 | KVStoreType: default_KVStoreType, |
| 117 | KVStoreTimeout: default_KVStoreTimeout, |
| 118 | KVStoreHost: default_KVStoreHost, |
| 119 | KVStorePort: default_KVStorePort, |
| 120 | KVStoreDataPrefix: default_KVStoreDataPrefix, |
| 121 | KVTxnKeyDelTime: default_KVTxnKeyDelTime, |
| 122 | CoreTopic: default_CoreTopic, |
| 123 | LogLevel: default_LogLevel, |
| 124 | Banner: default_Banner, |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 125 | DisplayVersionOnly: default_DisplayVersionOnly, |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 126 | RWCoreKey: default_RWCoreKey, |
| 127 | RWCoreCert: default_RWCoreCert, |
| 128 | RWCoreCA: default_RWCoreCA, |
| 129 | AffinityRouterTopic: default_AffinityRouterTopic, |
| 130 | InCompetingMode: default_InCompetingMode, |
| 131 | DefaultRequestTimeout: default_DefaultRequestTimeout, |
| 132 | LongRunningRequestTimeout: default_LongRunningRequestTimeout, |
| 133 | DefaultCoreTimeout: default_CoreTimeout, |
| 134 | CoreBindingKey: default_CoreBindingKey, |
Kent Hagerman | a6d0c36 | 2019-07-30 12:50:21 -0400 | [diff] [blame] | 135 | CorePairTopic: default_CorePairTopic, |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 136 | MaxConnectionRetries: default_MaxConnectionRetries, |
| 137 | ConnectionRetryInterval: default_ConnectionRetryInterval, |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 138 | LiveProbeInterval: default_LiveProbeInterval, |
| 139 | NotLiveProbeInterval: default_NotLiveProbeInterval, |
Kent Hagerman | c461883 | 2019-10-07 12:24:36 -0400 | [diff] [blame] | 140 | ProbeHost: default_ProbeHost, |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 141 | ProbePort: default_ProbePort, |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 142 | } |
| 143 | return &rwCoreFlag |
| 144 | } |
| 145 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 146 | // ParseCommandArguments parses the arguments when running read-write core service |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 147 | func (cf *RWCoreFlags) ParseCommandArguments() { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 148 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 149 | var help string |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 150 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 151 | help = fmt.Sprintf("RW core endpoint address") |
| 152 | flag.StringVar(&(cf.RWCoreEndpoint), "vcore-endpoint", default_RWCoreEndpoint, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 153 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 154 | help = fmt.Sprintf("GRPC server - host") |
| 155 | flag.StringVar(&(cf.GrpcHost), "grpc_host", default_GrpcHost, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 156 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 157 | help = fmt.Sprintf("GRPC server - port") |
| 158 | flag.IntVar(&(cf.GrpcPort), "grpc_port", default_GrpcPort, help) |
| 159 | |
| 160 | help = fmt.Sprintf("Kafka - Adapter messaging host") |
| 161 | flag.StringVar(&(cf.KafkaAdapterHost), "kafka_adapter_host", default_KafkaAdapterHost, help) |
| 162 | |
| 163 | help = fmt.Sprintf("Kafka - Adapter messaging port") |
| 164 | flag.IntVar(&(cf.KafkaAdapterPort), "kafka_adapter_port", default_KafkaAdapterPort, help) |
| 165 | |
| 166 | help = fmt.Sprintf("Kafka - Cluster messaging host") |
| 167 | flag.StringVar(&(cf.KafkaClusterHost), "kafka_cluster_host", default_KafkaClusterHost, help) |
| 168 | |
| 169 | help = fmt.Sprintf("Kafka - Cluster messaging port") |
| 170 | flag.IntVar(&(cf.KafkaClusterPort), "kafka_cluster_port", default_KafkaClusterPort, help) |
| 171 | |
| 172 | help = fmt.Sprintf("RW Core topic") |
| 173 | flag.StringVar(&(cf.CoreTopic), "rw_core_topic", default_CoreTopic, help) |
| 174 | |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 175 | help = fmt.Sprintf("Affinity Router topic") |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 176 | flag.StringVar(&(cf.AffinityRouterTopic), "affinity_router_topic", default_AffinityRouterTopic, help) |
| 177 | |
| 178 | help = fmt.Sprintf("In competing Mode - two cores competing to handle a transaction ") |
| 179 | flag.BoolVar(&cf.InCompetingMode, "in_competing_mode", default_InCompetingMode, help) |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 180 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 181 | help = fmt.Sprintf("KV store type") |
| 182 | flag.StringVar(&(cf.KVStoreType), "kv_store_type", default_KVStoreType, help) |
| 183 | |
| 184 | help = fmt.Sprintf("The default timeout when making a kv store request") |
| 185 | flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", default_KVStoreTimeout, help) |
| 186 | |
| 187 | help = fmt.Sprintf("KV store host") |
| 188 | flag.StringVar(&(cf.KVStoreHost), "kv_store_host", default_KVStoreHost, help) |
| 189 | |
| 190 | help = fmt.Sprintf("KV store port") |
| 191 | flag.IntVar(&(cf.KVStorePort), "kv_store_port", default_KVStorePort, help) |
| 192 | |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 193 | help = fmt.Sprintf("The time to wait before deleting a completed transaction key") |
| 194 | flag.IntVar(&(cf.KVTxnKeyDelTime), "kv_txn_delete_time", default_KVTxnKeyDelTime, help) |
| 195 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 196 | help = fmt.Sprintf("KV store data prefix") |
| 197 | flag.StringVar(&(cf.KVStoreDataPrefix), "kv_store_data_prefix", default_KVStoreDataPrefix, help) |
| 198 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 199 | help = fmt.Sprintf("Log level") |
| 200 | flag.IntVar(&(cf.LogLevel), "log_level", default_LogLevel, help) |
| 201 | |
khenaidoo | b608032 | 2019-01-29 21:47:38 -0500 | [diff] [blame] | 202 | help = fmt.Sprintf("Timeout for long running request") |
| 203 | flag.Int64Var(&(cf.LongRunningRequestTimeout), "timeout_long_request", default_LongRunningRequestTimeout, help) |
| 204 | |
| 205 | help = fmt.Sprintf("Default timeout for regular request") |
| 206 | flag.Int64Var(&(cf.DefaultRequestTimeout), "timeout_request", default_DefaultRequestTimeout, help) |
| 207 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 208 | help = fmt.Sprintf("Default Core timeout") |
| 209 | flag.Int64Var(&(cf.DefaultCoreTimeout), "core_timeout", default_CoreTimeout, help) |
| 210 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 211 | help = fmt.Sprintf("Show startup banner log lines") |
| 212 | flag.BoolVar(&cf.Banner, "banner", default_Banner, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 213 | |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 214 | help = fmt.Sprintf("Show version information and exit") |
| 215 | flag.BoolVar(&cf.DisplayVersionOnly, "version", default_DisplayVersionOnly, help) |
| 216 | |
Richard Jankowski | 46464e9 | 2019-03-05 11:53:55 -0500 | [diff] [blame] | 217 | help = fmt.Sprintf("The name of the meta-key whose value is the rw-core group to which the ofagent is bound") |
| 218 | flag.StringVar(&(cf.CoreBindingKey), "core_binding_key", default_CoreBindingKey, help) |
| 219 | |
Kent Hagerman | a6d0c36 | 2019-07-30 12:50:21 -0400 | [diff] [blame] | 220 | help = fmt.Sprintf("Core pairing group topic") |
| 221 | flag.StringVar(&cf.CorePairTopic, "core_pair_topic", default_CorePairTopic, help) |
| 222 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 223 | help = fmt.Sprintf("The number of retries to connect to a dependent component") |
| 224 | flag.IntVar(&(cf.MaxConnectionRetries), "max_connection_retries", default_MaxConnectionRetries, help) |
| 225 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 226 | help = fmt.Sprintf("The number of seconds between each connection retry attempt") |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 227 | flag.DurationVar(&(cf.ConnectionRetryInterval), "connection_retry_interval", default_ConnectionRetryInterval, help) |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 228 | |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 229 | help = fmt.Sprintf("The number of seconds between liveness probes while in a live state") |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 230 | flag.DurationVar(&(cf.LiveProbeInterval), "live_probe_interval", default_LiveProbeInterval, help) |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 231 | |
| 232 | help = fmt.Sprintf("The number of seconds between liveness probes while in a not live state") |
Girish Kumar | 4d3887d | 2019-11-22 14:22:05 +0000 | [diff] [blame] | 233 | flag.DurationVar(&(cf.NotLiveProbeInterval), "not_live_probe_interval", default_NotLiveProbeInterval, help) |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 234 | |
Kent Hagerman | c461883 | 2019-10-07 12:24:36 -0400 | [diff] [blame] | 235 | help = fmt.Sprintf("The host on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 236 | flag.StringVar(&(cf.ProbeHost), "probe_host", default_ProbeHost, help) |
| 237 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 238 | help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 239 | flag.IntVar(&(cf.ProbePort), "probe_port", default_ProbePort, help) |
| 240 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 241 | flag.Parse() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 242 | } |