Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 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 |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 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. |
| 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 | f8424cc | 2019-10-18 11:26:50 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/pkg/log" |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 24 | "os" |
| 25 | ) |
| 26 | |
| 27 | // Open OLT default constants |
| 28 | const ( |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 29 | EtcdStoreName = "etcd" |
| 30 | defaultInstanceid = "openOlt001" |
| 31 | defaultKafkaadapterhost = "127.0.0.1" |
| 32 | defaultKafkaadapterport = 9092 |
| 33 | defaultKafkaclusterhost = "127.0.0.1" |
| 34 | defaultKafkaclusterport = 9094 |
| 35 | defaultKvstoretype = EtcdStoreName |
| 36 | defaultKvstoretimeout = 5 //in seconds |
| 37 | defaultKvstorehost = "127.0.0.1" |
| 38 | defaultKvstoreport = 2379 // Consul = 8500; Etcd = 2379 |
| 39 | defaultLoglevel = 0 |
| 40 | defaultBanner = false |
| 41 | defaultDisplayVersionOnly = false |
| 42 | defaultTopic = "openolt" |
| 43 | defaultCoretopic = "rwcore" |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 44 | defaultEventtopic = "voltha.events" |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 45 | defaultOnunumber = 1 |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 46 | defaultProbeHost = "" |
| 47 | defaultProbePort = 8080 |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 48 | ) |
| 49 | |
| 50 | // AdapterFlags represents the set of configurations used by the read-write adaptercore service |
| 51 | type AdapterFlags struct { |
| 52 | // Command line parameters |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 53 | InstanceID string |
| 54 | KafkaAdapterHost string |
| 55 | KafkaAdapterPort int |
| 56 | KafkaClusterHost string |
| 57 | KafkaClusterPort int |
| 58 | KVStoreType string |
| 59 | KVStoreTimeout int // in seconds |
| 60 | KVStoreHost string |
| 61 | KVStorePort int |
| 62 | Topic string |
| 63 | CoreTopic string |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 64 | EventTopic string |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 65 | LogLevel int |
| 66 | OnuNumber int |
| 67 | Banner bool |
| 68 | DisplayVersionOnly bool |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 69 | ProbeHost string |
| 70 | ProbePort int |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | func init() { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 74 | _, _ = log.AddPackage(log.JSON, log.WarnLevel, nil) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 77 | // NewAdapterFlags returns a new RWCore config |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 78 | func NewAdapterFlags() *AdapterFlags { |
| 79 | var adapterFlags = AdapterFlags{ // Default values |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 80 | InstanceID: defaultInstanceid, |
| 81 | KafkaAdapterHost: defaultKafkaadapterhost, |
| 82 | KafkaAdapterPort: defaultKafkaadapterport, |
| 83 | KafkaClusterHost: defaultKafkaclusterhost, |
| 84 | KafkaClusterPort: defaultKafkaclusterport, |
| 85 | KVStoreType: defaultKvstoretype, |
| 86 | KVStoreTimeout: defaultKvstoretimeout, |
| 87 | KVStoreHost: defaultKvstorehost, |
| 88 | KVStorePort: defaultKvstoreport, |
| 89 | Topic: defaultTopic, |
| 90 | CoreTopic: defaultCoretopic, |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 91 | EventTopic: defaultEventtopic, |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 92 | LogLevel: defaultLoglevel, |
| 93 | OnuNumber: defaultOnunumber, |
| 94 | Banner: defaultBanner, |
| 95 | DisplayVersionOnly: defaultDisplayVersionOnly, |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 96 | ProbeHost: defaultProbeHost, |
| 97 | ProbePort: defaultProbePort, |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 98 | } |
| 99 | return &adapterFlags |
| 100 | } |
| 101 | |
| 102 | // ParseCommandArguments parses the arguments when running read-write adaptercore service |
| 103 | func (so *AdapterFlags) ParseCommandArguments() { |
| 104 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 105 | help := fmt.Sprintf("Kafka - Adapter messaging host") |
| 106 | flag.StringVar(&(so.KafkaAdapterHost), "kafka_adapter_host", defaultKafkaadapterhost, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 107 | |
| 108 | help = fmt.Sprintf("Kafka - Adapter messaging port") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 109 | flag.IntVar(&(so.KafkaAdapterPort), "kafka_adapter_port", defaultKafkaadapterport, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 110 | |
| 111 | help = fmt.Sprintf("Kafka - Cluster messaging host") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 112 | flag.StringVar(&(so.KafkaClusterHost), "kafka_cluster_host", defaultKafkaclusterhost, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 113 | |
| 114 | help = fmt.Sprintf("Kafka - Cluster messaging port") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 115 | flag.IntVar(&(so.KafkaClusterPort), "kafka_cluster_port", defaultKafkaclusterport, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 116 | |
| 117 | help = fmt.Sprintf("Open OLT topic") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 118 | flag.StringVar(&(so.Topic), "adapter_topic", defaultTopic, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 119 | |
| 120 | help = fmt.Sprintf("Core topic") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 121 | flag.StringVar(&(so.CoreTopic), "core_topic", defaultCoretopic, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 122 | |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 123 | help = fmt.Sprintf("Event topic") |
| 124 | flag.StringVar(&(so.EventTopic), "event_topic", defaultEventtopic, help) |
| 125 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 126 | help = fmt.Sprintf("KV store type") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 127 | flag.StringVar(&(so.KVStoreType), "kv_store_type", defaultKvstoretype, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 128 | |
| 129 | help = fmt.Sprintf("The default timeout when making a kv store request") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 130 | flag.IntVar(&(so.KVStoreTimeout), "kv_store_request_timeout", defaultKvstoretimeout, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 131 | |
| 132 | help = fmt.Sprintf("KV store host") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 133 | flag.StringVar(&(so.KVStoreHost), "kv_store_host", defaultKvstorehost, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 134 | |
| 135 | help = fmt.Sprintf("KV store port") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 136 | flag.IntVar(&(so.KVStorePort), "kv_store_port", defaultKvstoreport, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 137 | |
| 138 | help = fmt.Sprintf("Log level") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 139 | flag.IntVar(&(so.LogLevel), "log_level", defaultLoglevel, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 140 | |
| 141 | help = fmt.Sprintf("Number of ONUs") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 142 | flag.IntVar(&(so.OnuNumber), "onu_number", defaultOnunumber, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 143 | |
| 144 | help = fmt.Sprintf("Show startup banner log lines") |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 145 | flag.BoolVar(&(so.Banner), "banner", defaultBanner, help) |
| 146 | |
| 147 | help = fmt.Sprintf("Show version information and exit") |
| 148 | flag.BoolVar(&(so.DisplayVersionOnly), "version", defaultDisplayVersionOnly, help) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 149 | |
Rohan Agrawal | 828bf4e | 2019-10-22 10:13:19 +0000 | [diff] [blame] | 150 | help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 151 | flag.StringVar(&(so.ProbeHost), "probe_host", defaultProbeHost, help) |
| 152 | |
| 153 | help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 154 | flag.IntVar(&(so.ProbePort), "probe_port", defaultProbePort, help) |
| 155 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 156 | flag.Parse() |
| 157 | |
| 158 | containerName := getContainerInfo() |
| 159 | if len(containerName) > 0 { |
| 160 | so.InstanceID = containerName |
| 161 | } |
| 162 | |
| 163 | } |
| 164 | |
| 165 | func getContainerInfo() string { |
| 166 | return os.Getenv("HOSTNAME") |
| 167 | } |