Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1 | /* |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 3 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 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 |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 7 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 9 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 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. |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 15 | */ |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 16 | |
| 17 | //Package config provides the Log, kvstore, Kafka configuration |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 18 | package config |
| 19 | |
| 20 | import ( |
| 21 | "flag" |
| 22 | "fmt" |
Scott Baker | 5129015 | 2019-10-24 14:23:20 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 24 | "os" |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 25 | "time" |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // Open OLT default constants |
| 29 | const ( |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 30 | 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 Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 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 | //defaultGrpcTimeoutInterval is the time in seconds a grpc call will wait before returning error. |
| 56 | defaultGrpcTimeoutInterval = 2 * time.Second |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 57 | ) |
| 58 | |
| 59 | // AdapterFlags represents the set of configurations used by the read-write adaptercore service |
| 60 | type AdapterFlags struct { |
| 61 | // Command line parameters |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 62 | 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 Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | func init() { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 88 | _, _ = log.AddPackage(log.JSON, log.WarnLevel, nil) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 91 | // NewAdapterFlags returns a new RWCore config |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 92 | func NewAdapterFlags() *AdapterFlags { |
| 93 | var adapterFlags = AdapterFlags{ // Default values |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 94 | 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 Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 117 | } |
| 118 | return &adapterFlags |
| 119 | } |
| 120 | |
| 121 | // ParseCommandArguments parses the arguments when running read-write adaptercore service |
| 122 | func (so *AdapterFlags) ParseCommandArguments() { |
| 123 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 124 | help := fmt.Sprintf("Kafka - Adapter messaging host") |
| 125 | flag.StringVar(&(so.KafkaAdapterHost), "kafka_adapter_host", defaultKafkaadapterhost, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 126 | |
| 127 | help = fmt.Sprintf("Kafka - Adapter messaging port") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 128 | flag.IntVar(&(so.KafkaAdapterPort), "kafka_adapter_port", defaultKafkaadapterport, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 129 | |
| 130 | help = fmt.Sprintf("Kafka - Cluster messaging host") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 131 | flag.StringVar(&(so.KafkaClusterHost), "kafka_cluster_host", defaultKafkaclusterhost, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 132 | |
| 133 | help = fmt.Sprintf("Kafka - Cluster messaging port") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 134 | flag.IntVar(&(so.KafkaClusterPort), "kafka_cluster_port", defaultKafkaclusterport, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 135 | |
| 136 | help = fmt.Sprintf("Open OLT topic") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 137 | flag.StringVar(&(so.Topic), "adapter_topic", defaultTopic, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 138 | |
| 139 | help = fmt.Sprintf("Core topic") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 140 | flag.StringVar(&(so.CoreTopic), "core_topic", defaultCoretopic, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 141 | |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 142 | help = fmt.Sprintf("Event topic") |
| 143 | flag.StringVar(&(so.EventTopic), "event_topic", defaultEventtopic, help) |
| 144 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 145 | help = fmt.Sprintf("KV store type") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 146 | flag.StringVar(&(so.KVStoreType), "kv_store_type", defaultKvstoretype, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 147 | |
| 148 | help = fmt.Sprintf("The default timeout when making a kv store request") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 149 | flag.IntVar(&(so.KVStoreTimeout), "kv_store_request_timeout", defaultKvstoretimeout, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 150 | |
| 151 | help = fmt.Sprintf("KV store host") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 152 | flag.StringVar(&(so.KVStoreHost), "kv_store_host", defaultKvstorehost, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 153 | |
| 154 | help = fmt.Sprintf("KV store port") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 155 | flag.IntVar(&(so.KVStorePort), "kv_store_port", defaultKvstoreport, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 156 | |
| 157 | help = fmt.Sprintf("Log level") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 158 | flag.IntVar(&(so.LogLevel), "log_level", defaultLoglevel, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 159 | |
| 160 | help = fmt.Sprintf("Number of ONUs") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 161 | flag.IntVar(&(so.OnuNumber), "onu_number", defaultOnunumber, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 162 | |
| 163 | help = fmt.Sprintf("Show startup banner log lines") |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 164 | 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 Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 168 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 169 | 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 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 175 | 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 Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 181 | help = fmt.Sprintf("Number of seconds for heartbeat check interval.") |
| 182 | flag.DurationVar(&(so.HeartbeatCheckInterval), "hearbeat_check_interval", defaultHearbeatCheckInterval, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 183 | |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 184 | 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 Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 191 | containerName := getContainerInfo() |
| 192 | if len(containerName) > 0 { |
| 193 | so.InstanceID = containerName |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | |
| 198 | func getContainerInfo() string { |
| 199 | return os.Getenv("HOSTNAME") |
| 200 | } |