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