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