Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [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 | */ |
| 16 | package config |
| 17 | |
| 18 | import ( |
| 19 | "flag" |
| 20 | "fmt" |
Scott Baker | b61e333 | 2019-10-24 13:36:06 -0700 | [diff] [blame^] | 21 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 22 | "os" |
| 23 | ) |
| 24 | |
| 25 | // Simulated OLT default constants |
| 26 | const ( |
| 27 | EtcdStoreName = "etcd" |
| 28 | default_InstanceID = "simulatedOlt001" |
| 29 | default_KafkaAdapterHost = "127.0.0.1" |
| 30 | default_KafkaAdapterPort = 9092 |
| 31 | default_KafkaClusterHost = "127.0.0.1" |
| 32 | default_KafkaClusterPort = 9094 |
| 33 | default_KVStoreType = EtcdStoreName |
| 34 | default_KVStoreTimeout = 5 //in seconds |
| 35 | default_KVStoreHost = "127.0.0.1" |
| 36 | default_KVStorePort = 2379 // Consul = 8500; Etcd = 2379 |
| 37 | default_LogLevel = 0 |
| 38 | default_Banner = false |
| 39 | default_Topic = "simulated_olt" |
| 40 | default_CoreTopic = "rwcore" |
| 41 | default_OnuNumber = 1 |
Vignesh Ethiraj | 5620c67 | 2019-10-14 13:18:52 +0000 | [diff] [blame] | 42 | default_ProbeHost = "" |
| 43 | default_ProbePort = 8080 |
David Bainbridge | 8ea2c9d | 2019-10-22 21:35:22 +0000 | [diff] [blame] | 44 | default_PrintVersion = false |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 45 | ) |
| 46 | |
| 47 | // AdapterFlags represents the set of configurations used by the read-write adaptercore service |
| 48 | type AdapterFlags struct { |
| 49 | // Command line parameters |
| 50 | InstanceID string |
| 51 | KafkaAdapterHost string |
| 52 | KafkaAdapterPort int |
| 53 | KafkaClusterHost string |
| 54 | KafkaClusterPort int |
| 55 | KVStoreType string |
| 56 | KVStoreTimeout int // in seconds |
| 57 | KVStoreHost string |
| 58 | KVStorePort int |
| 59 | Topic string |
| 60 | CoreTopic string |
| 61 | LogLevel int |
| 62 | OnuNumber int |
| 63 | Banner bool |
Vignesh Ethiraj | 5620c67 | 2019-10-14 13:18:52 +0000 | [diff] [blame] | 64 | ProbeHost string |
| 65 | ProbePort int |
David Bainbridge | 8ea2c9d | 2019-10-22 21:35:22 +0000 | [diff] [blame] | 66 | PrintVersion bool |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | func init() { |
| 70 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 71 | } |
| 72 | |
| 73 | // NewRWCoreFlags returns a new RWCore config |
| 74 | func NewAdapterFlags() *AdapterFlags { |
| 75 | var adapterFlags = AdapterFlags{ // Default values |
| 76 | InstanceID: default_InstanceID, |
| 77 | KafkaAdapterHost: default_KafkaAdapterHost, |
| 78 | KafkaAdapterPort: default_KafkaAdapterPort, |
| 79 | KafkaClusterHost: default_KafkaClusterHost, |
| 80 | KafkaClusterPort: default_KafkaClusterPort, |
| 81 | KVStoreType: default_KVStoreType, |
| 82 | KVStoreTimeout: default_KVStoreTimeout, |
| 83 | KVStoreHost: default_KVStoreHost, |
| 84 | KVStorePort: default_KVStorePort, |
| 85 | Topic: default_Topic, |
| 86 | CoreTopic: default_CoreTopic, |
| 87 | LogLevel: default_LogLevel, |
| 88 | OnuNumber: default_OnuNumber, |
| 89 | Banner: default_Banner, |
Vignesh Ethiraj | 5620c67 | 2019-10-14 13:18:52 +0000 | [diff] [blame] | 90 | ProbeHost: default_ProbeHost, |
| 91 | ProbePort: default_ProbePort, |
David Bainbridge | 8ea2c9d | 2019-10-22 21:35:22 +0000 | [diff] [blame] | 92 | PrintVersion: default_PrintVersion, |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 93 | } |
| 94 | return &adapterFlags |
| 95 | } |
| 96 | |
| 97 | // ParseCommandArguments parses the arguments when running read-write adaptercore service |
| 98 | func (so *AdapterFlags) ParseCommandArguments() { |
| 99 | |
| 100 | var help string |
| 101 | |
| 102 | help = fmt.Sprintf("Kafka - Adapter messaging host") |
| 103 | flag.StringVar(&(so.KafkaAdapterHost), "kafka_adapter_host", default_KafkaAdapterHost, help) |
| 104 | |
| 105 | help = fmt.Sprintf("Kafka - Adapter messaging port") |
| 106 | flag.IntVar(&(so.KafkaAdapterPort), "kafka_adapter_port", default_KafkaAdapterPort, help) |
| 107 | |
| 108 | help = fmt.Sprintf("Kafka - Cluster messaging host") |
| 109 | flag.StringVar(&(so.KafkaClusterHost), "kafka_cluster_host", default_KafkaClusterHost, help) |
| 110 | |
| 111 | help = fmt.Sprintf("Kafka - Cluster messaging port") |
| 112 | flag.IntVar(&(so.KafkaClusterPort), "kafka_cluster_port", default_KafkaClusterPort, help) |
| 113 | |
| 114 | help = fmt.Sprintf("Simulated OLT topic") |
| 115 | flag.StringVar(&(so.Topic), "simulator_topic", default_Topic, help) |
| 116 | |
| 117 | help = fmt.Sprintf("Core topic") |
| 118 | flag.StringVar(&(so.CoreTopic), "core_topic", default_CoreTopic, help) |
| 119 | |
| 120 | help = fmt.Sprintf("KV store type") |
| 121 | flag.StringVar(&(so.KVStoreType), "kv_store_type", default_KVStoreType, help) |
| 122 | |
| 123 | help = fmt.Sprintf("The default timeout when making a kv store request") |
| 124 | flag.IntVar(&(so.KVStoreTimeout), "kv_store_request_timeout", default_KVStoreTimeout, help) |
| 125 | |
| 126 | help = fmt.Sprintf("KV store host") |
| 127 | flag.StringVar(&(so.KVStoreHost), "kv_store_host", default_KVStoreHost, help) |
| 128 | |
| 129 | help = fmt.Sprintf("KV store port") |
| 130 | flag.IntVar(&(so.KVStorePort), "kv_store_port", default_KVStorePort, help) |
| 131 | |
| 132 | help = fmt.Sprintf("Log level") |
| 133 | flag.IntVar(&(so.LogLevel), "log_level", default_LogLevel, help) |
| 134 | |
| 135 | help = fmt.Sprintf("Number of ONUs") |
| 136 | flag.IntVar(&(so.OnuNumber), "onu_number", default_OnuNumber, help) |
| 137 | |
| 138 | help = fmt.Sprintf("Show startup banner log lines") |
| 139 | flag.BoolVar(&so.Banner, "banner", default_Banner, help) |
| 140 | |
Vignesh Ethiraj | 5620c67 | 2019-10-14 13:18:52 +0000 | [diff] [blame] | 141 | help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 142 | flag.StringVar(&(so.ProbeHost), "probe_host", default_ProbeHost, help) |
| 143 | |
| 144 | help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.") |
| 145 | flag.IntVar(&(so.ProbePort), "probe_port", default_ProbePort, help) |
| 146 | |
David Bainbridge | 8ea2c9d | 2019-10-22 21:35:22 +0000 | [diff] [blame] | 147 | help = fmt.Sprintf("Print the version information and exit.") |
| 148 | flag.BoolVar(&so.PrintVersion, "version", default_PrintVersion, help) |
| 149 | |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 150 | flag.Parse() |
| 151 | |
| 152 | containerName := getContainerInfo() |
| 153 | if len(containerName) > 0 { |
| 154 | so.InstanceID = containerName |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | func getContainerInfo() string { |
| 160 | return os.Getenv("HOSTNAME") |
| 161 | } |