blob: 3a8d15713aa772376c0e3a7c4eddd80aa4c1843e [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"
Neha Sharma3f221ae2020-04-29 19:02:12 +000031 defaultKafkaadapteraddress = "127.0.0.1:9092"
32 defaultKafkaclusteraddress = "127.0.0.1:9094"
cbabu116b73f2019-12-10 17:56:32 +053033 defaultKvstoretype = EtcdStoreName
Neha Sharma3f221ae2020-04-29 19:02:12 +000034 defaultKvstoretimeout = 5 * time.Second
35 defaultKvstoreaddress = "127.0.0.1:2379" // Port: Consul = 8500; Etcd = 2379
David Bainbridge2b3d4882020-03-17 15:06:39 -070036 defaultLoglevel = "WARN"
cbabu116b73f2019-12-10 17:56:32 +053037 defaultBanner = false
38 defaultDisplayVersionOnly = false
39 defaultTopic = "openolt"
40 defaultCoretopic = "rwcore"
41 defaultEventtopic = "voltha.events"
42 defaultOnunumber = 1
Neha Sharma3f221ae2020-04-29 19:02:12 +000043 defaultProbeAddress = ":8080"
cbabu116b73f2019-12-10 17:56:32 +053044 defaultLiveProbeInterval = 60 * time.Second
45 defaultNotLiveProbeInterval = 5 * time.Second // Probe more frequently when not alive
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053046 //defaultHearbeatFailReportInterval is the time in seconds the adapter will keep checking the hardware for heartbeat.
Chaitrashree G Sa4649252020-03-11 21:24:11 -040047 defaultHearbeatCheckInterval = 15 * time.Second
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053048 // defaultHearbeatFailReportInterval is the time adapter will wait before updating the state to the core.
Chaitrashree G Sa4649252020-03-11 21:24:11 -040049 defaultHearbeatFailReportInterval = 0 * time.Second
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053050 //defaultGrpcTimeoutInterval is the time in seconds a grpc call will wait before returning error.
51 defaultGrpcTimeoutInterval = 2 * time.Second
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070052 defaultCurrentReplica = 1
53 defaultTotalReplicas = 1
Girish Gowdru0c588b22019-04-23 23:24:56 -040054)
55
56// AdapterFlags represents the set of configurations used by the read-write adaptercore service
57type AdapterFlags struct {
58 // Command line parameters
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053059 InstanceID string
Neha Sharma3f221ae2020-04-29 19:02:12 +000060 KafkaAdapterAddress string
61 KafkaClusterAddress string
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053062 KVStoreType string
Neha Sharma3f221ae2020-04-29 19:02:12 +000063 KVStoreTimeout time.Duration
64 KVStoreAddress string
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053065 Topic string
66 CoreTopic string
67 EventTopic string
Rohan Agrawal2488f192020-01-31 09:26:55 +000068 LogLevel string
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053069 OnuNumber int
70 Banner bool
71 DisplayVersionOnly bool
Neha Sharma3f221ae2020-04-29 19:02:12 +000072 ProbeAddress string
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053073 LiveProbeInterval time.Duration
74 NotLiveProbeInterval time.Duration
75 HeartbeatCheckInterval time.Duration
76 HeartbeatFailReportInterval time.Duration
77 GrpcTimeoutInterval time.Duration
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070078 CurrentReplica int
79 TotalReplicas int
Girish Gowdru0c588b22019-04-23 23:24:56 -040080}
81
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070082// NewAdapterFlags returns a new RWCore config
Girish Gowdru0c588b22019-04-23 23:24:56 -040083func NewAdapterFlags() *AdapterFlags {
84 var adapterFlags = AdapterFlags{ // Default values
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053085 InstanceID: defaultInstanceid,
Neha Sharma3f221ae2020-04-29 19:02:12 +000086 KafkaAdapterAddress: defaultKafkaadapteraddress,
87 KafkaClusterAddress: defaultKafkaclusteraddress,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053088 KVStoreType: defaultKvstoretype,
89 KVStoreTimeout: defaultKvstoretimeout,
Neha Sharma3f221ae2020-04-29 19:02:12 +000090 KVStoreAddress: defaultKvstoreaddress,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053091 Topic: defaultTopic,
92 CoreTopic: defaultCoretopic,
93 EventTopic: defaultEventtopic,
94 LogLevel: defaultLoglevel,
95 OnuNumber: defaultOnunumber,
96 Banner: defaultBanner,
97 DisplayVersionOnly: defaultDisplayVersionOnly,
Neha Sharma3f221ae2020-04-29 19:02:12 +000098 ProbeAddress: defaultProbeAddress,
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053099 LiveProbeInterval: defaultLiveProbeInterval,
100 NotLiveProbeInterval: defaultNotLiveProbeInterval,
101 HeartbeatCheckInterval: defaultHearbeatCheckInterval,
102 HeartbeatFailReportInterval: defaultHearbeatFailReportInterval,
103 GrpcTimeoutInterval: defaultGrpcTimeoutInterval,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400104 }
105 return &adapterFlags
106}
107
108// ParseCommandArguments parses the arguments when running read-write adaptercore service
109func (so *AdapterFlags) ParseCommandArguments() {
110
Neha Sharma3f221ae2020-04-29 19:02:12 +0000111 help := fmt.Sprintf("Kafka - Adapter messaging address")
112 flag.StringVar(&(so.KafkaAdapterAddress), "kafka_adapter_address", defaultKafkaadapteraddress, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400113
Neha Sharma3f221ae2020-04-29 19:02:12 +0000114 help = fmt.Sprintf("Kafka - Cluster messaging address")
115 flag.StringVar(&(so.KafkaClusterAddress), "kafka_cluster_address", defaultKafkaclusteraddress, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400116
117 help = fmt.Sprintf("Open OLT topic")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700118 flag.StringVar(&(so.Topic), "adapter_topic", defaultTopic, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400119
120 help = fmt.Sprintf("Core topic")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700121 flag.StringVar(&(so.CoreTopic), "core_topic", defaultCoretopic, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400122
Devmalya Paulfb990a52019-07-09 10:01:49 -0400123 help = fmt.Sprintf("Event topic")
124 flag.StringVar(&(so.EventTopic), "event_topic", defaultEventtopic, help)
125
Girish Gowdru0c588b22019-04-23 23:24:56 -0400126 help = fmt.Sprintf("KV store type")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700127 flag.StringVar(&(so.KVStoreType), "kv_store_type", defaultKvstoretype, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400128
129 help = fmt.Sprintf("The default timeout when making a kv store request")
Neha Sharmacc656962020-04-14 14:26:11 +0000130 flag.DurationVar(&(so.KVStoreTimeout), "kv_store_request_timeout", defaultKvstoretimeout, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400131
Neha Sharma3f221ae2020-04-29 19:02:12 +0000132 help = fmt.Sprintf("KV store address")
133 flag.StringVar(&(so.KVStoreAddress), "kv_store_address", defaultKvstoreaddress, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400134
135 help = fmt.Sprintf("Log level")
Rohan Agrawal2488f192020-01-31 09:26:55 +0000136 flag.StringVar(&(so.LogLevel), "log_level", defaultLoglevel, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400137
138 help = fmt.Sprintf("Number of ONUs")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700139 flag.IntVar(&(so.OnuNumber), "onu_number", defaultOnunumber, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400140
141 help = fmt.Sprintf("Show startup banner log lines")
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400142 flag.BoolVar(&(so.Banner), "banner", defaultBanner, help)
143
144 help = fmt.Sprintf("Show version information and exit")
145 flag.BoolVar(&(so.DisplayVersionOnly), "version", defaultDisplayVersionOnly, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400146
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000147 help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.")
Neha Sharma3f221ae2020-04-29 19:02:12 +0000148 flag.StringVar(&(so.ProbeAddress), "probe_address", defaultProbeAddress, help)
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000149
cbabu116b73f2019-12-10 17:56:32 +0530150 help = fmt.Sprintf("Number of seconds for the default liveliness check")
151 flag.DurationVar(&(so.LiveProbeInterval), "live_probe_interval", defaultLiveProbeInterval, help)
152
153 help = fmt.Sprintf("Number of seconds for liveliness check if probe is not running")
154 flag.DurationVar(&(so.NotLiveProbeInterval), "not_live_probe_interval", defaultNotLiveProbeInterval, help)
155
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530156 help = fmt.Sprintf("Number of seconds for heartbeat check interval.")
157 flag.DurationVar(&(so.HeartbeatCheckInterval), "hearbeat_check_interval", defaultHearbeatCheckInterval, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400158
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530159 help = fmt.Sprintf("Number of seconds adapter has to wait before reporting core on the hearbeat check failure.")
160 flag.DurationVar(&(so.HeartbeatFailReportInterval), "hearbeat_fail_interval", defaultHearbeatFailReportInterval, help)
161
162 help = fmt.Sprintf("Number of seconds for GRPC timeout.")
163 flag.DurationVar(&(so.GrpcTimeoutInterval), "grpc_timeout_interval", defaultGrpcTimeoutInterval, help)
164
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -0700165 help = "Replica number of this particular instance (default: %s)"
166 flag.IntVar(&(so.CurrentReplica), "current_replica", defaultCurrentReplica, help)
167
168 help = "Total number of instances for this adapter"
169 flag.IntVar(&(so.TotalReplicas), "total_replica", defaultTotalReplicas, help)
170
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530171 flag.Parse()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400172 containerName := getContainerInfo()
173 if len(containerName) > 0 {
174 so.InstanceID = containerName
175 }
176
177}
178
179func getContainerInfo() string {
180 return os.Getenv("HOSTNAME")
181}