blob: d47245a34b49bcf678cb77a8425354eb5ded1a40 [file] [log] [blame]
Holger Hildebrandtfa074992020-03-27 15:42:06 +00001/*
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
18package config
19
20import (
21 "flag"
22 "fmt"
23 "os"
24 "time"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000025)
26
27// Open ONU default constants
28const (
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
mpagenkoaf801632020-07-03 10:00:42 +000036 defaultKvstoretimeout = 5 * time.Second
Holger Hildebrandtfa074992020-03-27 15:42:06 +000037 defaultKvstorehost = "localhost"
38 defaultKvstoreport = 2379 // Consul = 8500; Etcd = 2379
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000039 defaultLoglevel = "WARN"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000040 defaultBanner = false
41 defaultDisplayVersionOnly = false
mpagenkodff5dda2020-08-28 11:52:01 +000042 defaultAccIncrEvto = false
Holger Hildebrandtfa074992020-03-27 15:42:06 +000043 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.
56 defaultKafkaReconnectRetries = -1
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000057 defaultCurrentReplica = 1
58 defaultTotalReplicas = 1
Holger Hildebrandtfa074992020-03-27 15:42:06 +000059)
60
61// AdapterFlags represents the set of configurations used by the read-write adaptercore service
62type AdapterFlags struct {
63 // Command line parameters
64 InstanceID string
65 KafkaAdapterHost string
66 KafkaAdapterPort int
67 KafkaClusterHost string
68 KafkaClusterPort int
69 KVStoreType string
mpagenkoaf801632020-07-03 10:00:42 +000070 KVStoreTimeout time.Duration
Holger Hildebrandtfa074992020-03-27 15:42:06 +000071 KVStoreHost string
72 KVStorePort int
73 Topic string
74 CoreTopic string
75 EventTopic string
76 LogLevel string
77 OnuNumber int
78 Banner bool
79 DisplayVersionOnly bool
mpagenkodff5dda2020-08-28 11:52:01 +000080 AccIncrEvto bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000081 ProbeHost string
82 ProbePort int
83 LiveProbeInterval time.Duration
84 NotLiveProbeInterval time.Duration
85 HeartbeatCheckInterval time.Duration
86 HeartbeatFailReportInterval time.Duration
87 KafkaReconnectRetries int
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000088 CurrentReplica int
89 TotalReplicas int
Holger Hildebrandtfa074992020-03-27 15:42:06 +000090}
91
92// NewAdapterFlags returns a new RWCore config
93func NewAdapterFlags() *AdapterFlags {
94 var adapterFlags = AdapterFlags{ // Default values
95 InstanceID: defaultInstanceid,
96 KafkaAdapterHost: defaultKafkaadapterhost,
97 KafkaAdapterPort: defaultKafkaadapterport,
98 KafkaClusterHost: defaultKafkaclusterhost,
99 KafkaClusterPort: defaultKafkaclusterport,
100 KVStoreType: defaultKvstoretype,
101 KVStoreTimeout: defaultKvstoretimeout,
102 KVStoreHost: defaultKvstorehost,
103 KVStorePort: defaultKvstoreport,
104 Topic: defaultTopic,
105 CoreTopic: defaultCoretopic,
106 EventTopic: defaultEventtopic,
107 LogLevel: defaultLoglevel,
108 OnuNumber: defaultOnunumber,
109 Banner: defaultBanner,
110 DisplayVersionOnly: defaultDisplayVersionOnly,
mpagenkodff5dda2020-08-28 11:52:01 +0000111 AccIncrEvto: defaultAccIncrEvto,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000112 ProbeHost: defaultProbeHost,
113 ProbePort: defaultProbePort,
114 LiveProbeInterval: defaultLiveProbeInterval,
115 NotLiveProbeInterval: defaultNotLiveProbeInterval,
116 HeartbeatCheckInterval: defaultHearbeatCheckInterval,
117 HeartbeatFailReportInterval: defaultHearbeatFailReportInterval,
118 KafkaReconnectRetries: defaultKafkaReconnectRetries,
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000119 CurrentReplica: defaultCurrentReplica,
120 TotalReplicas: defaultTotalReplicas,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000121 }
122 return &adapterFlags
123}
124
125// ParseCommandArguments parses the arguments when running read-write adaptercore service
126func (so *AdapterFlags) ParseCommandArguments() {
127
128 help := fmt.Sprintf("Kafka - Adapter messaging host")
129 flag.StringVar(&(so.KafkaAdapterHost), "kafka_adapter_host", defaultKafkaadapterhost, help)
130
131 help = fmt.Sprintf("Kafka - Adapter messaging port")
132 flag.IntVar(&(so.KafkaAdapterPort), "kafka_adapter_port", defaultKafkaadapterport, help)
133
134 help = fmt.Sprintf("Kafka - Cluster messaging host")
135 flag.StringVar(&(so.KafkaClusterHost), "kafka_cluster_host", defaultKafkaclusterhost, help)
136
137 help = fmt.Sprintf("Kafka - Cluster messaging port")
138 flag.IntVar(&(so.KafkaClusterPort), "kafka_cluster_port", defaultKafkaclusterport, help)
139
140 help = fmt.Sprintf("Open ONU topic")
141 flag.StringVar(&(so.Topic), "adapter_topic", defaultTopic, help)
142
143 help = fmt.Sprintf("Core topic")
144 flag.StringVar(&(so.CoreTopic), "core_topic", defaultCoretopic, help)
145
146 help = fmt.Sprintf("Event topic")
147 flag.StringVar(&(so.EventTopic), "event_topic", defaultEventtopic, help)
148
149 help = fmt.Sprintf("KV store type")
150 flag.StringVar(&(so.KVStoreType), "kv_store_type", defaultKvstoretype, help)
151
152 help = fmt.Sprintf("The default timeout when making a kv store request")
mpagenkoaf801632020-07-03 10:00:42 +0000153 flag.DurationVar(&(so.KVStoreTimeout), "kv_store_request_timeout", defaultKvstoretimeout, help)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000154
155 help = fmt.Sprintf("KV store host")
156 flag.StringVar(&(so.KVStoreHost), "kv_store_host", defaultKvstorehost, help)
157
158 help = fmt.Sprintf("KV store port")
159 flag.IntVar(&(so.KVStorePort), "kv_store_port", defaultKvstoreport, help)
160
161 help = fmt.Sprintf("Log level")
162 flag.StringVar(&(so.LogLevel), "log_level", defaultLoglevel, help)
163
164 help = fmt.Sprintf("Number of ONUs")
165 flag.IntVar(&(so.OnuNumber), "onu_number", defaultOnunumber, help)
166
167 help = fmt.Sprintf("Show startup banner log lines")
168 flag.BoolVar(&(so.Banner), "banner", defaultBanner, help)
169
170 help = fmt.Sprintf("Show version information and exit")
171 flag.BoolVar(&(so.DisplayVersionOnly), "version", defaultDisplayVersionOnly, help)
172
mpagenkodff5dda2020-08-28 11:52:01 +0000173 help = fmt.Sprintf("Acceptance of incremental EVTOCD configuration")
174 flag.BoolVar(&(so.AccIncrEvto), "accept_incr_evto", defaultAccIncrEvto, help)
175
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000176 help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.")
177 flag.StringVar(&(so.ProbeHost), "probe_host", defaultProbeHost, help)
178
179 help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.")
180 flag.IntVar(&(so.ProbePort), "probe_port", defaultProbePort, help)
181
182 help = fmt.Sprintf("Number of seconds for the default liveliness check")
183 flag.DurationVar(&(so.LiveProbeInterval), "live_probe_interval", defaultLiveProbeInterval, help)
184
185 help = fmt.Sprintf("Number of seconds for liveliness check if probe is not running")
186 flag.DurationVar(&(so.NotLiveProbeInterval), "not_live_probe_interval", defaultNotLiveProbeInterval, help)
187
188 help = fmt.Sprintf("Number of seconds for heartbeat check interval.")
189 flag.DurationVar(&(so.HeartbeatCheckInterval), "hearbeat_check_interval", defaultHearbeatCheckInterval, help)
190
191 help = fmt.Sprintf("Number of seconds adapter has to wait before reporting core on the hearbeat check failure.")
192 flag.DurationVar(&(so.HeartbeatFailReportInterval), "hearbeat_fail_interval", defaultHearbeatFailReportInterval, help)
193
194 help = fmt.Sprintf("Number of retries to connect to Kafka.")
195 flag.IntVar(&(so.KafkaReconnectRetries), "kafka_reconnect_retries", defaultKafkaReconnectRetries, help)
196
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000197 help = "Replica number of this particular instance (default: %s)"
198 flag.IntVar(&(so.CurrentReplica), "current_replica", defaultCurrentReplica, help)
199
200 help = "Total number of instances for this adapter"
201 flag.IntVar(&(so.TotalReplicas), "total_replica", defaultTotalReplicas, help)
202
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000203 flag.Parse()
204 containerName := getContainerInfo()
205 if len(containerName) > 0 {
206 so.InstanceID = containerName
207 }
208
209}
210
211func getContainerInfo() string {
212 return os.Getenv("HOSTNAME")
213}