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" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 21 | "github.com/opencord/voltha-go/common/log" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 22 | "os" |
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 | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 27 | ConsulStoreName = "consul" |
| 28 | EtcdStoreName = "etcd" |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 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 |
khenaidoo | b608032 | 2019-01-29 21:47:38 -0500 | [diff] [blame] | 51 | default_LongRunningRequestTimeout = int64(2000) |
| 52 | default_DefaultRequestTimeout = int64(500) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 53 | ) |
| 54 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 55 | // RWCoreFlags represents the set of configurations used by the read-write core service |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 56 | type RWCoreFlags struct { |
| 57 | // Command line parameters |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 58 | InstanceID string |
| 59 | RWCoreEndpoint string |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 60 | GrpcHost string |
| 61 | GrpcPort int |
| 62 | KafkaAdapterHost string |
| 63 | KafkaAdapterPort int |
| 64 | KafkaClusterHost string |
| 65 | KafkaClusterPort int |
| 66 | KVStoreType string |
| 67 | KVStoreTimeout int // in seconds |
| 68 | KVStoreHost string |
| 69 | KVStorePort int |
| 70 | KVTxnKeyDelTime int |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 71 | KVStoreDataPrefix string |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 72 | CoreTopic string |
| 73 | LogLevel int |
| 74 | Banner bool |
| 75 | RWCoreKey string |
| 76 | RWCoreCert string |
| 77 | RWCoreCA string |
| 78 | AffinityRouterTopic string |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 79 | InCompetingMode bool |
khenaidoo | b608032 | 2019-01-29 21:47:38 -0500 | [diff] [blame] | 80 | LongRunningRequestTimeout int64 |
| 81 | DefaultRequestTimeout int64 |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 82 | } |
| 83 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 84 | func init() { |
| 85 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 86 | } |
| 87 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 88 | // NewRWCoreFlags returns a new RWCore config |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 89 | func NewRWCoreFlags() *RWCoreFlags { |
| 90 | var rwCoreFlag = RWCoreFlags{ // Default values |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 91 | InstanceID: default_InstanceID, |
| 92 | RWCoreEndpoint: default_RWCoreEndpoint, |
| 93 | GrpcHost: default_GrpcHost, |
| 94 | GrpcPort: default_GrpcPort, |
| 95 | KafkaAdapterHost: default_KafkaAdapterHost, |
| 96 | KafkaAdapterPort: default_KafkaAdapterPort, |
| 97 | KafkaClusterHost: default_KafkaClusterHost, |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 98 | KafkaClusterPort: default_KafkaClusterPort, |
| 99 | KVStoreType: default_KVStoreType, |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 100 | KVStoreTimeout: default_KVStoreTimeout, |
| 101 | KVStoreHost: default_KVStoreHost, |
| 102 | KVStorePort: default_KVStorePort, |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 103 | KVStoreDataPrefix: default_KVStoreDataPrefix, |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 104 | KVTxnKeyDelTime: default_KVTxnKeyDelTime, |
| 105 | CoreTopic: default_CoreTopic, |
| 106 | LogLevel: default_LogLevel, |
| 107 | Banner: default_Banner, |
| 108 | RWCoreKey: default_RWCoreKey, |
| 109 | RWCoreCert: default_RWCoreCert, |
| 110 | RWCoreCA: default_RWCoreCA, |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 111 | AffinityRouterTopic: default_AffinityRouterTopic, |
| 112 | InCompetingMode: default_InCompetingMode, |
khenaidoo | b608032 | 2019-01-29 21:47:38 -0500 | [diff] [blame] | 113 | DefaultRequestTimeout:default_DefaultRequestTimeout, |
| 114 | LongRunningRequestTimeout:default_LongRunningRequestTimeout, |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 115 | } |
| 116 | return &rwCoreFlag |
| 117 | } |
| 118 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 119 | // ParseCommandArguments parses the arguments when running read-write core service |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 120 | func (cf *RWCoreFlags) ParseCommandArguments() { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 121 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 122 | var help string |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 123 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 124 | help = fmt.Sprintf("RW core endpoint address") |
| 125 | flag.StringVar(&(cf.RWCoreEndpoint), "vcore-endpoint", default_RWCoreEndpoint, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 126 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 127 | help = fmt.Sprintf("GRPC server - host") |
| 128 | flag.StringVar(&(cf.GrpcHost), "grpc_host", default_GrpcHost, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 129 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 130 | help = fmt.Sprintf("GRPC server - port") |
| 131 | flag.IntVar(&(cf.GrpcPort), "grpc_port", default_GrpcPort, help) |
| 132 | |
| 133 | help = fmt.Sprintf("Kafka - Adapter messaging host") |
| 134 | flag.StringVar(&(cf.KafkaAdapterHost), "kafka_adapter_host", default_KafkaAdapterHost, help) |
| 135 | |
| 136 | help = fmt.Sprintf("Kafka - Adapter messaging port") |
| 137 | flag.IntVar(&(cf.KafkaAdapterPort), "kafka_adapter_port", default_KafkaAdapterPort, help) |
| 138 | |
| 139 | help = fmt.Sprintf("Kafka - Cluster messaging host") |
| 140 | flag.StringVar(&(cf.KafkaClusterHost), "kafka_cluster_host", default_KafkaClusterHost, help) |
| 141 | |
| 142 | help = fmt.Sprintf("Kafka - Cluster messaging port") |
| 143 | flag.IntVar(&(cf.KafkaClusterPort), "kafka_cluster_port", default_KafkaClusterPort, help) |
| 144 | |
| 145 | help = fmt.Sprintf("RW Core topic") |
| 146 | flag.StringVar(&(cf.CoreTopic), "rw_core_topic", default_CoreTopic, help) |
| 147 | |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 148 | help = fmt.Sprintf("Affinity Router topic") |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 149 | flag.StringVar(&(cf.AffinityRouterTopic), "affinity_router_topic", default_AffinityRouterTopic, help) |
| 150 | |
| 151 | help = fmt.Sprintf("In competing Mode - two cores competing to handle a transaction ") |
| 152 | flag.BoolVar(&cf.InCompetingMode, "in_competing_mode", default_InCompetingMode, help) |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 153 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 154 | help = fmt.Sprintf("KV store type") |
| 155 | flag.StringVar(&(cf.KVStoreType), "kv_store_type", default_KVStoreType, help) |
| 156 | |
| 157 | help = fmt.Sprintf("The default timeout when making a kv store request") |
| 158 | flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", default_KVStoreTimeout, help) |
| 159 | |
| 160 | help = fmt.Sprintf("KV store host") |
| 161 | flag.StringVar(&(cf.KVStoreHost), "kv_store_host", default_KVStoreHost, help) |
| 162 | |
| 163 | help = fmt.Sprintf("KV store port") |
| 164 | flag.IntVar(&(cf.KVStorePort), "kv_store_port", default_KVStorePort, help) |
| 165 | |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 166 | help = fmt.Sprintf("The time to wait before deleting a completed transaction key") |
| 167 | flag.IntVar(&(cf.KVTxnKeyDelTime), "kv_txn_delete_time", default_KVTxnKeyDelTime, help) |
| 168 | |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 169 | help = fmt.Sprintf("KV store data prefix") |
| 170 | flag.StringVar(&(cf.KVStoreDataPrefix), "kv_store_data_prefix", default_KVStoreDataPrefix, help) |
| 171 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 172 | help = fmt.Sprintf("Log level") |
| 173 | flag.IntVar(&(cf.LogLevel), "log_level", default_LogLevel, help) |
| 174 | |
khenaidoo | b608032 | 2019-01-29 21:47:38 -0500 | [diff] [blame] | 175 | help = fmt.Sprintf("Timeout for long running request") |
| 176 | flag.Int64Var(&(cf.LongRunningRequestTimeout), "timeout_long_request", default_LongRunningRequestTimeout, help) |
| 177 | |
| 178 | help = fmt.Sprintf("Default timeout for regular request") |
| 179 | flag.Int64Var(&(cf.DefaultRequestTimeout), "timeout_request", default_DefaultRequestTimeout, help) |
| 180 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 181 | help = fmt.Sprintf("Show startup banner log lines") |
| 182 | flag.BoolVar(&cf.Banner, "banner", default_Banner, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 183 | |
| 184 | flag.Parse() |
| 185 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 186 | containerName := getContainerInfo() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 187 | if len(containerName) > 0 { |
| 188 | cf.InstanceID = containerName |
| 189 | } |
| 190 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | func getContainerInfo() string { |
| 194 | return os.Getenv("HOSTNAME") |
| 195 | } |