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