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" |
| 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_LogLevel = 0 |
| 42 | default_Banner = false |
| 43 | default_CoreTopic = "rwcore" |
| 44 | default_RWCoreEndpoint = "rwcore" |
| 45 | default_RWCoreKey = "pki/voltha.key" |
| 46 | default_RWCoreCert = "pki/voltha.crt" |
| 47 | default_RWCoreCA = "pki/voltha-CA.pem" |
| 48 | default_Affinity_Router_Topic = "affinityRouter" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 49 | ) |
| 50 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 51 | // RWCoreFlags represents the set of configurations used by the read-write core service |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 52 | type RWCoreFlags struct { |
| 53 | // Command line parameters |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 54 | InstanceID string |
| 55 | RWCoreEndpoint string |
| 56 | GrpcHost string |
| 57 | GrpcPort int |
| 58 | KafkaAdapterHost string |
| 59 | KafkaAdapterPort int |
| 60 | KafkaClusterHost string |
| 61 | KafkaClusterPort int |
| 62 | KVStoreType string |
| 63 | KVStoreTimeout int // in seconds |
| 64 | KVStoreHost string |
| 65 | KVStorePort int |
| 66 | KVTxnKeyDelTime int |
| 67 | CoreTopic string |
| 68 | LogLevel int |
| 69 | Banner bool |
| 70 | RWCoreKey string |
| 71 | RWCoreCert string |
| 72 | RWCoreCA string |
| 73 | AffinityRouterTopic string |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 74 | } |
| 75 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 76 | func init() { |
| 77 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 78 | } |
| 79 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 80 | // NewRWCoreFlags returns a new RWCore config |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 81 | func NewRWCoreFlags() *RWCoreFlags { |
| 82 | var rwCoreFlag = RWCoreFlags{ // Default values |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 83 | InstanceID: default_InstanceID, |
| 84 | RWCoreEndpoint: default_RWCoreEndpoint, |
| 85 | GrpcHost: default_GrpcHost, |
| 86 | GrpcPort: default_GrpcPort, |
| 87 | KafkaAdapterHost: default_KafkaAdapterHost, |
| 88 | KafkaAdapterPort: default_KafkaAdapterPort, |
| 89 | KafkaClusterHost: default_KafkaClusterHost, |
| 90 | KafkaClusterPort: default_KafkaClusterPort, |
| 91 | KVStoreType: default_KVStoreType, |
| 92 | KVStoreTimeout: default_KVStoreTimeout, |
| 93 | KVStoreHost: default_KVStoreHost, |
| 94 | KVStorePort: default_KVStorePort, |
| 95 | KVTxnKeyDelTime: default_KVTxnKeyDelTime, |
| 96 | CoreTopic: default_CoreTopic, |
| 97 | LogLevel: default_LogLevel, |
| 98 | Banner: default_Banner, |
| 99 | RWCoreKey: default_RWCoreKey, |
| 100 | RWCoreCert: default_RWCoreCert, |
| 101 | RWCoreCA: default_RWCoreCA, |
| 102 | AffinityRouterTopic: default_Affinity_Router_Topic, |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 103 | } |
| 104 | return &rwCoreFlag |
| 105 | } |
| 106 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 107 | // ParseCommandArguments parses the arguments when running read-write core service |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 108 | func (cf *RWCoreFlags) ParseCommandArguments() { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 109 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 110 | var help string |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 111 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 112 | help = fmt.Sprintf("RW core endpoint address") |
| 113 | flag.StringVar(&(cf.RWCoreEndpoint), "vcore-endpoint", default_RWCoreEndpoint, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 114 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 115 | help = fmt.Sprintf("GRPC server - host") |
| 116 | flag.StringVar(&(cf.GrpcHost), "grpc_host", default_GrpcHost, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 117 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 118 | help = fmt.Sprintf("GRPC server - port") |
| 119 | flag.IntVar(&(cf.GrpcPort), "grpc_port", default_GrpcPort, help) |
| 120 | |
| 121 | help = fmt.Sprintf("Kafka - Adapter messaging host") |
| 122 | flag.StringVar(&(cf.KafkaAdapterHost), "kafka_adapter_host", default_KafkaAdapterHost, help) |
| 123 | |
| 124 | help = fmt.Sprintf("Kafka - Adapter messaging port") |
| 125 | flag.IntVar(&(cf.KafkaAdapterPort), "kafka_adapter_port", default_KafkaAdapterPort, help) |
| 126 | |
| 127 | help = fmt.Sprintf("Kafka - Cluster messaging host") |
| 128 | flag.StringVar(&(cf.KafkaClusterHost), "kafka_cluster_host", default_KafkaClusterHost, help) |
| 129 | |
| 130 | help = fmt.Sprintf("Kafka - Cluster messaging port") |
| 131 | flag.IntVar(&(cf.KafkaClusterPort), "kafka_cluster_port", default_KafkaClusterPort, help) |
| 132 | |
| 133 | help = fmt.Sprintf("RW Core topic") |
| 134 | flag.StringVar(&(cf.CoreTopic), "rw_core_topic", default_CoreTopic, help) |
| 135 | |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 136 | help = fmt.Sprintf("Affinity Router topic") |
| 137 | flag.StringVar(&(cf.AffinityRouterTopic), "affinity_router_topic", default_Affinity_Router_Topic, help) |
| 138 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 139 | help = fmt.Sprintf("KV store type") |
| 140 | flag.StringVar(&(cf.KVStoreType), "kv_store_type", default_KVStoreType, help) |
| 141 | |
| 142 | help = fmt.Sprintf("The default timeout when making a kv store request") |
| 143 | flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", default_KVStoreTimeout, help) |
| 144 | |
| 145 | help = fmt.Sprintf("KV store host") |
| 146 | flag.StringVar(&(cf.KVStoreHost), "kv_store_host", default_KVStoreHost, help) |
| 147 | |
| 148 | help = fmt.Sprintf("KV store port") |
| 149 | flag.IntVar(&(cf.KVStorePort), "kv_store_port", default_KVStorePort, help) |
| 150 | |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 151 | help = fmt.Sprintf("The time to wait before deleting a completed transaction key") |
| 152 | flag.IntVar(&(cf.KVTxnKeyDelTime), "kv_txn_delete_time", default_KVTxnKeyDelTime, help) |
| 153 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 154 | help = fmt.Sprintf("Log level") |
| 155 | flag.IntVar(&(cf.LogLevel), "log_level", default_LogLevel, help) |
| 156 | |
| 157 | help = fmt.Sprintf("Show startup banner log lines") |
| 158 | flag.BoolVar(&cf.Banner, "banner", default_Banner, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 159 | |
| 160 | flag.Parse() |
| 161 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 162 | containerName := getContainerInfo() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 163 | if len(containerName) > 0 { |
| 164 | cf.InstanceID = containerName |
| 165 | } |
| 166 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | func getContainerInfo() string { |
| 170 | return os.Getenv("HOSTNAME") |
| 171 | } |