blob: babd8ea2a7fcdd1ea0c035c5d04b36e66c7ce0b9 [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"
Scott Baker51290152019-10-24 14:23:20 -070023 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Girish Gowdru0c588b22019-04-23 23:24:56 -040024 "os"
cbabu116b73f2019-12-10 17:56:32 +053025 "time"
Girish Gowdru0c588b22019-04-23 23:24:56 -040026)
27
28// Open OLT default constants
29const (
cbabu116b73f2019-12-10 17:56:32 +053030 EtcdStoreName = "etcd"
31 defaultInstanceid = "openOlt001"
32 defaultKafkaadapterhost = "127.0.0.1"
33 defaultKafkaadapterport = 9092
34 defaultKafkaclusterhost = "127.0.0.1"
35 defaultKafkaclusterport = 9094
36 defaultKvstoretype = EtcdStoreName
37 defaultKvstoretimeout = 5 //in seconds
38 defaultKvstorehost = "127.0.0.1"
39 defaultKvstoreport = 2379 // Consul = 8500; Etcd = 2379
40 defaultLoglevel = 0
41 defaultBanner = false
42 defaultDisplayVersionOnly = false
43 defaultTopic = "openolt"
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
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053051 //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 //defaultGrpcTimeoutInterval is the time in seconds a grpc call will wait before returning error.
56 defaultGrpcTimeoutInterval = 2 * time.Second
Girish Gowdru0c588b22019-04-23 23:24:56 -040057)
58
59// AdapterFlags represents the set of configurations used by the read-write adaptercore service
60type AdapterFlags struct {
61 // Command line parameters
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053062 InstanceID string
63 KafkaAdapterHost string
64 KafkaAdapterPort int
65 KafkaClusterHost string
66 KafkaClusterPort int
67 KVStoreType string
68 KVStoreTimeout int // in seconds
69 KVStoreHost string
70 KVStorePort int
71 Topic string
72 CoreTopic string
73 EventTopic string
74 LogLevel int
75 OnuNumber int
76 Banner bool
77 DisplayVersionOnly bool
78 ProbeHost string
79 ProbePort int
80 LiveProbeInterval time.Duration
81 NotLiveProbeInterval time.Duration
82 HeartbeatCheckInterval time.Duration
83 HeartbeatFailReportInterval time.Duration
84 GrpcTimeoutInterval time.Duration
Girish Gowdru0c588b22019-04-23 23:24:56 -040085}
86
87func init() {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070088 _, _ = log.AddPackage(log.JSON, log.WarnLevel, nil)
Girish Gowdru0c588b22019-04-23 23:24:56 -040089}
90
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070091// NewAdapterFlags returns a new RWCore config
Girish Gowdru0c588b22019-04-23 23:24:56 -040092func NewAdapterFlags() *AdapterFlags {
93 var adapterFlags = AdapterFlags{ // Default values
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053094 InstanceID: defaultInstanceid,
95 KafkaAdapterHost: defaultKafkaadapterhost,
96 KafkaAdapterPort: defaultKafkaadapterport,
97 KafkaClusterHost: defaultKafkaclusterhost,
98 KafkaClusterPort: defaultKafkaclusterport,
99 KVStoreType: defaultKvstoretype,
100 KVStoreTimeout: defaultKvstoretimeout,
101 KVStoreHost: defaultKvstorehost,
102 KVStorePort: defaultKvstoreport,
103 Topic: defaultTopic,
104 CoreTopic: defaultCoretopic,
105 EventTopic: defaultEventtopic,
106 LogLevel: defaultLoglevel,
107 OnuNumber: defaultOnunumber,
108 Banner: defaultBanner,
109 DisplayVersionOnly: defaultDisplayVersionOnly,
110 ProbeHost: defaultProbeHost,
111 ProbePort: defaultProbePort,
112 LiveProbeInterval: defaultLiveProbeInterval,
113 NotLiveProbeInterval: defaultNotLiveProbeInterval,
114 HeartbeatCheckInterval: defaultHearbeatCheckInterval,
115 HeartbeatFailReportInterval: defaultHearbeatFailReportInterval,
116 GrpcTimeoutInterval: defaultGrpcTimeoutInterval,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400117 }
118 return &adapterFlags
119}
120
121// ParseCommandArguments parses the arguments when running read-write adaptercore service
122func (so *AdapterFlags) ParseCommandArguments() {
123
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700124 help := fmt.Sprintf("Kafka - Adapter messaging host")
125 flag.StringVar(&(so.KafkaAdapterHost), "kafka_adapter_host", defaultKafkaadapterhost, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400126
127 help = fmt.Sprintf("Kafka - Adapter messaging port")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700128 flag.IntVar(&(so.KafkaAdapterPort), "kafka_adapter_port", defaultKafkaadapterport, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400129
130 help = fmt.Sprintf("Kafka - Cluster messaging host")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700131 flag.StringVar(&(so.KafkaClusterHost), "kafka_cluster_host", defaultKafkaclusterhost, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400132
133 help = fmt.Sprintf("Kafka - Cluster messaging port")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700134 flag.IntVar(&(so.KafkaClusterPort), "kafka_cluster_port", defaultKafkaclusterport, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400135
136 help = fmt.Sprintf("Open OLT topic")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700137 flag.StringVar(&(so.Topic), "adapter_topic", defaultTopic, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400138
139 help = fmt.Sprintf("Core topic")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700140 flag.StringVar(&(so.CoreTopic), "core_topic", defaultCoretopic, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400141
Devmalya Paulfb990a52019-07-09 10:01:49 -0400142 help = fmt.Sprintf("Event topic")
143 flag.StringVar(&(so.EventTopic), "event_topic", defaultEventtopic, help)
144
Girish Gowdru0c588b22019-04-23 23:24:56 -0400145 help = fmt.Sprintf("KV store type")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700146 flag.StringVar(&(so.KVStoreType), "kv_store_type", defaultKvstoretype, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147
148 help = fmt.Sprintf("The default timeout when making a kv store request")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700149 flag.IntVar(&(so.KVStoreTimeout), "kv_store_request_timeout", defaultKvstoretimeout, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400150
151 help = fmt.Sprintf("KV store host")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700152 flag.StringVar(&(so.KVStoreHost), "kv_store_host", defaultKvstorehost, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400153
154 help = fmt.Sprintf("KV store port")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700155 flag.IntVar(&(so.KVStorePort), "kv_store_port", defaultKvstoreport, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400156
157 help = fmt.Sprintf("Log level")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700158 flag.IntVar(&(so.LogLevel), "log_level", defaultLoglevel, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400159
160 help = fmt.Sprintf("Number of ONUs")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700161 flag.IntVar(&(so.OnuNumber), "onu_number", defaultOnunumber, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400162
163 help = fmt.Sprintf("Show startup banner log lines")
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400164 flag.BoolVar(&(so.Banner), "banner", defaultBanner, help)
165
166 help = fmt.Sprintf("Show version information and exit")
167 flag.BoolVar(&(so.DisplayVersionOnly), "version", defaultDisplayVersionOnly, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400168
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000169 help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.")
170 flag.StringVar(&(so.ProbeHost), "probe_host", defaultProbeHost, help)
171
172 help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.")
173 flag.IntVar(&(so.ProbePort), "probe_port", defaultProbePort, help)
174
cbabu116b73f2019-12-10 17:56:32 +0530175 help = fmt.Sprintf("Number of seconds for the default liveliness check")
176 flag.DurationVar(&(so.LiveProbeInterval), "live_probe_interval", defaultLiveProbeInterval, help)
177
178 help = fmt.Sprintf("Number of seconds for liveliness check if probe is not running")
179 flag.DurationVar(&(so.NotLiveProbeInterval), "not_live_probe_interval", defaultNotLiveProbeInterval, help)
180
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530181 help = fmt.Sprintf("Number of seconds for heartbeat check interval.")
182 flag.DurationVar(&(so.HeartbeatCheckInterval), "hearbeat_check_interval", defaultHearbeatCheckInterval, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400183
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530184 help = fmt.Sprintf("Number of seconds adapter has to wait before reporting core on the hearbeat check failure.")
185 flag.DurationVar(&(so.HeartbeatFailReportInterval), "hearbeat_fail_interval", defaultHearbeatFailReportInterval, help)
186
187 help = fmt.Sprintf("Number of seconds for GRPC timeout.")
188 flag.DurationVar(&(so.GrpcTimeoutInterval), "grpc_timeout_interval", defaultGrpcTimeoutInterval, help)
189
190 flag.Parse()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 containerName := getContainerInfo()
192 if len(containerName) > 0 {
193 so.InstanceID = containerName
194 }
195
196}
197
198func getContainerInfo() string {
199 return os.Getenv("HOSTNAME")
200}