Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [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 | */ |
| 16 | package config |
| 17 | |
| 18 | import ( |
| 19 | "flag" |
| 20 | "fmt" |
| 21 | "github.com/opencord/voltha-go/common/log" |
| 22 | "os" |
| 23 | ) |
| 24 | |
| 25 | // RO Core service default constants |
| 26 | const ( |
| 27 | ConsulStoreName = "consul" |
| 28 | EtcdStoreName = "etcd" |
| 29 | default_InstanceID = "rocore001" |
| 30 | default_GrpcPort = 50057 |
| 31 | default_GrpcHost = "" |
| 32 | default_KVStoreType = EtcdStoreName |
| 33 | default_KVStoreTimeout = 5 //in seconds |
| 34 | default_KVStoreHost = "127.0.0.1" |
| 35 | default_KVStorePort = 2379 // Consul = 8500; Etcd = 2379 |
| 36 | default_KVTxnKeyDelTime = 60 |
| 37 | default_LogLevel = 0 |
| 38 | default_Banner = false |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 39 | default_DisplayVersionOnly = false |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 40 | default_CoreTopic = "rocore" |
| 41 | default_ROCoreEndpoint = "rocore" |
| 42 | default_ROCoreKey = "pki/voltha.key" |
| 43 | default_ROCoreCert = "pki/voltha.crt" |
| 44 | default_ROCoreCA = "pki/voltha-CA.pem" |
| 45 | default_Affinity_Router_Topic = "affinityRouter" |
Hardik Windlass | 17c2c74 | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 46 | default_ProbePort = 8080 |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 47 | ) |
| 48 | |
| 49 | // ROCoreFlags represents the set of configurations used by the read-only core service |
| 50 | type ROCoreFlags struct { |
| 51 | // Command line parameters |
| 52 | InstanceID string |
| 53 | ROCoreEndpoint string |
| 54 | GrpcHost string |
| 55 | GrpcPort int |
| 56 | KVStoreType string |
| 57 | KVStoreTimeout int // in seconds |
| 58 | KVStoreHost string |
| 59 | KVStorePort int |
| 60 | KVTxnKeyDelTime int |
| 61 | CoreTopic string |
| 62 | LogLevel int |
| 63 | Banner bool |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 64 | DisplayVersionOnly bool |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 65 | ROCoreKey string |
| 66 | ROCoreCert string |
| 67 | ROCoreCA string |
| 68 | AffinityRouterTopic string |
Hardik Windlass | 17c2c74 | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 69 | ProbePort int |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | func init() { |
| 73 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 74 | } |
| 75 | |
| 76 | // NewROCoreFlags returns a new ROCore config |
| 77 | func NewROCoreFlags() *ROCoreFlags { |
| 78 | var roCoreFlag = ROCoreFlags{ // Default values |
| 79 | InstanceID: default_InstanceID, |
| 80 | ROCoreEndpoint: default_ROCoreEndpoint, |
| 81 | GrpcHost: default_GrpcHost, |
| 82 | GrpcPort: default_GrpcPort, |
| 83 | KVStoreType: default_KVStoreType, |
| 84 | KVStoreTimeout: default_KVStoreTimeout, |
| 85 | KVStoreHost: default_KVStoreHost, |
| 86 | KVStorePort: default_KVStorePort, |
| 87 | KVTxnKeyDelTime: default_KVTxnKeyDelTime, |
| 88 | CoreTopic: default_CoreTopic, |
| 89 | LogLevel: default_LogLevel, |
| 90 | Banner: default_Banner, |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 91 | DisplayVersionOnly: default_DisplayVersionOnly, |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 92 | ROCoreKey: default_ROCoreKey, |
| 93 | ROCoreCert: default_ROCoreCert, |
| 94 | ROCoreCA: default_ROCoreCA, |
| 95 | AffinityRouterTopic: default_Affinity_Router_Topic, |
Hardik Windlass | 17c2c74 | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 96 | ProbePort: default_ProbePort, |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 97 | } |
| 98 | return &roCoreFlag |
| 99 | } |
| 100 | |
| 101 | // ParseCommandArguments parses the arguments when running read-only core service |
| 102 | func (cf *ROCoreFlags) ParseCommandArguments() { |
| 103 | |
| 104 | var help string |
| 105 | |
| 106 | help = fmt.Sprintf("RO core endpoint address") |
| 107 | flag.StringVar(&(cf.ROCoreEndpoint), "vcore-endpoint", default_ROCoreEndpoint, help) |
| 108 | |
| 109 | help = fmt.Sprintf("GRPC server - host") |
| 110 | flag.StringVar(&(cf.GrpcHost), "grpc_host", default_GrpcHost, help) |
| 111 | |
| 112 | help = fmt.Sprintf("GRPC server - port") |
| 113 | flag.IntVar(&(cf.GrpcPort), "grpc_port", default_GrpcPort, help) |
| 114 | |
| 115 | help = fmt.Sprintf("RO Core topic") |
| 116 | flag.StringVar(&(cf.CoreTopic), "ro_core_topic", default_CoreTopic, help) |
| 117 | |
| 118 | help = fmt.Sprintf("Affinity Router topic") |
| 119 | flag.StringVar(&(cf.AffinityRouterTopic), "affinity_router_topic", default_Affinity_Router_Topic, help) |
| 120 | |
| 121 | help = fmt.Sprintf("KV store type") |
| 122 | flag.StringVar(&(cf.KVStoreType), "kv_store_type", default_KVStoreType, help) |
| 123 | |
| 124 | help = fmt.Sprintf("The default timeout when making a kv store request") |
| 125 | flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", default_KVStoreTimeout, help) |
| 126 | |
| 127 | help = fmt.Sprintf("KV store host") |
| 128 | flag.StringVar(&(cf.KVStoreHost), "kv_store_host", default_KVStoreHost, help) |
| 129 | |
| 130 | help = fmt.Sprintf("KV store port") |
| 131 | flag.IntVar(&(cf.KVStorePort), "kv_store_port", default_KVStorePort, help) |
| 132 | |
| 133 | help = fmt.Sprintf("The time to wait before deleting a completed transaction key") |
| 134 | flag.IntVar(&(cf.KVTxnKeyDelTime), "kv_txn_delete_time", default_KVTxnKeyDelTime, help) |
| 135 | |
| 136 | help = fmt.Sprintf("Log level") |
| 137 | flag.IntVar(&(cf.LogLevel), "log_level", default_LogLevel, help) |
| 138 | |
| 139 | help = fmt.Sprintf("Show startup banner log lines") |
| 140 | flag.BoolVar(&cf.Banner, "banner", default_Banner, help) |
| 141 | |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 142 | help = fmt.Sprintf("Show version information and exit") |
| 143 | flag.BoolVar(&cf.DisplayVersionOnly, "version", default_DisplayVersionOnly, help) |
| 144 | |
Hardik Windlass | 17c2c74 | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 145 | help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 146 | flag.IntVar(&(cf.ProbePort), "probe_port", default_ProbePort, help) |
| 147 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 148 | flag.Parse() |
| 149 | |
| 150 | containerName := getContainerInfo() |
| 151 | if len(containerName) > 0 { |
| 152 | cf.InstanceID = containerName |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | |
| 157 | func getContainerInfo() string { |
| 158 | return os.Getenv("HOSTNAME") |
| 159 | } |