blob: 5ba2f3c11273075898f0e322aa16ef9caf62f48b [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoocfee5f42018-07-19 22:47:38 -040017package config
18
19import (
khenaidoocfee5f42018-07-19 22:47:38 -040020 "flag"
21 "fmt"
npujar1d86a522019-11-14 17:11:16 +053022
Girish Kumar4d3887d2019-11-22 14:22:05 +000023 "time"
npujar1d86a522019-11-14 17:11:16 +053024
25 "github.com/opencord/voltha-lib-go/v2/pkg/log"
khenaidoocfee5f42018-07-19 22:47:38 -040026)
27
khenaidoo5c11af72018-07-20 17:21:05 -040028// RW Core service default constants
khenaidoocfee5f42018-07-19 22:47:38 -040029const (
npujar1d86a522019-11-14 17:11:16 +053030 ConsulStoreName = "consul"
31 EtcdStoreName = "etcd"
32 defaultGrpcPort = 50057
33 defaultGrpcHost = ""
34 defaultKafkaAdapterHost = "127.0.0.1"
35 defaultKafkaAdapterPort = 9092
36 defaultKafkaClusterHost = "127.0.0.1"
37 defaultKafkaClusterPort = 9094
38 defaultKVStoreType = EtcdStoreName
39 defaultKVStoreTimeout = 5 //in seconds
40 defaultKVStoreHost = "127.0.0.1"
41 defaultKVStorePort = 2379 // Consul = 8500; Etcd = 2379
42 defaultKVTxnKeyDelTime = 60
43 defaultKVStoreDataPrefix = "service/voltha"
44 defaultLogLevel = 0
45 defaultBanner = false
46 defaultDisplayVersionOnly = false
47 defaultCoreTopic = "rwcore"
48 defaultRWCoreEndpoint = "rwcore"
49 defaultRWCoreKey = "pki/voltha.key"
50 defaultRWCoreCert = "pki/voltha.crt"
51 defaultRWCoreCA = "pki/voltha-CA.pem"
52 defaultAffinityRouterTopic = "affinityRouter"
53 defaultInCompetingMode = true
54 defaultLongRunningRequestTimeout = int64(2000)
55 defaultDefaultRequestTimeout = int64(500)
56 defaultCoreTimeout = int64(500)
57 defaultCoreBindingKey = "voltha_backend_name"
58 defaultCorePairTopic = "rwcore_1"
59 defaultMaxConnectionRetries = -1 // retries forever
60 defaultConnectionRetryInterval = 2 * time.Second
61 defaultLiveProbeInterval = 60 * time.Second
62 defaultNotLiveProbeInterval = 5 * time.Second // Probe more frequently when not alive
63 defaultProbeHost = ""
64 defaultProbePort = 8080
khenaidoocfee5f42018-07-19 22:47:38 -040065)
66
khenaidoo5c11af72018-07-20 17:21:05 -040067// RWCoreFlags represents the set of configurations used by the read-write core service
khenaidoocfee5f42018-07-19 22:47:38 -040068type RWCoreFlags struct {
69 // Command line parameters
khenaidoo2c6a0992019-04-29 13:46:56 -040070 RWCoreEndpoint string
71 GrpcHost string
72 GrpcPort int
73 KafkaAdapterHost string
74 KafkaAdapterPort int
75 KafkaClusterHost string
76 KafkaClusterPort int
77 KVStoreType string
78 KVStoreTimeout int // in seconds
79 KVStoreHost string
80 KVStorePort int
81 KVTxnKeyDelTime int
82 KVStoreDataPrefix string
83 CoreTopic string
84 LogLevel int
85 Banner bool
David K. Bainbridgef430cd52019-05-28 15:00:35 -070086 DisplayVersionOnly bool
khenaidoo2c6a0992019-04-29 13:46:56 -040087 RWCoreKey string
88 RWCoreCert string
89 RWCoreCA string
90 AffinityRouterTopic string
91 InCompetingMode bool
khenaidoob6080322019-01-29 21:47:38 -050092 LongRunningRequestTimeout int64
khenaidoo2c6a0992019-04-29 13:46:56 -040093 DefaultRequestTimeout int64
94 DefaultCoreTimeout int64
95 CoreBindingKey string
Kent Hagermana6d0c362019-07-30 12:50:21 -040096 CorePairTopic string
khenaidoob3244212019-08-27 14:32:27 -040097 MaxConnectionRetries int
Girish Kumar4d3887d2019-11-22 14:22:05 +000098 ConnectionRetryInterval time.Duration
99 LiveProbeInterval time.Duration
100 NotLiveProbeInterval time.Duration
Kent Hagermanc4618832019-10-07 12:24:36 -0400101 ProbeHost string
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700102 ProbePort int
khenaidoocfee5f42018-07-19 22:47:38 -0400103}
104
khenaidoob9203542018-09-17 22:56:37 -0400105func init() {
npujar1d86a522019-11-14 17:11:16 +0530106 _, err := log.AddPackage(log.JSON, log.WarnLevel, nil)
107 if err != nil {
108 log.Errorw("unable-to-register-a-package-to-the-log-map", log.Fields{"error": err})
109 }
khenaidoob9203542018-09-17 22:56:37 -0400110}
111
khenaidoo5c11af72018-07-20 17:21:05 -0400112// NewRWCoreFlags returns a new RWCore config
khenaidoocfee5f42018-07-19 22:47:38 -0400113func NewRWCoreFlags() *RWCoreFlags {
114 var rwCoreFlag = RWCoreFlags{ // Default values
npujar1d86a522019-11-14 17:11:16 +0530115 RWCoreEndpoint: defaultRWCoreEndpoint,
116 GrpcHost: defaultGrpcHost,
117 GrpcPort: defaultGrpcPort,
118 KafkaAdapterHost: defaultKafkaAdapterHost,
119 KafkaAdapterPort: defaultKafkaAdapterPort,
120 KafkaClusterHost: defaultKafkaClusterHost,
121 KafkaClusterPort: defaultKafkaClusterPort,
122 KVStoreType: defaultKVStoreType,
123 KVStoreTimeout: defaultKVStoreTimeout,
124 KVStoreHost: defaultKVStoreHost,
125 KVStorePort: defaultKVStorePort,
126 KVStoreDataPrefix: defaultKVStoreDataPrefix,
127 KVTxnKeyDelTime: defaultKVTxnKeyDelTime,
128 CoreTopic: defaultCoreTopic,
129 LogLevel: defaultLogLevel,
130 Banner: defaultBanner,
131 DisplayVersionOnly: defaultDisplayVersionOnly,
132 RWCoreKey: defaultRWCoreKey,
133 RWCoreCert: defaultRWCoreCert,
134 RWCoreCA: defaultRWCoreCA,
135 AffinityRouterTopic: defaultAffinityRouterTopic,
136 InCompetingMode: defaultInCompetingMode,
137 DefaultRequestTimeout: defaultDefaultRequestTimeout,
138 LongRunningRequestTimeout: defaultLongRunningRequestTimeout,
139 DefaultCoreTimeout: defaultCoreTimeout,
140 CoreBindingKey: defaultCoreBindingKey,
141 CorePairTopic: defaultCorePairTopic,
142 MaxConnectionRetries: defaultMaxConnectionRetries,
143 ConnectionRetryInterval: defaultConnectionRetryInterval,
144 LiveProbeInterval: defaultLiveProbeInterval,
145 NotLiveProbeInterval: defaultNotLiveProbeInterval,
146 ProbeHost: defaultProbeHost,
147 ProbePort: defaultProbePort,
khenaidoocfee5f42018-07-19 22:47:38 -0400148 }
149 return &rwCoreFlag
150}
151
khenaidoo5c11af72018-07-20 17:21:05 -0400152// ParseCommandArguments parses the arguments when running read-write core service
khenaidoocfee5f42018-07-19 22:47:38 -0400153func (cf *RWCoreFlags) ParseCommandArguments() {
khenaidoocfee5f42018-07-19 22:47:38 -0400154
npujar1d86a522019-11-14 17:11:16 +0530155 help := fmt.Sprintf("RW core endpoint address")
156 flag.StringVar(&(cf.RWCoreEndpoint), "vcore-endpoint", defaultRWCoreEndpoint, help)
khenaidoocfee5f42018-07-19 22:47:38 -0400157
khenaidoo5c11af72018-07-20 17:21:05 -0400158 help = fmt.Sprintf("GRPC server - host")
npujar1d86a522019-11-14 17:11:16 +0530159 flag.StringVar(&(cf.GrpcHost), "grpc_host", defaultGrpcHost, help)
khenaidoocfee5f42018-07-19 22:47:38 -0400160
khenaidoo5c11af72018-07-20 17:21:05 -0400161 help = fmt.Sprintf("GRPC server - port")
npujar1d86a522019-11-14 17:11:16 +0530162 flag.IntVar(&(cf.GrpcPort), "grpc_port", defaultGrpcPort, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400163
164 help = fmt.Sprintf("Kafka - Adapter messaging host")
npujar1d86a522019-11-14 17:11:16 +0530165 flag.StringVar(&(cf.KafkaAdapterHost), "kafka_adapter_host", defaultKafkaAdapterHost, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400166
167 help = fmt.Sprintf("Kafka - Adapter messaging port")
npujar1d86a522019-11-14 17:11:16 +0530168 flag.IntVar(&(cf.KafkaAdapterPort), "kafka_adapter_port", defaultKafkaAdapterPort, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400169
170 help = fmt.Sprintf("Kafka - Cluster messaging host")
npujar1d86a522019-11-14 17:11:16 +0530171 flag.StringVar(&(cf.KafkaClusterHost), "kafka_cluster_host", defaultKafkaClusterHost, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400172
173 help = fmt.Sprintf("Kafka - Cluster messaging port")
npujar1d86a522019-11-14 17:11:16 +0530174 flag.IntVar(&(cf.KafkaClusterPort), "kafka_cluster_port", defaultKafkaClusterPort, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400175
176 help = fmt.Sprintf("RW Core topic")
npujar1d86a522019-11-14 17:11:16 +0530177 flag.StringVar(&(cf.CoreTopic), "rw_core_topic", defaultCoreTopic, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400178
khenaidoo79232702018-12-04 11:00:41 -0500179 help = fmt.Sprintf("Affinity Router topic")
npujar1d86a522019-11-14 17:11:16 +0530180 flag.StringVar(&(cf.AffinityRouterTopic), "affinity_router_topic", defaultAffinityRouterTopic, help)
khenaidoo9cdc1a62019-01-24 21:57:40 -0500181
182 help = fmt.Sprintf("In competing Mode - two cores competing to handle a transaction ")
npujar1d86a522019-11-14 17:11:16 +0530183 flag.BoolVar(&cf.InCompetingMode, "in_competing_mode", defaultInCompetingMode, help)
khenaidoo79232702018-12-04 11:00:41 -0500184
khenaidoo5c11af72018-07-20 17:21:05 -0400185 help = fmt.Sprintf("KV store type")
npujar1d86a522019-11-14 17:11:16 +0530186 flag.StringVar(&(cf.KVStoreType), "kv_store_type", defaultKVStoreType, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400187
188 help = fmt.Sprintf("The default timeout when making a kv store request")
npujar1d86a522019-11-14 17:11:16 +0530189 flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", defaultKVStoreTimeout, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400190
191 help = fmt.Sprintf("KV store host")
npujar1d86a522019-11-14 17:11:16 +0530192 flag.StringVar(&(cf.KVStoreHost), "kv_store_host", defaultKVStoreHost, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400193
194 help = fmt.Sprintf("KV store port")
npujar1d86a522019-11-14 17:11:16 +0530195 flag.IntVar(&(cf.KVStorePort), "kv_store_port", defaultKVStorePort, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400196
Richard Jankowskie4d77662018-10-17 13:53:21 -0400197 help = fmt.Sprintf("The time to wait before deleting a completed transaction key")
npujar1d86a522019-11-14 17:11:16 +0530198 flag.IntVar(&(cf.KVTxnKeyDelTime), "kv_txn_delete_time", defaultKVTxnKeyDelTime, help)
Richard Jankowskie4d77662018-10-17 13:53:21 -0400199
khenaidoo9cdc1a62019-01-24 21:57:40 -0500200 help = fmt.Sprintf("KV store data prefix")
npujar1d86a522019-11-14 17:11:16 +0530201 flag.StringVar(&(cf.KVStoreDataPrefix), "kv_store_data_prefix", defaultKVStoreDataPrefix, help)
khenaidoo9cdc1a62019-01-24 21:57:40 -0500202
khenaidoo5c11af72018-07-20 17:21:05 -0400203 help = fmt.Sprintf("Log level")
npujar1d86a522019-11-14 17:11:16 +0530204 flag.IntVar(&(cf.LogLevel), "log_level", defaultLogLevel, help)
khenaidoo5c11af72018-07-20 17:21:05 -0400205
khenaidoob6080322019-01-29 21:47:38 -0500206 help = fmt.Sprintf("Timeout for long running request")
npujar1d86a522019-11-14 17:11:16 +0530207 flag.Int64Var(&(cf.LongRunningRequestTimeout), "timeout_long_request", defaultLongRunningRequestTimeout, help)
khenaidoob6080322019-01-29 21:47:38 -0500208
209 help = fmt.Sprintf("Default timeout for regular request")
npujar1d86a522019-11-14 17:11:16 +0530210 flag.Int64Var(&(cf.DefaultRequestTimeout), "timeout_request", defaultDefaultRequestTimeout, help)
khenaidoob6080322019-01-29 21:47:38 -0500211
khenaidoo2c6a0992019-04-29 13:46:56 -0400212 help = fmt.Sprintf("Default Core timeout")
npujar1d86a522019-11-14 17:11:16 +0530213 flag.Int64Var(&(cf.DefaultCoreTimeout), "core_timeout", defaultCoreTimeout, help)
khenaidoo2c6a0992019-04-29 13:46:56 -0400214
khenaidoo5c11af72018-07-20 17:21:05 -0400215 help = fmt.Sprintf("Show startup banner log lines")
npujar1d86a522019-11-14 17:11:16 +0530216 flag.BoolVar(&cf.Banner, "banner", defaultBanner, help)
khenaidoocfee5f42018-07-19 22:47:38 -0400217
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700218 help = fmt.Sprintf("Show version information and exit")
npujar1d86a522019-11-14 17:11:16 +0530219 flag.BoolVar(&cf.DisplayVersionOnly, "version", defaultDisplayVersionOnly, help)
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700220
Richard Jankowski46464e92019-03-05 11:53:55 -0500221 help = fmt.Sprintf("The name of the meta-key whose value is the rw-core group to which the ofagent is bound")
npujar1d86a522019-11-14 17:11:16 +0530222 flag.StringVar(&(cf.CoreBindingKey), "core_binding_key", defaultCoreBindingKey, help)
Richard Jankowski46464e92019-03-05 11:53:55 -0500223
Kent Hagermana6d0c362019-07-30 12:50:21 -0400224 help = fmt.Sprintf("Core pairing group topic")
npujar1d86a522019-11-14 17:11:16 +0530225 flag.StringVar(&cf.CorePairTopic, "core_pair_topic", defaultCorePairTopic, help)
Kent Hagermana6d0c362019-07-30 12:50:21 -0400226
khenaidoob3244212019-08-27 14:32:27 -0400227 help = fmt.Sprintf("The number of retries to connect to a dependent component")
npujar1d86a522019-11-14 17:11:16 +0530228 flag.IntVar(&(cf.MaxConnectionRetries), "max_connection_retries", defaultMaxConnectionRetries, help)
khenaidoob3244212019-08-27 14:32:27 -0400229
Scott Bakeree6a0872019-10-29 15:59:52 -0700230 help = fmt.Sprintf("The number of seconds between each connection retry attempt")
npujar1d86a522019-11-14 17:11:16 +0530231 flag.DurationVar(&(cf.ConnectionRetryInterval), "connection_retry_interval", defaultConnectionRetryInterval, help)
khenaidoob3244212019-08-27 14:32:27 -0400232
Scott Bakeree6a0872019-10-29 15:59:52 -0700233 help = fmt.Sprintf("The number of seconds between liveness probes while in a live state")
npujar1d86a522019-11-14 17:11:16 +0530234 flag.DurationVar(&(cf.LiveProbeInterval), "live_probe_interval", defaultLiveProbeInterval, help)
Scott Bakeree6a0872019-10-29 15:59:52 -0700235
236 help = fmt.Sprintf("The number of seconds between liveness probes while in a not live state")
npujar1d86a522019-11-14 17:11:16 +0530237 flag.DurationVar(&(cf.NotLiveProbeInterval), "not_live_probe_interval", defaultNotLiveProbeInterval, help)
Scott Bakeree6a0872019-10-29 15:59:52 -0700238
Kent Hagermanc4618832019-10-07 12:24:36 -0400239 help = fmt.Sprintf("The host on which to listen to answer liveness and readiness probe queries over HTTP.")
npujar1d86a522019-11-14 17:11:16 +0530240 flag.StringVar(&(cf.ProbeHost), "probe_host", defaultProbeHost, help)
Kent Hagermanc4618832019-10-07 12:24:36 -0400241
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700242 help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.")
npujar1d86a522019-11-14 17:11:16 +0530243 flag.IntVar(&(cf.ProbePort), "probe_port", defaultProbePort, help)
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700244
khenaidoocfee5f42018-07-19 22:47:38 -0400245 flag.Parse()
khenaidoocfee5f42018-07-19 22:47:38 -0400246}