blob: d5752317466169e959fe26f3aed2a8c0d03c3f00 [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
Girish Gowdru0c588b22019-04-23 23:24:56 -040051)
52
53// AdapterFlags represents the set of configurations used by the read-write adaptercore service
54type AdapterFlags struct {
55 // Command line parameters
cbabu116b73f2019-12-10 17:56:32 +053056 InstanceID string
57 KafkaAdapterHost string
58 KafkaAdapterPort int
59 KafkaClusterHost string
60 KafkaClusterPort int
61 KVStoreType string
62 KVStoreTimeout int // in seconds
63 KVStoreHost string
64 KVStorePort int
65 Topic string
66 CoreTopic string
67 EventTopic string
68 LogLevel int
69 OnuNumber int
70 Banner bool
71 DisplayVersionOnly bool
72 ProbeHost string
73 ProbePort int
74 LiveProbeInterval time.Duration
75 NotLiveProbeInterval time.Duration
Girish Gowdru0c588b22019-04-23 23:24:56 -040076}
77
78func init() {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070079 _, _ = log.AddPackage(log.JSON, log.WarnLevel, nil)
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
cbabu116b73f2019-12-10 17:56:32 +053085 InstanceID: defaultInstanceid,
86 KafkaAdapterHost: defaultKafkaadapterhost,
87 KafkaAdapterPort: defaultKafkaadapterport,
88 KafkaClusterHost: defaultKafkaclusterhost,
89 KafkaClusterPort: defaultKafkaclusterport,
90 KVStoreType: defaultKvstoretype,
91 KVStoreTimeout: defaultKvstoretimeout,
92 KVStoreHost: defaultKvstorehost,
93 KVStorePort: defaultKvstoreport,
94 Topic: defaultTopic,
95 CoreTopic: defaultCoretopic,
96 EventTopic: defaultEventtopic,
97 LogLevel: defaultLoglevel,
98 OnuNumber: defaultOnunumber,
99 Banner: defaultBanner,
100 DisplayVersionOnly: defaultDisplayVersionOnly,
101 ProbeHost: defaultProbeHost,
102 ProbePort: defaultProbePort,
103 LiveProbeInterval: defaultLiveProbeInterval,
104 NotLiveProbeInterval: defaultNotLiveProbeInterval,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400105 }
106 return &adapterFlags
107}
108
109// ParseCommandArguments parses the arguments when running read-write adaptercore service
110func (so *AdapterFlags) ParseCommandArguments() {
111
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700112 help := fmt.Sprintf("Kafka - Adapter messaging host")
113 flag.StringVar(&(so.KafkaAdapterHost), "kafka_adapter_host", defaultKafkaadapterhost, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400114
115 help = fmt.Sprintf("Kafka - Adapter messaging port")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700116 flag.IntVar(&(so.KafkaAdapterPort), "kafka_adapter_port", defaultKafkaadapterport, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400117
118 help = fmt.Sprintf("Kafka - Cluster messaging host")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700119 flag.StringVar(&(so.KafkaClusterHost), "kafka_cluster_host", defaultKafkaclusterhost, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400120
121 help = fmt.Sprintf("Kafka - Cluster messaging port")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700122 flag.IntVar(&(so.KafkaClusterPort), "kafka_cluster_port", defaultKafkaclusterport, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400123
124 help = fmt.Sprintf("Open OLT topic")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700125 flag.StringVar(&(so.Topic), "adapter_topic", defaultTopic, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400126
127 help = fmt.Sprintf("Core topic")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700128 flag.StringVar(&(so.CoreTopic), "core_topic", defaultCoretopic, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400129
Devmalya Paulfb990a52019-07-09 10:01:49 -0400130 help = fmt.Sprintf("Event topic")
131 flag.StringVar(&(so.EventTopic), "event_topic", defaultEventtopic, help)
132
Girish Gowdru0c588b22019-04-23 23:24:56 -0400133 help = fmt.Sprintf("KV store type")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700134 flag.StringVar(&(so.KVStoreType), "kv_store_type", defaultKvstoretype, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400135
136 help = fmt.Sprintf("The default timeout when making a kv store request")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700137 flag.IntVar(&(so.KVStoreTimeout), "kv_store_request_timeout", defaultKvstoretimeout, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400138
139 help = fmt.Sprintf("KV store host")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700140 flag.StringVar(&(so.KVStoreHost), "kv_store_host", defaultKvstorehost, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400141
142 help = fmt.Sprintf("KV store port")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700143 flag.IntVar(&(so.KVStorePort), "kv_store_port", defaultKvstoreport, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400144
145 help = fmt.Sprintf("Log level")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700146 flag.IntVar(&(so.LogLevel), "log_level", defaultLoglevel, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147
148 help = fmt.Sprintf("Number of ONUs")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700149 flag.IntVar(&(so.OnuNumber), "onu_number", defaultOnunumber, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400150
151 help = fmt.Sprintf("Show startup banner log lines")
Matt Jeanneretf880eb62019-07-16 20:08:03 -0400152 flag.BoolVar(&(so.Banner), "banner", defaultBanner, help)
153
154 help = fmt.Sprintf("Show version information and exit")
155 flag.BoolVar(&(so.DisplayVersionOnly), "version", defaultDisplayVersionOnly, help)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400156
Rohan Agrawal828bf4e2019-10-22 10:13:19 +0000157 help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.")
158 flag.StringVar(&(so.ProbeHost), "probe_host", defaultProbeHost, help)
159
160 help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.")
161 flag.IntVar(&(so.ProbePort), "probe_port", defaultProbePort, help)
162
cbabu116b73f2019-12-10 17:56:32 +0530163 help = fmt.Sprintf("Number of seconds for the default liveliness check")
164 flag.DurationVar(&(so.LiveProbeInterval), "live_probe_interval", defaultLiveProbeInterval, help)
165
166 help = fmt.Sprintf("Number of seconds for liveliness check if probe is not running")
167 flag.DurationVar(&(so.NotLiveProbeInterval), "not_live_probe_interval", defaultNotLiveProbeInterval, help)
168
Girish Gowdru0c588b22019-04-23 23:24:56 -0400169 flag.Parse()
170
171 containerName := getContainerInfo()
172 if len(containerName) > 0 {
173 so.InstanceID = containerName
174 }
175
176}
177
178func getContainerInfo() string {
179 return os.Getenv("HOSTNAME")
180}