blob: a282b9bd9b17c058fff49d202cc014d3511efdae [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 */
khenaidoocfee5f42018-07-19 22:47:38 -040016package config
17
18import (
khenaidoocfee5f42018-07-19 22:47:38 -040019 "flag"
20 "fmt"
khenaidoob9203542018-09-17 22:56:37 -040021 "github.com/opencord/voltha-go/common/log"
khenaidoocfee5f42018-07-19 22:47:38 -040022 "os"
khenaidoocfee5f42018-07-19 22:47:38 -040023)
24
khenaidoo5c11af72018-07-20 17:21:05 -040025// RW Core service default constants
khenaidoocfee5f42018-07-19 22:47:38 -040026const (
khenaidoo2c6a0992019-04-29 13:46:56 -040027 ConsulStoreName = "consul"
28 EtcdStoreName = "etcd"
29 default_InstanceID = "rwcore001"
30 default_GrpcPort = 50057
31 default_GrpcHost = ""
32 default_KafkaAdapterHost = "127.0.0.1"
33 default_KafkaAdapterPort = 9092
34 default_KafkaClusterHost = "127.0.0.1"
35 default_KafkaClusterPort = 9094
36 default_KVStoreType = EtcdStoreName
37 default_KVStoreTimeout = 5 //in seconds
38 default_KVStoreHost = "127.0.0.1"
39 default_KVStorePort = 2379 // Consul = 8500; Etcd = 2379
40 default_KVTxnKeyDelTime = 60
41 default_KVStoreDataPrefix = "service/voltha"
42 default_LogLevel = 0
43 default_Banner = false
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
khenaidoob6080322019-01-29 21:47:38 -050051 default_LongRunningRequestTimeout = int64(2000)
khenaidoo2c6a0992019-04-29 13:46:56 -040052 default_DefaultRequestTimeout = int64(500)
53 default_CoreTimeout = int64(500)
54 default_CoreBindingKey = "voltha_backend_name"
khenaidoocfee5f42018-07-19 22:47:38 -040055)
56
khenaidoo5c11af72018-07-20 17:21:05 -040057// RWCoreFlags represents the set of configurations used by the read-write core service
khenaidoocfee5f42018-07-19 22:47:38 -040058type RWCoreFlags struct {
59 // Command line parameters
khenaidoo2c6a0992019-04-29 13:46:56 -040060 InstanceID string
61 RWCoreEndpoint string
62 GrpcHost string
63 GrpcPort int
64 KafkaAdapterHost string
65 KafkaAdapterPort int
66 KafkaClusterHost string
67 KafkaClusterPort int
68 KVStoreType string
69 KVStoreTimeout int // in seconds
70 KVStoreHost string
71 KVStorePort int
72 KVTxnKeyDelTime int
73 KVStoreDataPrefix string
74 CoreTopic string
75 LogLevel int
76 Banner bool
77 RWCoreKey string
78 RWCoreCert string
79 RWCoreCA string
80 AffinityRouterTopic string
81 InCompetingMode bool
khenaidoob6080322019-01-29 21:47:38 -050082 LongRunningRequestTimeout int64
khenaidoo2c6a0992019-04-29 13:46:56 -040083 DefaultRequestTimeout int64
84 DefaultCoreTimeout int64
85 CoreBindingKey string
khenaidoocfee5f42018-07-19 22:47:38 -040086}
87
khenaidoob9203542018-09-17 22:56:37 -040088func init() {
89 log.AddPackage(log.JSON, log.WarnLevel, nil)
90}
91
khenaidoo5c11af72018-07-20 17:21:05 -040092// NewRWCoreFlags returns a new RWCore config
khenaidoocfee5f42018-07-19 22:47:38 -040093func NewRWCoreFlags() *RWCoreFlags {
94 var rwCoreFlag = RWCoreFlags{ // Default values
khenaidoo2c6a0992019-04-29 13:46:56 -040095 InstanceID: default_InstanceID,
96 RWCoreEndpoint: default_RWCoreEndpoint,
97 GrpcHost: default_GrpcHost,
98 GrpcPort: default_GrpcPort,
99 KafkaAdapterHost: default_KafkaAdapterHost,
100 KafkaAdapterPort: default_KafkaAdapterPort,
101 KafkaClusterHost: default_KafkaClusterHost,
102 KafkaClusterPort: default_KafkaClusterPort,
103 KVStoreType: default_KVStoreType,
104 KVStoreTimeout: default_KVStoreTimeout,
105 KVStoreHost: default_KVStoreHost,
106 KVStorePort: default_KVStorePort,
107 KVStoreDataPrefix: default_KVStoreDataPrefix,
108 KVTxnKeyDelTime: default_KVTxnKeyDelTime,
109 CoreTopic: default_CoreTopic,
110 LogLevel: default_LogLevel,
111 Banner: default_Banner,
112 RWCoreKey: default_RWCoreKey,
113 RWCoreCert: default_RWCoreCert,
114 RWCoreCA: default_RWCoreCA,
115 AffinityRouterTopic: default_AffinityRouterTopic,
116 InCompetingMode: default_InCompetingMode,
117 DefaultRequestTimeout: default_DefaultRequestTimeout,
118 LongRunningRequestTimeout: default_LongRunningRequestTimeout,
119 DefaultCoreTimeout: default_CoreTimeout,
120 CoreBindingKey: default_CoreBindingKey,
khenaidoocfee5f42018-07-19 22:47:38 -0400121 }
122 return &rwCoreFlag
123}
124
khenaidoo5c11af72018-07-20 17:21:05 -0400125// ParseCommandArguments parses the arguments when running read-write core service
khenaidoocfee5f42018-07-19 22:47:38 -0400126func (cf *RWCoreFlags) ParseCommandArguments() {
khenaidoocfee5f42018-07-19 22:47:38 -0400127
khenaidoo5c11af72018-07-20 17:21:05 -0400128 var help string
khenaidoocfee5f42018-07-19 22:47:38 -0400129
Stephane Barbariec53a2752019-03-08 17:50:10 -0500130 help = fmt.Sprintf("RW instance id")
131 flag.StringVar(&(cf.InstanceID), "instance-id", default_InstanceID, help)
132
khenaidoo5c11af72018-07-20 17:21:05 -0400133 help = fmt.Sprintf("RW core endpoint address")
134 flag.StringVar(&(cf.RWCoreEndpoint), "vcore-endpoint", default_RWCoreEndpoint, help)
khenaidoocfee5f42018-07-19 22:47:38 -0400135
khenaidoo5c11af72018-07-20 17:21:05 -0400136 help = fmt.Sprintf("GRPC server - host")
137 flag.StringVar(&(cf.GrpcHost), "grpc_host", default_GrpcHost, help)
khenaidoocfee5f42018-07-19 22:47:38 -0400138
khenaidoo5c11af72018-07-20 17:21:05 -0400139 help = fmt.Sprintf("GRPC server - port")
140 flag.IntVar(&(cf.GrpcPort), "grpc_port", default_GrpcPort, help)
141
142 help = fmt.Sprintf("Kafka - Adapter messaging host")
143 flag.StringVar(&(cf.KafkaAdapterHost), "kafka_adapter_host", default_KafkaAdapterHost, help)
144
145 help = fmt.Sprintf("Kafka - Adapter messaging port")
146 flag.IntVar(&(cf.KafkaAdapterPort), "kafka_adapter_port", default_KafkaAdapterPort, help)
147
148 help = fmt.Sprintf("Kafka - Cluster messaging host")
149 flag.StringVar(&(cf.KafkaClusterHost), "kafka_cluster_host", default_KafkaClusterHost, help)
150
151 help = fmt.Sprintf("Kafka - Cluster messaging port")
152 flag.IntVar(&(cf.KafkaClusterPort), "kafka_cluster_port", default_KafkaClusterPort, help)
153
154 help = fmt.Sprintf("RW Core topic")
155 flag.StringVar(&(cf.CoreTopic), "rw_core_topic", default_CoreTopic, help)
156
khenaidoo79232702018-12-04 11:00:41 -0500157 help = fmt.Sprintf("Affinity Router topic")
khenaidoo9cdc1a62019-01-24 21:57:40 -0500158 flag.StringVar(&(cf.AffinityRouterTopic), "affinity_router_topic", default_AffinityRouterTopic, help)
159
160 help = fmt.Sprintf("In competing Mode - two cores competing to handle a transaction ")
161 flag.BoolVar(&cf.InCompetingMode, "in_competing_mode", default_InCompetingMode, help)
khenaidoo79232702018-12-04 11:00:41 -0500162
khenaidoo5c11af72018-07-20 17:21:05 -0400163 help = fmt.Sprintf("KV store type")
164 flag.StringVar(&(cf.KVStoreType), "kv_store_type", default_KVStoreType, help)
165
166 help = fmt.Sprintf("The default timeout when making a kv store request")
167 flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", default_KVStoreTimeout, help)
168
169 help = fmt.Sprintf("KV store host")
170 flag.StringVar(&(cf.KVStoreHost), "kv_store_host", default_KVStoreHost, help)
171
172 help = fmt.Sprintf("KV store port")
173 flag.IntVar(&(cf.KVStorePort), "kv_store_port", default_KVStorePort, help)
174
Richard Jankowskie4d77662018-10-17 13:53:21 -0400175 help = fmt.Sprintf("The time to wait before deleting a completed transaction key")
176 flag.IntVar(&(cf.KVTxnKeyDelTime), "kv_txn_delete_time", default_KVTxnKeyDelTime, help)
177
khenaidoo9cdc1a62019-01-24 21:57:40 -0500178 help = fmt.Sprintf("KV store data prefix")
179 flag.StringVar(&(cf.KVStoreDataPrefix), "kv_store_data_prefix", default_KVStoreDataPrefix, help)
180
khenaidoo5c11af72018-07-20 17:21:05 -0400181 help = fmt.Sprintf("Log level")
182 flag.IntVar(&(cf.LogLevel), "log_level", default_LogLevel, help)
183
khenaidoob6080322019-01-29 21:47:38 -0500184 help = fmt.Sprintf("Timeout for long running request")
185 flag.Int64Var(&(cf.LongRunningRequestTimeout), "timeout_long_request", default_LongRunningRequestTimeout, help)
186
187 help = fmt.Sprintf("Default timeout for regular request")
188 flag.Int64Var(&(cf.DefaultRequestTimeout), "timeout_request", default_DefaultRequestTimeout, help)
189
khenaidoo2c6a0992019-04-29 13:46:56 -0400190 help = fmt.Sprintf("Default Core timeout")
191 flag.Int64Var(&(cf.DefaultCoreTimeout), "core_timeout", default_CoreTimeout, help)
192
khenaidoo5c11af72018-07-20 17:21:05 -0400193 help = fmt.Sprintf("Show startup banner log lines")
194 flag.BoolVar(&cf.Banner, "banner", default_Banner, help)
khenaidoocfee5f42018-07-19 22:47:38 -0400195
Richard Jankowski46464e92019-03-05 11:53:55 -0500196 help = fmt.Sprintf("The name of the meta-key whose value is the rw-core group to which the ofagent is bound")
197 flag.StringVar(&(cf.CoreBindingKey), "core_binding_key", default_CoreBindingKey, help)
198
khenaidoocfee5f42018-07-19 22:47:38 -0400199 flag.Parse()
200
khenaidoocfee5f42018-07-19 22:47:38 -0400201 containerName := getContainerInfo()
khenaidoocfee5f42018-07-19 22:47:38 -0400202 if len(containerName) > 0 {
203 cf.InstanceID = containerName
204 }
205
khenaidoocfee5f42018-07-19 22:47:38 -0400206}
207
208func getContainerInfo() string {
209 return os.Getenv("HOSTNAME")
210}