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" |
| 44 | defaultCoretopic = "rwcore" |
| 45 | defaultEventtopic = "voltha.events" |
| 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 |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 60 | ) |
| 61 | |
| 62 | // AdapterFlags represents the set of configurations used by the read-write adaptercore service |
| 63 | type AdapterFlags struct { |
| 64 | // Command line parameters |
| 65 | InstanceID string |
| 66 | KafkaAdapterHost string |
| 67 | KafkaAdapterPort int |
| 68 | KafkaClusterHost string |
| 69 | KafkaClusterPort int |
| 70 | KVStoreType string |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 71 | KVStoreTimeout time.Duration |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 72 | KVStoreHost string |
| 73 | KVStorePort int |
| 74 | Topic string |
| 75 | CoreTopic string |
| 76 | EventTopic string |
| 77 | LogLevel string |
| 78 | OnuNumber int |
| 79 | Banner bool |
| 80 | DisplayVersionOnly bool |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 81 | AccIncrEvto bool |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 82 | ProbeHost string |
| 83 | ProbePort int |
| 84 | LiveProbeInterval time.Duration |
| 85 | NotLiveProbeInterval time.Duration |
| 86 | HeartbeatCheckInterval time.Duration |
| 87 | HeartbeatFailReportInterval time.Duration |
| 88 | KafkaReconnectRetries int |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 89 | CurrentReplica int |
| 90 | TotalReplicas int |
Himani Chawla | d96df18 | 2020-09-28 11:12:02 +0530 | [diff] [blame^] | 91 | MaxTimeoutInterAdapterComm time.Duration |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // NewAdapterFlags returns a new RWCore config |
| 95 | func NewAdapterFlags() *AdapterFlags { |
| 96 | var adapterFlags = AdapterFlags{ // Default values |
| 97 | InstanceID: defaultInstanceid, |
| 98 | KafkaAdapterHost: defaultKafkaadapterhost, |
| 99 | KafkaAdapterPort: defaultKafkaadapterport, |
| 100 | KafkaClusterHost: defaultKafkaclusterhost, |
| 101 | KafkaClusterPort: defaultKafkaclusterport, |
| 102 | KVStoreType: defaultKvstoretype, |
| 103 | KVStoreTimeout: defaultKvstoretimeout, |
| 104 | KVStoreHost: defaultKvstorehost, |
| 105 | KVStorePort: defaultKvstoreport, |
| 106 | Topic: defaultTopic, |
| 107 | CoreTopic: defaultCoretopic, |
| 108 | EventTopic: defaultEventtopic, |
| 109 | LogLevel: defaultLoglevel, |
| 110 | OnuNumber: defaultOnunumber, |
| 111 | Banner: defaultBanner, |
| 112 | DisplayVersionOnly: defaultDisplayVersionOnly, |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 113 | AccIncrEvto: defaultAccIncrEvto, |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 114 | ProbeHost: defaultProbeHost, |
| 115 | ProbePort: defaultProbePort, |
| 116 | LiveProbeInterval: defaultLiveProbeInterval, |
| 117 | NotLiveProbeInterval: defaultNotLiveProbeInterval, |
| 118 | HeartbeatCheckInterval: defaultHearbeatCheckInterval, |
| 119 | HeartbeatFailReportInterval: defaultHearbeatFailReportInterval, |
| 120 | KafkaReconnectRetries: defaultKafkaReconnectRetries, |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 121 | CurrentReplica: defaultCurrentReplica, |
| 122 | TotalReplicas: defaultTotalReplicas, |
Himani Chawla | d96df18 | 2020-09-28 11:12:02 +0530 | [diff] [blame^] | 123 | MaxTimeoutInterAdapterComm: defaultMaxTimeoutInterAdapterComm, |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 124 | } |
| 125 | return &adapterFlags |
| 126 | } |
| 127 | |
| 128 | // ParseCommandArguments parses the arguments when running read-write adaptercore service |
| 129 | func (so *AdapterFlags) ParseCommandArguments() { |
| 130 | |
| 131 | help := fmt.Sprintf("Kafka - Adapter messaging host") |
| 132 | flag.StringVar(&(so.KafkaAdapterHost), "kafka_adapter_host", defaultKafkaadapterhost, help) |
| 133 | |
| 134 | help = fmt.Sprintf("Kafka - Adapter messaging port") |
| 135 | flag.IntVar(&(so.KafkaAdapterPort), "kafka_adapter_port", defaultKafkaadapterport, help) |
| 136 | |
| 137 | help = fmt.Sprintf("Kafka - Cluster messaging host") |
| 138 | flag.StringVar(&(so.KafkaClusterHost), "kafka_cluster_host", defaultKafkaclusterhost, help) |
| 139 | |
| 140 | help = fmt.Sprintf("Kafka - Cluster messaging port") |
| 141 | flag.IntVar(&(so.KafkaClusterPort), "kafka_cluster_port", defaultKafkaclusterport, help) |
| 142 | |
| 143 | help = fmt.Sprintf("Open ONU topic") |
| 144 | flag.StringVar(&(so.Topic), "adapter_topic", defaultTopic, help) |
| 145 | |
| 146 | help = fmt.Sprintf("Core topic") |
| 147 | flag.StringVar(&(so.CoreTopic), "core_topic", defaultCoretopic, help) |
| 148 | |
| 149 | help = fmt.Sprintf("Event topic") |
| 150 | flag.StringVar(&(so.EventTopic), "event_topic", defaultEventtopic, help) |
| 151 | |
| 152 | help = fmt.Sprintf("KV store type") |
| 153 | flag.StringVar(&(so.KVStoreType), "kv_store_type", defaultKvstoretype, help) |
| 154 | |
| 155 | help = fmt.Sprintf("The default timeout when making a kv store request") |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 156 | flag.DurationVar(&(so.KVStoreTimeout), "kv_store_request_timeout", defaultKvstoretimeout, help) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 157 | |
| 158 | help = fmt.Sprintf("KV store host") |
| 159 | flag.StringVar(&(so.KVStoreHost), "kv_store_host", defaultKvstorehost, help) |
| 160 | |
| 161 | help = fmt.Sprintf("KV store port") |
| 162 | flag.IntVar(&(so.KVStorePort), "kv_store_port", defaultKvstoreport, help) |
| 163 | |
| 164 | help = fmt.Sprintf("Log level") |
| 165 | flag.StringVar(&(so.LogLevel), "log_level", defaultLoglevel, help) |
| 166 | |
| 167 | help = fmt.Sprintf("Number of ONUs") |
| 168 | flag.IntVar(&(so.OnuNumber), "onu_number", defaultOnunumber, help) |
| 169 | |
| 170 | help = fmt.Sprintf("Show startup banner log lines") |
| 171 | flag.BoolVar(&(so.Banner), "banner", defaultBanner, help) |
| 172 | |
| 173 | help = fmt.Sprintf("Show version information and exit") |
| 174 | flag.BoolVar(&(so.DisplayVersionOnly), "version", defaultDisplayVersionOnly, help) |
| 175 | |
mpagenko | dff5dda | 2020-08-28 11:52:01 +0000 | [diff] [blame] | 176 | help = fmt.Sprintf("Acceptance of incremental EVTOCD configuration") |
| 177 | flag.BoolVar(&(so.AccIncrEvto), "accept_incr_evto", defaultAccIncrEvto, help) |
| 178 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 179 | help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 180 | flag.StringVar(&(so.ProbeHost), "probe_host", defaultProbeHost, help) |
| 181 | |
| 182 | help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 183 | flag.IntVar(&(so.ProbePort), "probe_port", defaultProbePort, help) |
| 184 | |
| 185 | help = fmt.Sprintf("Number of seconds for the default liveliness check") |
| 186 | flag.DurationVar(&(so.LiveProbeInterval), "live_probe_interval", defaultLiveProbeInterval, help) |
| 187 | |
| 188 | help = fmt.Sprintf("Number of seconds for liveliness check if probe is not running") |
| 189 | flag.DurationVar(&(so.NotLiveProbeInterval), "not_live_probe_interval", defaultNotLiveProbeInterval, help) |
| 190 | |
| 191 | help = fmt.Sprintf("Number of seconds for heartbeat check interval.") |
| 192 | flag.DurationVar(&(so.HeartbeatCheckInterval), "hearbeat_check_interval", defaultHearbeatCheckInterval, help) |
| 193 | |
| 194 | help = fmt.Sprintf("Number of seconds adapter has to wait before reporting core on the hearbeat check failure.") |
| 195 | flag.DurationVar(&(so.HeartbeatFailReportInterval), "hearbeat_fail_interval", defaultHearbeatFailReportInterval, help) |
| 196 | |
| 197 | help = fmt.Sprintf("Number of retries to connect to Kafka.") |
| 198 | flag.IntVar(&(so.KafkaReconnectRetries), "kafka_reconnect_retries", defaultKafkaReconnectRetries, help) |
| 199 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 200 | help = "Replica number of this particular instance (default: %s)" |
| 201 | flag.IntVar(&(so.CurrentReplica), "current_replica", defaultCurrentReplica, help) |
| 202 | |
| 203 | help = "Total number of instances for this adapter" |
| 204 | flag.IntVar(&(so.TotalReplicas), "total_replica", defaultTotalReplicas, help) |
| 205 | |
Himani Chawla | d96df18 | 2020-09-28 11:12:02 +0530 | [diff] [blame^] | 206 | help = fmt.Sprintf("Maximum Number of seconds for the default interadapter communication timeout") |
| 207 | flag.DurationVar(&(so.MaxTimeoutInterAdapterComm), "max_timeout_interadapter_comm", |
| 208 | defaultMaxTimeoutInterAdapterComm, help) |
| 209 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 210 | flag.Parse() |
| 211 | containerName := getContainerInfo() |
| 212 | if len(containerName) > 0 { |
| 213 | so.InstanceID = containerName |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |
| 218 | func getContainerInfo() string { |
| 219 | return os.Getenv("HOSTNAME") |
| 220 | } |