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