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