blob: 06e532203f8b409d9c1a42090205087c154b2085 [file] [log] [blame]
Girish Gowdru0c588b22019-04-23 23:24:56 -04001/*
cbabu116b73f2019-12-10 17:56:32 +05302* Copyright 2018-present Open Networking Foundation
Girish Gowdru0c588b22019-04-23 23:24:56 -04003
cbabu116b73f2019-12-10 17:56:32 +05304* 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
Girish Gowdru0c588b22019-04-23 23:24:56 -04007
cbabu116b73f2019-12-10 17:56:32 +05308* http://www.apache.org/licenses/LICENSE-2.0
Girish Gowdru0c588b22019-04-23 23:24:56 -04009
cbabu116b73f2019-12-10 17:56:32 +053010* 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.
Girish Gowdru0c588b22019-04-23 23:24:56 -040015 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070016
17//Package config provides the Log, kvstore, Kafka configuration
Girish Gowdru0c588b22019-04-23 23:24:56 -040018package config
19
20import (
21 "flag"
22 "fmt"
Girish Gowdru0c588b22019-04-23 23:24:56 -040023 "os"
cbabu116b73f2019-12-10 17:56:32 +053024 "time"
Girish Gowdru0c588b22019-04-23 23:24:56 -040025)
26
27// Open OLT default constants
28const (
cbabu116b73f2019-12-10 17:56:32 +053029 EtcdStoreName = "etcd"
30 defaultInstanceid = "openOlt001"
31 defaultKafkaadapterhost = "127.0.0.1"
32 defaultKafkaadapterport = 9092
33 defaultKafkaclusterhost = "127.0.0.1"
34 defaultKafkaclusterport = 9094
35 defaultKvstoretype = EtcdStoreName
36 defaultKvstoretimeout = 5 //in seconds
37 defaultKvstorehost = "127.0.0.1"
38 defaultKvstoreport = 2379 // Consul = 8500; Etcd = 2379
David Bainbridge2b3d4882020-03-17 15:06:39 -070039 defaultLoglevel = "WARN"
cbabu116b73f2019-12-10 17:56:32 +053040 defaultBanner = false
41 defaultDisplayVersionOnly = false
42 defaultTopic = "openolt"
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
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053050 //defaultHearbeatFailReportInterval is the time in seconds the adapter will keep checking the hardware for heartbeat.
Chaitrashree G Sa4649252020-03-11 21:24:11 -040051 defaultHearbeatCheckInterval = 15 * time.Second
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053052 // defaultHearbeatFailReportInterval is the time adapter will wait before updating the state to the core.
Chaitrashree G Sa4649252020-03-11 21:24:11 -040053 defaultHearbeatFailReportInterval = 0 * time.Second
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053054 //defaultGrpcTimeoutInterval is the time in seconds a grpc call will wait before returning error.
55 defaultGrpcTimeoutInterval = 2 * time.Second
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070056 defaultCurrentReplica = 1
57 defaultTotalReplicas = 1
Girish Gowdru0c588b22019-04-23 23:24:56 -040058)
59
60// AdapterFlags represents the set of configurations used by the read-write adaptercore service
61type AdapterFlags struct {
62 // Command line parameters
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053063 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
Rohan Agrawal2488f192020-01-31 09:26:55 +000075 LogLevel string
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053076 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 GrpcTimeoutInterval time.Duration
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070086 CurrentReplica int
87 TotalReplicas int
Girish Gowdru0c588b22019-04-23 23:24:56 -040088}
89
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070090// NewAdapterFlags returns a new RWCore config
Girish Gowdru0c588b22019-04-23 23:24:56 -040091func NewAdapterFlags() *AdapterFlags {
92 var adapterFlags = AdapterFlags{ // Default values
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053093 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 GrpcTimeoutInterval: defaultGrpcTimeoutInterval,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400116 }
117 return &adapterFlags
118}
119
120// ParseCommandArguments parses the arguments when running read-write adaptercore service
121func (so *AdapterFlags) ParseCommandArguments() {
122
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700123 help := fmt.Sprintf("Kafka - Adapter messaging host")
124 flag.StringVar(&(so.KafkaAdapterHost), "kafka_adapter_host", defaultKafkaadapterhost, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400125
126 help = fmt.Sprintf("Kafka - Adapter messaging port")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700127 flag.IntVar(&(so.KafkaAdapterPort), "kafka_adapter_port", defaultKafkaadapterport, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400128
129 help = fmt.Sprintf("Kafka - Cluster messaging host")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700130 flag.StringVar(&(so.KafkaClusterHost), "kafka_cluster_host", defaultKafkaclusterhost, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400131
132 help = fmt.Sprintf("Kafka - Cluster messaging port")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700133 flag.IntVar(&(so.KafkaClusterPort), "kafka_cluster_port", defaultKafkaclusterport, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400134
135 help = fmt.Sprintf("Open OLT topic")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700136 flag.StringVar(&(so.Topic), "adapter_topic", defaultTopic, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400137
138 help = fmt.Sprintf("Core topic")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700139 flag.StringVar(&(so.CoreTopic), "core_topic", defaultCoretopic, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400140
Devmalya Paulfb990a52019-07-09 10:01:49 -0400141 help = fmt.Sprintf("Event topic")
142 flag.StringVar(&(so.EventTopic), "event_topic", defaultEventtopic, help)
143
Girish Gowdru0c588b22019-04-23 23:24:56 -0400144 help = fmt.Sprintf("KV store type")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700145 flag.StringVar(&(so.KVStoreType), "kv_store_type", defaultKvstoretype, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400146
147 help = fmt.Sprintf("The default timeout when making a kv store request")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700148 flag.IntVar(&(so.KVStoreTimeout), "kv_store_request_timeout", defaultKvstoretimeout, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400149
150 help = fmt.Sprintf("KV store host")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700151 flag.StringVar(&(so.KVStoreHost), "kv_store_host", defaultKvstorehost, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400152
153 help = fmt.Sprintf("KV store port")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700154 flag.IntVar(&(so.KVStorePort), "kv_store_port", defaultKvstoreport, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400155
156 help = fmt.Sprintf("Log level")
Rohan Agrawal2488f192020-01-31 09:26:55 +0000157 flag.StringVar(&(so.LogLevel), "log_level", defaultLoglevel, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400158
159 help = fmt.Sprintf("Number of ONUs")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700160 flag.IntVar(&(so.OnuNumber), "onu_number", defaultOnunumber, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400161
162 help = fmt.Sprintf("Show startup banner log lines")
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400163 flag.BoolVar(&(so.Banner), "banner", defaultBanner, help)
164
165 help = fmt.Sprintf("Show version information and exit")
166 flag.BoolVar(&(so.DisplayVersionOnly), "version", defaultDisplayVersionOnly, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400167
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000168 help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.")
169 flag.StringVar(&(so.ProbeHost), "probe_host", defaultProbeHost, help)
170
171 help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.")
172 flag.IntVar(&(so.ProbePort), "probe_port", defaultProbePort, help)
173
cbabu116b73f2019-12-10 17:56:32 +0530174 help = fmt.Sprintf("Number of seconds for the default liveliness check")
175 flag.DurationVar(&(so.LiveProbeInterval), "live_probe_interval", defaultLiveProbeInterval, help)
176
177 help = fmt.Sprintf("Number of seconds for liveliness check if probe is not running")
178 flag.DurationVar(&(so.NotLiveProbeInterval), "not_live_probe_interval", defaultNotLiveProbeInterval, help)
179
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530180 help = fmt.Sprintf("Number of seconds for heartbeat check interval.")
181 flag.DurationVar(&(so.HeartbeatCheckInterval), "hearbeat_check_interval", defaultHearbeatCheckInterval, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400182
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530183 help = fmt.Sprintf("Number of seconds adapter has to wait before reporting core on the hearbeat check failure.")
184 flag.DurationVar(&(so.HeartbeatFailReportInterval), "hearbeat_fail_interval", defaultHearbeatFailReportInterval, help)
185
186 help = fmt.Sprintf("Number of seconds for GRPC timeout.")
187 flag.DurationVar(&(so.GrpcTimeoutInterval), "grpc_timeout_interval", defaultGrpcTimeoutInterval, help)
188
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700189 help = "Replica number of this particular instance (default: %s)"
190 flag.IntVar(&(so.CurrentReplica), "current_replica", defaultCurrentReplica, help)
191
192 help = "Total number of instances for this adapter"
193 flag.IntVar(&(so.TotalReplicas), "total_replica", defaultTotalReplicas, help)
194
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530195 flag.Parse()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400196 containerName := getContainerInfo()
197 if len(containerName) > 0 {
198 so.InstanceID = containerName
199 }
200
201}
202
203func getContainerInfo() string {
204 return os.Getenv("HOSTNAME")
205}