blob: d320b13b322fed465cc2cefc1823ce8166569e99 [file] [log] [blame]
Stephane Barbariea75791c2019-01-24 10:58:06 -05001/*
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 */
16package config
17
18import (
19 "flag"
20 "fmt"
Scott Baker807addd2019-10-24 15:16:21 -070021 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Stephane Barbariea75791c2019-01-24 10:58:06 -050022 "os"
23)
24
25// RO Core service default constants
26const (
27 ConsulStoreName = "consul"
28 EtcdStoreName = "etcd"
29 default_InstanceID = "rocore001"
30 default_GrpcPort = 50057
31 default_GrpcHost = ""
32 default_KVStoreType = EtcdStoreName
33 default_KVStoreTimeout = 5 //in seconds
34 default_KVStoreHost = "127.0.0.1"
35 default_KVStorePort = 2379 // Consul = 8500; Etcd = 2379
36 default_KVTxnKeyDelTime = 60
37 default_LogLevel = 0
38 default_Banner = false
David K. Bainbridgef430cd52019-05-28 15:00:35 -070039 default_DisplayVersionOnly = false
Stephane Barbariea75791c2019-01-24 10:58:06 -050040 default_CoreTopic = "rocore"
41 default_ROCoreEndpoint = "rocore"
42 default_ROCoreKey = "pki/voltha.key"
43 default_ROCoreCert = "pki/voltha.crt"
44 default_ROCoreCA = "pki/voltha-CA.pem"
45 default_Affinity_Router_Topic = "affinityRouter"
Kent Hagermanc4618832019-10-07 12:24:36 -040046 default_ProbeHost = ""
Hardik Windlassdc63dde2019-09-30 07:15:13 +000047 default_ProbePort = 8080
Stephane Barbariea75791c2019-01-24 10:58:06 -050048)
49
50// ROCoreFlags represents the set of configurations used by the read-only core service
51type ROCoreFlags struct {
52 // Command line parameters
53 InstanceID string
54 ROCoreEndpoint string
55 GrpcHost string
56 GrpcPort int
57 KVStoreType string
58 KVStoreTimeout int // in seconds
59 KVStoreHost string
60 KVStorePort int
61 KVTxnKeyDelTime int
62 CoreTopic string
63 LogLevel int
64 Banner bool
David K. Bainbridgef430cd52019-05-28 15:00:35 -070065 DisplayVersionOnly bool
Stephane Barbariea75791c2019-01-24 10:58:06 -050066 ROCoreKey string
67 ROCoreCert string
68 ROCoreCA string
69 AffinityRouterTopic string
Kent Hagermanc4618832019-10-07 12:24:36 -040070 ProbeHost string
Hardik Windlassdc63dde2019-09-30 07:15:13 +000071 ProbePort int
Stephane Barbariea75791c2019-01-24 10:58:06 -050072}
73
74func init() {
75 log.AddPackage(log.JSON, log.WarnLevel, nil)
76}
77
78// NewROCoreFlags returns a new ROCore config
79func NewROCoreFlags() *ROCoreFlags {
80 var roCoreFlag = ROCoreFlags{ // Default values
81 InstanceID: default_InstanceID,
82 ROCoreEndpoint: default_ROCoreEndpoint,
83 GrpcHost: default_GrpcHost,
84 GrpcPort: default_GrpcPort,
85 KVStoreType: default_KVStoreType,
86 KVStoreTimeout: default_KVStoreTimeout,
87 KVStoreHost: default_KVStoreHost,
88 KVStorePort: default_KVStorePort,
89 KVTxnKeyDelTime: default_KVTxnKeyDelTime,
90 CoreTopic: default_CoreTopic,
91 LogLevel: default_LogLevel,
92 Banner: default_Banner,
David K. Bainbridgef430cd52019-05-28 15:00:35 -070093 DisplayVersionOnly: default_DisplayVersionOnly,
Stephane Barbariea75791c2019-01-24 10:58:06 -050094 ROCoreKey: default_ROCoreKey,
95 ROCoreCert: default_ROCoreCert,
96 ROCoreCA: default_ROCoreCA,
97 AffinityRouterTopic: default_Affinity_Router_Topic,
Kent Hagermanc4618832019-10-07 12:24:36 -040098 ProbeHost: default_ProbeHost,
Hardik Windlassdc63dde2019-09-30 07:15:13 +000099 ProbePort: default_ProbePort,
Stephane Barbariea75791c2019-01-24 10:58:06 -0500100 }
101 return &roCoreFlag
102}
103
104// ParseCommandArguments parses the arguments when running read-only core service
105func (cf *ROCoreFlags) ParseCommandArguments() {
106
107 var help string
108
109 help = fmt.Sprintf("RO core endpoint address")
110 flag.StringVar(&(cf.ROCoreEndpoint), "vcore-endpoint", default_ROCoreEndpoint, help)
111
112 help = fmt.Sprintf("GRPC server - host")
113 flag.StringVar(&(cf.GrpcHost), "grpc_host", default_GrpcHost, help)
114
115 help = fmt.Sprintf("GRPC server - port")
116 flag.IntVar(&(cf.GrpcPort), "grpc_port", default_GrpcPort, help)
117
118 help = fmt.Sprintf("RO Core topic")
119 flag.StringVar(&(cf.CoreTopic), "ro_core_topic", default_CoreTopic, help)
120
121 help = fmt.Sprintf("Affinity Router topic")
122 flag.StringVar(&(cf.AffinityRouterTopic), "affinity_router_topic", default_Affinity_Router_Topic, help)
123
124 help = fmt.Sprintf("KV store type")
125 flag.StringVar(&(cf.KVStoreType), "kv_store_type", default_KVStoreType, help)
126
127 help = fmt.Sprintf("The default timeout when making a kv store request")
128 flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", default_KVStoreTimeout, help)
129
130 help = fmt.Sprintf("KV store host")
131 flag.StringVar(&(cf.KVStoreHost), "kv_store_host", default_KVStoreHost, help)
132
133 help = fmt.Sprintf("KV store port")
134 flag.IntVar(&(cf.KVStorePort), "kv_store_port", default_KVStorePort, help)
135
136 help = fmt.Sprintf("The time to wait before deleting a completed transaction key")
137 flag.IntVar(&(cf.KVTxnKeyDelTime), "kv_txn_delete_time", default_KVTxnKeyDelTime, help)
138
139 help = fmt.Sprintf("Log level")
140 flag.IntVar(&(cf.LogLevel), "log_level", default_LogLevel, help)
141
142 help = fmt.Sprintf("Show startup banner log lines")
143 flag.BoolVar(&cf.Banner, "banner", default_Banner, help)
144
David K. Bainbridgef430cd52019-05-28 15:00:35 -0700145 help = fmt.Sprintf("Show version information and exit")
146 flag.BoolVar(&cf.DisplayVersionOnly, "version", default_DisplayVersionOnly, help)
147
Kent Hagermanc4618832019-10-07 12:24:36 -0400148 help = fmt.Sprintf("The address on which to listen to answer liveness and readiness probe queries over HTTP.")
149 flag.StringVar(&(cf.ProbeHost), "probe_host", default_ProbeHost, help)
150
Hardik Windlassdc63dde2019-09-30 07:15:13 +0000151 help = fmt.Sprintf("The port on which to listen to answer liveness and readiness probe queries over HTTP.")
152 flag.IntVar(&(cf.ProbePort), "probe_port", default_ProbePort, help)
153
Stephane Barbariea75791c2019-01-24 10:58:06 -0500154 flag.Parse()
155
156 containerName := getContainerInfo()
157 if len(containerName) > 0 {
158 cf.InstanceID = containerName
159 }
160
161}
162
163func getContainerInfo() string {
164 return os.Getenv("HOSTNAME")
165}