Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [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 | |
| 17 | //Package config provides the Log, kvstore, Kafka configuration |
| 18 | package config |
| 19 | |
| 20 | import ( |
| 21 | "flag" |
| 22 | "fmt" |
| 23 | "os" |
| 24 | "time" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // Open ONU default constants |
| 28 | const ( |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 29 | etcdStoreName = "etcd" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 30 | defaultInstanceid = "openonu" |
| 31 | defaultKafkaadapterhost = "192.168.0.20" |
| 32 | defaultKafkaadapterport = 9092 |
| 33 | defaultKafkaclusterhost = "10.100.198.220" |
| 34 | defaultKafkaclusterport = 9092 |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 35 | defaultKvstoretype = etcdStoreName |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 36 | defaultKvstoretimeout = 5 * time.Second |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 37 | defaultKvstorehost = "localhost" |
| 38 | defaultKvstoreport = 2379 // Consul = 8500; Etcd = 2379 |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 39 | defaultLoglevel = "WARN" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 40 | defaultBanner = false |
| 41 | defaultDisplayVersionOnly = false |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 42 | defaultAccIncrEvto = false |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 43 | defaultTopic = "openonu" |
Holger Hildebrandt | a768fe9 | 2020-10-01 13:06:21 +0000 | [diff] [blame] | 44 | defaultCoreTopic = "rwcore" |
| 45 | defaultEventTopic = "voltha.events" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 46 | defaultOnunumber = 1 |
| 47 | defaultProbeHost = "" |
| 48 | defaultProbePort = 8080 |
| 49 | defaultLiveProbeInterval = 60 * time.Second |
| 50 | defaultNotLiveProbeInterval = 5 * time.Second // Probe more frequently when not alive |
| 51 | //defaultHearbeatFailReportInterval is the time in seconds the adapter will keep checking the hardware for heartbeat. |
| 52 | defaultHearbeatCheckInterval = 30 * time.Second |
| 53 | // defaultHearbeatFailReportInterval is the time adapter will wait before updating the state to the core. |
| 54 | defaultHearbeatFailReportInterval = 180 * time.Second |
| 55 | //defaultKafkaReconnectRetries -1: reconnect endlessly. |
Himani Chawla | d96df18 | 2020-09-28 11:12:02 +0530 | [diff] [blame] | 56 | defaultKafkaReconnectRetries = -1 |
| 57 | defaultCurrentReplica = 1 |
| 58 | defaultTotalReplicas = 1 |
| 59 | defaultMaxTimeoutInterAdapterComm = 30 * time.Second |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 60 | |
| 61 | // For Tracing |
| 62 | defaultTraceEnabled = false |
| 63 | defaultTraceAgentAddress = "127.0.0.1:6831" |
| 64 | defaultLogCorrelationEnabled = true |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 65 | ) |
| 66 | |
| 67 | // AdapterFlags represents the set of configurations used by the read-write adaptercore service |
| 68 | type AdapterFlags struct { |
| 69 | // Command line parameters |
| 70 | InstanceID string |
| 71 | KafkaAdapterHost string |
| 72 | KafkaAdapterPort int |
| 73 | KafkaClusterHost string |
| 74 | KafkaClusterPort int |
| 75 | KVStoreType string |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 76 | KVStoreTimeout time.Duration |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 77 | KVStoreHost string |
| 78 | KVStorePort int |
| 79 | Topic string |
| 80 | CoreTopic string |
| 81 | EventTopic string |
| 82 | LogLevel string |
| 83 | OnuNumber int |
| 84 | Banner bool |
| 85 | DisplayVersionOnly bool |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 86 | AccIncrEvto bool |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 87 | ProbeHost string |
| 88 | ProbePort int |
| 89 | LiveProbeInterval time.Duration |
| 90 | NotLiveProbeInterval time.Duration |
| 91 | HeartbeatCheckInterval time.Duration |
| 92 | HeartbeatFailReportInterval time.Duration |
| 93 | KafkaReconnectRetries int |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 94 | CurrentReplica int |
| 95 | TotalReplicas int |
Himani Chawla | d96df18 | 2020-09-28 11:12:02 +0530 | [diff] [blame] | 96 | MaxTimeoutInterAdapterComm time.Duration |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 97 | TraceEnabled bool |
| 98 | TraceAgentAddress string |
| 99 | LogCorrelationEnabled bool |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // NewAdapterFlags returns a new RWCore config |
| 103 | func NewAdapterFlags() *AdapterFlags { |
| 104 | var adapterFlags = AdapterFlags{ // Default values |
| 105 | InstanceID: defaultInstanceid, |
| 106 | KafkaAdapterHost: defaultKafkaadapterhost, |
| 107 | KafkaAdapterPort: defaultKafkaadapterport, |
| 108 | KafkaClusterHost: defaultKafkaclusterhost, |
| 109 | KafkaClusterPort: defaultKafkaclusterport, |
| 110 | KVStoreType: defaultKvstoretype, |
| 111 | KVStoreTimeout: defaultKvstoretimeout, |
| 112 | KVStoreHost: defaultKvstorehost, |
| 113 | KVStorePort: defaultKvstoreport, |
| 114 | Topic: defaultTopic, |
Holger Hildebrandt | a768fe9 | 2020-10-01 13:06:21 +0000 | [diff] [blame] | 115 | CoreTopic: defaultCoreTopic, |
| 116 | EventTopic: defaultEventTopic, |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 117 | LogLevel: defaultLoglevel, |
| 118 | OnuNumber: defaultOnunumber, |
| 119 | Banner: defaultBanner, |
| 120 | DisplayVersionOnly: defaultDisplayVersionOnly, |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 121 | AccIncrEvto: defaultAccIncrEvto, |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 122 | ProbeHost: defaultProbeHost, |
| 123 | ProbePort: defaultProbePort, |
| 124 | LiveProbeInterval: defaultLiveProbeInterval, |
| 125 | NotLiveProbeInterval: defaultNotLiveProbeInterval, |
| 126 | HeartbeatCheckInterval: defaultHearbeatCheckInterval, |
| 127 | HeartbeatFailReportInterval: defaultHearbeatFailReportInterval, |
| 128 | KafkaReconnectRetries: defaultKafkaReconnectRetries, |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 129 | CurrentReplica: defaultCurrentReplica, |
| 130 | TotalReplicas: defaultTotalReplicas, |
Himani Chawla | d96df18 | 2020-09-28 11:12:02 +0530 | [diff] [blame] | 131 | MaxTimeoutInterAdapterComm: defaultMaxTimeoutInterAdapterComm, |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 132 | TraceEnabled: defaultTraceEnabled, |
| 133 | TraceAgentAddress: defaultTraceAgentAddress, |
| 134 | LogCorrelationEnabled: defaultLogCorrelationEnabled, |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 135 | } |
| 136 | return &adapterFlags |
| 137 | } |
| 138 | |
| 139 | // ParseCommandArguments parses the arguments when running read-write adaptercore service |
| 140 | func (so *AdapterFlags) ParseCommandArguments() { |
| 141 | |
| 142 | help := fmt.Sprintf("Kafka - Adapter messaging host") |
| 143 | flag.StringVar(&(so.KafkaAdapterHost), "kafka_adapter_host", defaultKafkaadapterhost, help) |
| 144 | |
| 145 | help = fmt.Sprintf("Kafka - Adapter messaging port") |
| 146 | flag.IntVar(&(so.KafkaAdapterPort), "kafka_adapter_port", defaultKafkaadapterport, help) |
| 147 | |
| 148 | help = fmt.Sprintf("Kafka - Cluster messaging host") |
| 149 | flag.StringVar(&(so.KafkaClusterHost), "kafka_cluster_host", defaultKafkaclusterhost, help) |
| 150 | |
| 151 | help = fmt.Sprintf("Kafka - Cluster messaging port") |
| 152 | flag.IntVar(&(so.KafkaClusterPort), "kafka_cluster_port", defaultKafkaclusterport, help) |
| 153 | |
| 154 | help = fmt.Sprintf("Open ONU topic") |
Andrea Campanella | 961734c | 2021-01-18 11:44:47 +0100 | [diff] [blame] | 155 | baseAdapterTopic := flag.String("adapter_topic", defaultTopic, help) |
Holger Hildebrandt | a768fe9 | 2020-10-01 13:06:21 +0000 | [diff] [blame] | 156 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 157 | help = fmt.Sprintf("Core topic") |
Holger Hildebrandt | a768fe9 | 2020-10-01 13:06:21 +0000 | [diff] [blame] | 158 | flag.StringVar(&(so.CoreTopic), "core_topic", defaultCoreTopic, help) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 159 | |
| 160 | help = fmt.Sprintf("Event topic") |
Holger Hildebrandt | a768fe9 | 2020-10-01 13:06:21 +0000 | [diff] [blame] | 161 | flag.StringVar(&(so.EventTopic), "event_topic", defaultEventTopic, help) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 162 | |
| 163 | help = fmt.Sprintf("KV store type") |
| 164 | flag.StringVar(&(so.KVStoreType), "kv_store_type", defaultKvstoretype, help) |
| 165 | |
| 166 | help = fmt.Sprintf("The default timeout when making a kv store request") |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 167 | flag.DurationVar(&(so.KVStoreTimeout), "kv_store_request_timeout", defaultKvstoretimeout, help) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 168 | |
| 169 | help = fmt.Sprintf("KV store host") |
| 170 | flag.StringVar(&(so.KVStoreHost), "kv_store_host", defaultKvstorehost, help) |
| 171 | |
| 172 | help = fmt.Sprintf("KV store port") |
| 173 | flag.IntVar(&(so.KVStorePort), "kv_store_port", defaultKvstoreport, help) |
| 174 | |
| 175 | help = fmt.Sprintf("Log level") |
| 176 | flag.StringVar(&(so.LogLevel), "log_level", defaultLoglevel, help) |
| 177 | |
| 178 | help = fmt.Sprintf("Number of ONUs") |
| 179 | flag.IntVar(&(so.OnuNumber), "onu_number", defaultOnunumber, help) |
| 180 | |
| 181 | help = fmt.Sprintf("Show startup banner log lines") |
| 182 | flag.BoolVar(&(so.Banner), "banner", defaultBanner, help) |
| 183 | |
| 184 | help = fmt.Sprintf("Show version information and exit") |
| 185 | flag.BoolVar(&(so.DisplayVersionOnly), "version", defaultDisplayVersionOnly, help) |
| 186 | |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 187 | help = fmt.Sprintf("Acceptance of incremental EVTOCD configuration") |
| 188 | flag.BoolVar(&(so.AccIncrEvto), "accept_incr_evto", defaultAccIncrEvto, help) |
| 189 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 190 | help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 191 | flag.StringVar(&(so.ProbeHost), "probe_host", defaultProbeHost, help) |
| 192 | |
| 193 | help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 194 | flag.IntVar(&(so.ProbePort), "probe_port", defaultProbePort, help) |
| 195 | |
| 196 | help = fmt.Sprintf("Number of seconds for the default liveliness check") |
| 197 | flag.DurationVar(&(so.LiveProbeInterval), "live_probe_interval", defaultLiveProbeInterval, help) |
| 198 | |
| 199 | help = fmt.Sprintf("Number of seconds for liveliness check if probe is not running") |
| 200 | flag.DurationVar(&(so.NotLiveProbeInterval), "not_live_probe_interval", defaultNotLiveProbeInterval, help) |
| 201 | |
| 202 | help = fmt.Sprintf("Number of seconds for heartbeat check interval.") |
| 203 | flag.DurationVar(&(so.HeartbeatCheckInterval), "hearbeat_check_interval", defaultHearbeatCheckInterval, help) |
| 204 | |
| 205 | help = fmt.Sprintf("Number of seconds adapter has to wait before reporting core on the hearbeat check failure.") |
| 206 | flag.DurationVar(&(so.HeartbeatFailReportInterval), "hearbeat_fail_interval", defaultHearbeatFailReportInterval, help) |
| 207 | |
| 208 | help = fmt.Sprintf("Number of retries to connect to Kafka.") |
| 209 | flag.IntVar(&(so.KafkaReconnectRetries), "kafka_reconnect_retries", defaultKafkaReconnectRetries, help) |
| 210 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 211 | help = "Replica number of this particular instance (default: %s)" |
| 212 | flag.IntVar(&(so.CurrentReplica), "current_replica", defaultCurrentReplica, help) |
| 213 | |
| 214 | help = "Total number of instances for this adapter" |
| 215 | flag.IntVar(&(so.TotalReplicas), "total_replica", defaultTotalReplicas, help) |
| 216 | |
Himani Chawla | d96df18 | 2020-09-28 11:12:02 +0530 | [diff] [blame] | 217 | help = fmt.Sprintf("Maximum Number of seconds for the default interadapter communication timeout") |
| 218 | flag.DurationVar(&(so.MaxTimeoutInterAdapterComm), "max_timeout_interadapter_comm", |
| 219 | defaultMaxTimeoutInterAdapterComm, help) |
| 220 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 221 | help = fmt.Sprintf("Whether to send logs to tracing agent?") |
| 222 | flag.BoolVar(&(so.TraceEnabled), "trace_enabled", defaultTraceEnabled, help) |
| 223 | |
| 224 | help = fmt.Sprintf("The address of tracing agent to which span info should be sent.") |
| 225 | flag.StringVar(&(so.TraceAgentAddress), "trace_agent_address", defaultTraceAgentAddress, help) |
| 226 | |
| 227 | help = fmt.Sprintf("Whether to enrich log statements with fields denoting operation being executed for achieving correlation?") |
| 228 | flag.BoolVar(&(so.LogCorrelationEnabled), "log_correlation_enabled", defaultLogCorrelationEnabled, help) |
| 229 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 230 | flag.Parse() |
| 231 | containerName := getContainerInfo() |
| 232 | if len(containerName) > 0 { |
| 233 | so.InstanceID = containerName |
| 234 | } |
| 235 | |
Andrea Campanella | 961734c | 2021-01-18 11:44:47 +0100 | [diff] [blame] | 236 | so.Topic = fmt.Sprintf("%s_%d", *baseAdapterTopic, int32(so.CurrentReplica)) |
| 237 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | func getContainerInfo() string { |
| 241 | return os.Getenv("HOSTNAME") |
| 242 | } |