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