khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | //"context" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | //dt "github.com/docker/docker/api/types" |
| 8 | //dc "github.com/docker/docker/client" |
| 9 | "os" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 10 | ) |
| 11 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 12 | // RW Core service default constants |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 13 | const ( |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 14 | ConsulStoreName = "consul" |
| 15 | EtcdStoreName = "etcd" |
| 16 | default_InstanceID = "rwcore001" |
| 17 | default_GrpcPort = 50057 |
| 18 | default_GrpcHost = "10.100.198.240" |
| 19 | default_KafkaAdapterHost = "10.100.198.240" |
| 20 | default_KafkaAdapterPort = 9092 |
| 21 | default_KafkaClusterHost = "10.100.198.240" |
| 22 | default_KafkaClusterPort = 9094 |
| 23 | default_KVStoreType = ConsulStoreName |
| 24 | default_KVStoreTimeout = 5 //in seconds |
| 25 | default_KVStoreHost = "10.100.198.240" |
| 26 | default_KVStorePort = 8500 // Etcd = 2379 |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 27 | default_LogLevel = 0 |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 28 | default_Banner = false |
| 29 | default_CoreTopic = "rwcore" |
| 30 | default_RWCoreEndpoint = "rwcore" |
| 31 | default_RWCoreKey = "pki/voltha.key" |
| 32 | default_RWCoreCert = "pki/voltha.crt" |
| 33 | default_RWCoreCA = "pki/voltha-CA.pem" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 34 | ) |
| 35 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 36 | // RWCoreFlags represents the set of configurations used by the read-write core service |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 37 | type RWCoreFlags struct { |
| 38 | // Command line parameters |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 39 | InstanceID string |
| 40 | RWCoreEndpoint string |
| 41 | GrpcHost string |
| 42 | GrpcPort int |
| 43 | KafkaAdapterHost string |
| 44 | KafkaAdapterPort int |
| 45 | KafkaClusterHost string |
| 46 | KafkaClusterPort int |
| 47 | KVStoreType string |
| 48 | KVStoreTimeout int // in seconds |
| 49 | KVStoreHost string |
| 50 | KVStorePort int |
| 51 | CoreTopic string |
| 52 | LogLevel int |
| 53 | Banner bool |
| 54 | RWCoreKey string |
| 55 | RWCoreCert string |
| 56 | RWCoreCA string |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 57 | } |
| 58 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 59 | // NewRWCoreFlags returns a new RWCore config |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 60 | func NewRWCoreFlags() *RWCoreFlags { |
| 61 | var rwCoreFlag = RWCoreFlags{ // Default values |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 62 | InstanceID: default_InstanceID, |
| 63 | RWCoreEndpoint: default_RWCoreEndpoint, |
| 64 | GrpcHost: default_GrpcHost, |
| 65 | GrpcPort: default_GrpcPort, |
| 66 | KafkaAdapterHost: default_KafkaAdapterHost, |
| 67 | KafkaAdapterPort: default_KafkaAdapterPort, |
| 68 | KafkaClusterHost: default_KafkaClusterHost, |
| 69 | KafkaClusterPort: default_KafkaClusterPort, |
| 70 | KVStoreType: default_KVStoreType, |
| 71 | KVStoreTimeout: default_KVStoreTimeout, |
| 72 | KVStoreHost: default_KVStoreHost, |
| 73 | KVStorePort: default_KVStorePort, |
| 74 | CoreTopic: default_CoreTopic, |
| 75 | LogLevel: default_LogLevel, |
| 76 | Banner: default_Banner, |
| 77 | RWCoreKey: default_RWCoreKey, |
| 78 | RWCoreCert: default_RWCoreCert, |
| 79 | RWCoreCA: default_RWCoreCA, |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 80 | } |
| 81 | return &rwCoreFlag |
| 82 | } |
| 83 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 84 | // ParseCommandArguments parses the arguments when running read-write core service |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 85 | func (cf *RWCoreFlags) ParseCommandArguments() { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 86 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 87 | var help string |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 88 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 89 | help = fmt.Sprintf("RW core endpoint address") |
| 90 | flag.StringVar(&(cf.RWCoreEndpoint), "vcore-endpoint", default_RWCoreEndpoint, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 91 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 92 | help = fmt.Sprintf("GRPC server - host") |
| 93 | flag.StringVar(&(cf.GrpcHost), "grpc_host", default_GrpcHost, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 94 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 95 | help = fmt.Sprintf("GRPC server - port") |
| 96 | flag.IntVar(&(cf.GrpcPort), "grpc_port", default_GrpcPort, help) |
| 97 | |
| 98 | help = fmt.Sprintf("Kafka - Adapter messaging host") |
| 99 | flag.StringVar(&(cf.KafkaAdapterHost), "kafka_adapter_host", default_KafkaAdapterHost, help) |
| 100 | |
| 101 | help = fmt.Sprintf("Kafka - Adapter messaging port") |
| 102 | flag.IntVar(&(cf.KafkaAdapterPort), "kafka_adapter_port", default_KafkaAdapterPort, help) |
| 103 | |
| 104 | help = fmt.Sprintf("Kafka - Cluster messaging host") |
| 105 | flag.StringVar(&(cf.KafkaClusterHost), "kafka_cluster_host", default_KafkaClusterHost, help) |
| 106 | |
| 107 | help = fmt.Sprintf("Kafka - Cluster messaging port") |
| 108 | flag.IntVar(&(cf.KafkaClusterPort), "kafka_cluster_port", default_KafkaClusterPort, help) |
| 109 | |
| 110 | help = fmt.Sprintf("RW Core topic") |
| 111 | flag.StringVar(&(cf.CoreTopic), "rw_core_topic", default_CoreTopic, help) |
| 112 | |
| 113 | help = fmt.Sprintf("KV store type") |
| 114 | flag.StringVar(&(cf.KVStoreType), "kv_store_type", default_KVStoreType, help) |
| 115 | |
| 116 | help = fmt.Sprintf("The default timeout when making a kv store request") |
| 117 | flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", default_KVStoreTimeout, help) |
| 118 | |
| 119 | help = fmt.Sprintf("KV store host") |
| 120 | flag.StringVar(&(cf.KVStoreHost), "kv_store_host", default_KVStoreHost, help) |
| 121 | |
| 122 | help = fmt.Sprintf("KV store port") |
| 123 | flag.IntVar(&(cf.KVStorePort), "kv_store_port", default_KVStorePort, help) |
| 124 | |
| 125 | help = fmt.Sprintf("Log level") |
| 126 | flag.IntVar(&(cf.LogLevel), "log_level", default_LogLevel, help) |
| 127 | |
| 128 | help = fmt.Sprintf("Show startup banner log lines") |
| 129 | flag.BoolVar(&cf.Banner, "banner", default_Banner, help) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 130 | |
| 131 | flag.Parse() |
| 132 | |
| 133 | // Update the necessary keys with the prefixes |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 134 | //start := time.Now() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 135 | containerName := getContainerInfo() |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 136 | //fmt.Println("container name:", containerName) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 137 | if len(containerName) > 0 { |
| 138 | cf.InstanceID = containerName |
| 139 | } |
| 140 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 141 | //fmt.Println("Inside config:", cf) |
| 142 | //elapsed := time.Since(start) |
| 143 | //fmt.Println("time:", elapsed/time.Second) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | func getContainerInfo() string { |
| 147 | return os.Getenv("HOSTNAME") |
| 148 | } |