blob: b7906f22e7f44bf77812ae8067c4be608f76b35a [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"
21 "github.com/opencord/voltha-go/common/log"
22 "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
39 default_CoreTopic = "rocore"
40 default_ROCoreEndpoint = "rocore"
41 default_ROCoreKey = "pki/voltha.key"
42 default_ROCoreCert = "pki/voltha.crt"
43 default_ROCoreCA = "pki/voltha-CA.pem"
44 default_Affinity_Router_Topic = "affinityRouter"
45)
46
47// ROCoreFlags represents the set of configurations used by the read-only core service
48type ROCoreFlags struct {
49 // Command line parameters
50 InstanceID string
51 ROCoreEndpoint string
52 GrpcHost string
53 GrpcPort int
54 KVStoreType string
55 KVStoreTimeout int // in seconds
56 KVStoreHost string
57 KVStorePort int
58 KVTxnKeyDelTime int
59 CoreTopic string
60 LogLevel int
61 Banner bool
62 ROCoreKey string
63 ROCoreCert string
64 ROCoreCA string
65 AffinityRouterTopic string
66}
67
68func init() {
69 log.AddPackage(log.JSON, log.WarnLevel, nil)
70}
71
72// NewROCoreFlags returns a new ROCore config
73func NewROCoreFlags() *ROCoreFlags {
74 var roCoreFlag = ROCoreFlags{ // Default values
75 InstanceID: default_InstanceID,
76 ROCoreEndpoint: default_ROCoreEndpoint,
77 GrpcHost: default_GrpcHost,
78 GrpcPort: default_GrpcPort,
79 KVStoreType: default_KVStoreType,
80 KVStoreTimeout: default_KVStoreTimeout,
81 KVStoreHost: default_KVStoreHost,
82 KVStorePort: default_KVStorePort,
83 KVTxnKeyDelTime: default_KVTxnKeyDelTime,
84 CoreTopic: default_CoreTopic,
85 LogLevel: default_LogLevel,
86 Banner: default_Banner,
87 ROCoreKey: default_ROCoreKey,
88 ROCoreCert: default_ROCoreCert,
89 ROCoreCA: default_ROCoreCA,
90 AffinityRouterTopic: default_Affinity_Router_Topic,
91 }
92 return &roCoreFlag
93}
94
95// ParseCommandArguments parses the arguments when running read-only core service
96func (cf *ROCoreFlags) ParseCommandArguments() {
97
98 var help string
99
100 help = fmt.Sprintf("RO core endpoint address")
101 flag.StringVar(&(cf.ROCoreEndpoint), "vcore-endpoint", default_ROCoreEndpoint, help)
102
103 help = fmt.Sprintf("GRPC server - host")
104 flag.StringVar(&(cf.GrpcHost), "grpc_host", default_GrpcHost, help)
105
106 help = fmt.Sprintf("GRPC server - port")
107 flag.IntVar(&(cf.GrpcPort), "grpc_port", default_GrpcPort, help)
108
109 help = fmt.Sprintf("RO Core topic")
110 flag.StringVar(&(cf.CoreTopic), "ro_core_topic", default_CoreTopic, help)
111
112 help = fmt.Sprintf("Affinity Router topic")
113 flag.StringVar(&(cf.AffinityRouterTopic), "affinity_router_topic", default_Affinity_Router_Topic, help)
114
115 help = fmt.Sprintf("KV store type")
116 flag.StringVar(&(cf.KVStoreType), "kv_store_type", default_KVStoreType, help)
117
118 help = fmt.Sprintf("The default timeout when making a kv store request")
119 flag.IntVar(&(cf.KVStoreTimeout), "kv_store_request_timeout", default_KVStoreTimeout, help)
120
121 help = fmt.Sprintf("KV store host")
122 flag.StringVar(&(cf.KVStoreHost), "kv_store_host", default_KVStoreHost, help)
123
124 help = fmt.Sprintf("KV store port")
125 flag.IntVar(&(cf.KVStorePort), "kv_store_port", default_KVStorePort, help)
126
127 help = fmt.Sprintf("The time to wait before deleting a completed transaction key")
128 flag.IntVar(&(cf.KVTxnKeyDelTime), "kv_txn_delete_time", default_KVTxnKeyDelTime, help)
129
130 help = fmt.Sprintf("Log level")
131 flag.IntVar(&(cf.LogLevel), "log_level", default_LogLevel, help)
132
133 help = fmt.Sprintf("Show startup banner log lines")
134 flag.BoolVar(&cf.Banner, "banner", default_Banner, help)
135
136 flag.Parse()
137
138 containerName := getContainerInfo()
139 if len(containerName) > 0 {
140 cf.InstanceID = containerName
141 }
142
143}
144
145func getContainerInfo() string {
146 return os.Getenv("HOSTNAME")
147}