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