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" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 22 | "os" |
Girish Kumar | 9148264 | 2019-11-08 11:38:03 +0000 | [diff] [blame] | 23 | "time" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // RO Core service default constants |
| 27 | const ( |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 28 | ConsulStoreName = "consul" |
| 29 | EtcdStoreName = "etcd" |
| 30 | default_InstanceID = "rocore001" |
| 31 | default_GrpcPort = 50057 |
| 32 | default_GrpcHost = "" |
| 33 | default_KVStoreType = EtcdStoreName |
| 34 | default_KVStoreTimeout = 5 //in seconds |
| 35 | default_KVStoreHost = "127.0.0.1" |
| 36 | default_KVStorePort = 2379 // Consul = 8500; Etcd = 2379 |
| 37 | default_KVTxnKeyDelTime = 60 |
| 38 | default_LogLevel = 0 |
| 39 | default_Banner = false |
| 40 | default_DisplayVersionOnly = false |
| 41 | default_CoreTopic = "rocore" |
| 42 | default_ROCoreEndpoint = "rocore" |
| 43 | default_ROCoreKey = "pki/voltha.key" |
| 44 | default_ROCoreCert = "pki/voltha.crt" |
| 45 | default_ROCoreCA = "pki/voltha-CA.pem" |
| 46 | default_Affinity_Router_Topic = "affinityRouter" |
| 47 | default_ProbeHost = "" |
| 48 | default_ProbePort = 8080 |
| 49 | default_LiveProbeInterval = 60 * time.Second |
| 50 | default_NotLiveProbeInterval = 5 * time.Second // Probe more frequently to detect Recovery early |
| 51 | default_CoreTimeout = 59 * time.Second |
| 52 | default_MaxConnectionRetries = -1 // retries forever |
| 53 | default_ConnectionRetryInterval = 2 * time.Second // in seconds |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 54 | ) |
| 55 | |
| 56 | // ROCoreFlags represents the set of configurations used by the read-only core service |
| 57 | type ROCoreFlags struct { |
| 58 | // Command line parameters |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 59 | InstanceID string |
| 60 | ROCoreEndpoint string |
| 61 | GrpcHost string |
| 62 | GrpcPort int |
| 63 | KVStoreType string |
| 64 | KVStoreTimeout int // in seconds |
| 65 | KVStoreHost string |
| 66 | KVStorePort int |
| 67 | KVTxnKeyDelTime int |
| 68 | CoreTopic string |
| 69 | LogLevel int |
| 70 | Banner bool |
| 71 | DisplayVersionOnly bool |
| 72 | ROCoreKey string |
| 73 | ROCoreCert string |
| 74 | ROCoreCA string |
| 75 | AffinityRouterTopic string |
| 76 | ProbeHost string |
| 77 | ProbePort int |
| 78 | LiveProbeInterval time.Duration |
| 79 | NotLiveProbeInterval time.Duration |
| 80 | CoreTimeout time.Duration |
| 81 | MaxConnectionRetries int |
| 82 | ConnectionRetryInterval time.Duration |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | func init() { |
| 86 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 87 | } |
| 88 | |
| 89 | // NewROCoreFlags returns a new ROCore config |
| 90 | func NewROCoreFlags() *ROCoreFlags { |
| 91 | var roCoreFlag = ROCoreFlags{ // Default values |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 92 | InstanceID: default_InstanceID, |
| 93 | ROCoreEndpoint: default_ROCoreEndpoint, |
| 94 | GrpcHost: default_GrpcHost, |
| 95 | GrpcPort: default_GrpcPort, |
| 96 | KVStoreType: default_KVStoreType, |
| 97 | KVStoreTimeout: default_KVStoreTimeout, |
| 98 | KVStoreHost: default_KVStoreHost, |
| 99 | KVStorePort: default_KVStorePort, |
| 100 | KVTxnKeyDelTime: default_KVTxnKeyDelTime, |
| 101 | CoreTopic: default_CoreTopic, |
| 102 | LogLevel: default_LogLevel, |
| 103 | Banner: default_Banner, |
| 104 | DisplayVersionOnly: default_DisplayVersionOnly, |
| 105 | ROCoreKey: default_ROCoreKey, |
| 106 | ROCoreCert: default_ROCoreCert, |
| 107 | ROCoreCA: default_ROCoreCA, |
| 108 | AffinityRouterTopic: default_Affinity_Router_Topic, |
| 109 | ProbeHost: default_ProbeHost, |
| 110 | ProbePort: default_ProbePort, |
| 111 | LiveProbeInterval: default_LiveProbeInterval, |
| 112 | NotLiveProbeInterval: default_NotLiveProbeInterval, |
| 113 | CoreTimeout: default_CoreTimeout, |
| 114 | MaxConnectionRetries: default_MaxConnectionRetries, |
| 115 | ConnectionRetryInterval: default_ConnectionRetryInterval, |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 116 | } |
| 117 | return &roCoreFlag |
| 118 | } |
| 119 | |
| 120 | // ParseCommandArguments parses the arguments when running read-only core service |
| 121 | func (cf *ROCoreFlags) ParseCommandArguments() { |
| 122 | |
| 123 | var help string |
| 124 | |
| 125 | help = fmt.Sprintf("RO core endpoint address") |
| 126 | flag.StringVar(&(cf.ROCoreEndpoint), "vcore-endpoint", default_ROCoreEndpoint, help) |
| 127 | |
| 128 | help = fmt.Sprintf("GRPC server - host") |
| 129 | flag.StringVar(&(cf.GrpcHost), "grpc_host", default_GrpcHost, help) |
| 130 | |
| 131 | help = fmt.Sprintf("GRPC server - port") |
| 132 | flag.IntVar(&(cf.GrpcPort), "grpc_port", default_GrpcPort, help) |
| 133 | |
| 134 | help = fmt.Sprintf("RO Core topic") |
| 135 | flag.StringVar(&(cf.CoreTopic), "ro_core_topic", default_CoreTopic, help) |
| 136 | |
| 137 | help = fmt.Sprintf("Affinity Router topic") |
| 138 | flag.StringVar(&(cf.AffinityRouterTopic), "affinity_router_topic", default_Affinity_Router_Topic, help) |
| 139 | |
| 140 | help = fmt.Sprintf("KV store type") |
| 141 | flag.StringVar(&(cf.KVStoreType), "kv_store_type", default_KVStoreType, help) |
| 142 | |
| 143 | help = fmt.Sprintf("The default timeout when making a kv store request") |
| 144 | flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", default_KVStoreTimeout, help) |
| 145 | |
| 146 | help = fmt.Sprintf("KV store host") |
| 147 | flag.StringVar(&(cf.KVStoreHost), "kv_store_host", default_KVStoreHost, help) |
| 148 | |
| 149 | help = fmt.Sprintf("KV store port") |
| 150 | flag.IntVar(&(cf.KVStorePort), "kv_store_port", default_KVStorePort, help) |
| 151 | |
| 152 | help = fmt.Sprintf("The time to wait before deleting a completed transaction key") |
| 153 | flag.IntVar(&(cf.KVTxnKeyDelTime), "kv_txn_delete_time", default_KVTxnKeyDelTime, help) |
| 154 | |
| 155 | help = fmt.Sprintf("Log level") |
| 156 | flag.IntVar(&(cf.LogLevel), "log_level", default_LogLevel, help) |
| 157 | |
| 158 | help = fmt.Sprintf("Show startup banner log lines") |
| 159 | flag.BoolVar(&cf.Banner, "banner", default_Banner, help) |
| 160 | |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 161 | help = fmt.Sprintf("Show version information and exit") |
| 162 | flag.BoolVar(&cf.DisplayVersionOnly, "version", default_DisplayVersionOnly, help) |
| 163 | |
Kent Hagerman | c461883 | 2019-10-07 12:24:36 -0400 | [diff] [blame] | 164 | help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 165 | flag.StringVar(&(cf.ProbeHost), "probe_host", default_ProbeHost, help) |
| 166 | |
Hardik Windlass | dc63dde | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 167 | help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 168 | flag.IntVar(&(cf.ProbePort), "probe_port", default_ProbePort, help) |
| 169 | |
Girish Kumar | 9148264 | 2019-11-08 11:38:03 +0000 | [diff] [blame] | 170 | help = fmt.Sprintf("Time interval between liveness probes while in a live state") |
| 171 | flag.DurationVar(&(cf.LiveProbeInterval), "live_probe_interval", default_LiveProbeInterval, help) |
| 172 | |
| 173 | help = fmt.Sprintf("Time interval between liveness probes while in a not live state") |
| 174 | flag.DurationVar(&(cf.NotLiveProbeInterval), "not_live_probe_interval", default_NotLiveProbeInterval, help) |
| 175 | |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 176 | help = fmt.Sprintf("The maximum time the core will wait while attempting to connect to a dependent component duration") |
| 177 | flag.DurationVar(&(cf.CoreTimeout), "core_timeout", default_CoreTimeout, help) |
| 178 | |
| 179 | help = fmt.Sprintf("The number of retries to connect to a dependent component") |
| 180 | flag.IntVar(&(cf.MaxConnectionRetries), "max_connection_retries", default_MaxConnectionRetries, help) |
| 181 | |
| 182 | help = fmt.Sprintf("The duration between each connection retry attempt ") |
| 183 | flag.DurationVar(&(cf.ConnectionRetryInterval), "connection_retry_interval", default_ConnectionRetryInterval, help) |
| 184 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 185 | flag.Parse() |
| 186 | |
| 187 | containerName := getContainerInfo() |
| 188 | if len(containerName) > 0 { |
| 189 | cf.InstanceID = containerName |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |
| 194 | func getContainerInfo() string { |
| 195 | return os.Getenv("HOSTNAME") |
| 196 | } |