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 |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 51 | ) |
| 52 | |
| 53 | // AdapterFlags represents the set of configurations used by the read-write adaptercore service |
| 54 | type AdapterFlags struct { |
| 55 | // Command line parameters |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 56 | 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 Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func init() { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 79 | _, _ = log.AddPackage(log.JSON, log.WarnLevel, nil) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 82 | // NewAdapterFlags returns a new RWCore config |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 83 | func NewAdapterFlags() *AdapterFlags { |
| 84 | var adapterFlags = AdapterFlags{ // Default values |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 85 | 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 Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 105 | } |
| 106 | return &adapterFlags |
| 107 | } |
| 108 | |
| 109 | // ParseCommandArguments parses the arguments when running read-write adaptercore service |
| 110 | func (so *AdapterFlags) ParseCommandArguments() { |
| 111 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 112 | help := fmt.Sprintf("Kafka - Adapter messaging host") |
| 113 | flag.StringVar(&(so.KafkaAdapterHost), "kafka_adapter_host", defaultKafkaadapterhost, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 114 | |
| 115 | help = fmt.Sprintf("Kafka - Adapter messaging port") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 116 | flag.IntVar(&(so.KafkaAdapterPort), "kafka_adapter_port", defaultKafkaadapterport, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 117 | |
| 118 | help = fmt.Sprintf("Kafka - Cluster messaging host") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 119 | flag.StringVar(&(so.KafkaClusterHost), "kafka_cluster_host", defaultKafkaclusterhost, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 120 | |
| 121 | help = fmt.Sprintf("Kafka - Cluster messaging port") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 122 | flag.IntVar(&(so.KafkaClusterPort), "kafka_cluster_port", defaultKafkaclusterport, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 123 | |
| 124 | help = fmt.Sprintf("Open OLT topic") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 125 | flag.StringVar(&(so.Topic), "adapter_topic", defaultTopic, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 126 | |
| 127 | help = fmt.Sprintf("Core topic") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 128 | flag.StringVar(&(so.CoreTopic), "core_topic", defaultCoretopic, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 129 | |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 130 | help = fmt.Sprintf("Event topic") |
| 131 | flag.StringVar(&(so.EventTopic), "event_topic", defaultEventtopic, help) |
| 132 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 133 | help = fmt.Sprintf("KV store type") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 134 | flag.StringVar(&(so.KVStoreType), "kv_store_type", defaultKvstoretype, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 135 | |
| 136 | help = fmt.Sprintf("The default timeout when making a kv store request") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 137 | flag.IntVar(&(so.KVStoreTimeout), "kv_store_request_timeout", defaultKvstoretimeout, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 138 | |
| 139 | help = fmt.Sprintf("KV store host") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 140 | flag.StringVar(&(so.KVStoreHost), "kv_store_host", defaultKvstorehost, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 141 | |
| 142 | help = fmt.Sprintf("KV store port") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 143 | flag.IntVar(&(so.KVStorePort), "kv_store_port", defaultKvstoreport, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 144 | |
| 145 | help = fmt.Sprintf("Log level") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 146 | flag.IntVar(&(so.LogLevel), "log_level", defaultLoglevel, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 147 | |
| 148 | help = fmt.Sprintf("Number of ONUs") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 149 | flag.IntVar(&(so.OnuNumber), "onu_number", defaultOnunumber, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 150 | |
| 151 | help = fmt.Sprintf("Show startup banner log lines") |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 152 | 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 Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 156 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 157 | 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 | |
cbabu | 116b73f | 2019-12-10 17:56:32 +0530 | [diff] [blame] | 163 | 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 Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 169 | flag.Parse() |
| 170 | |
| 171 | containerName := getContainerInfo() |
| 172 | if len(containerName) > 0 { |
| 173 | so.InstanceID = containerName |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | func getContainerInfo() string { |
| 179 | return os.Getenv("HOSTNAME") |
| 180 | } |