2 * ============LICENSE_START=======================================================
3 * org.onap.dcaegen2.collectors.ves
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2018 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.dcae.controller;
23 import io.vavr.collection.Map;
24 import io.vavr.control.Option;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import static io.vavr.API.List;
29 import static io.vavr.API.Try;
30 import static org.onap.dcae.common.publishing.VavrUtils.f;
32 final class EnvPropertiesReader {
34 private final static Logger log = LoggerFactory.getLogger(EnvPropertiesReader.class);
36 static Option<EnvProps> readEnvProps(Map<String, String> environmentVariables) {
37 log.info("Loading necessary environment variables for dynamic configuration update");
38 int consulPort = getConsulPort(environmentVariables);
39 String consulProtocol = getConsulProtocol(environmentVariables);
40 String cbsProtocol = getCbsProtocol(environmentVariables);
41 Option<String> consulHost = getConsulHost(environmentVariables);
42 Option<String> cbsServiceName = getCBSName(environmentVariables);
43 Option<String> vesCollectorAppName = getAppName(environmentVariables);
44 return Option.sequence(List(consulHost, cbsServiceName, vesCollectorAppName))
45 .map(e -> new EnvProps(consulProtocol, e.get(0), consulPort, cbsProtocol, e.get(1), e.get(2)))
46 .onEmpty(() -> log.warn("Some required environment variables are missing"))
47 .peek(props -> log.info(f("Discovered following environment variables: '%s'", props)));
50 private static Option<String> getAppName(Map<String, String> environmentVariables) {
51 return environmentVariables.get("HOSTNAME")
52 .orElse(environmentVariables.get("SERVICE_NAME"))
53 .onEmpty(() -> log.warn("App service name (as registered in CBS) (env var: 'HOSTNAME' / 'SERVICE_NAME') "
54 + "is missing error environment variables."));
57 private static Option<String> getCBSName(Map<String, String> environmentVariables) {
58 return environmentVariables.get("CONFIG_BINDING_SERVICE")
59 .onEmpty(() -> log.warn("Name of CBS Service (as registered in Consul) (env var: 'CONFIG_BINDING_SERVICE') "
60 + "is missing from environment variables."));
63 private static Integer getConsulPort(Map<String, String> environmentVariables) {
64 return environmentVariables.get("CONSUL_PORT")
65 .flatMap(str -> Try(() -> Integer.valueOf(str))
66 .onFailure(exc -> log.warn("Consul port is not an integer value", exc))
68 .onEmpty(() -> log.warn("Consul port (env var: 'CONSUL_PORT') is missing from environment variables. "
69 + "Using default value of 8500"))
73 private static Option<String> getConsulHost(Map<String, String> environmentVariables) {
74 return environmentVariables.get("CONSUL_HOST")
75 .onEmpty(() -> log.warn("Consul host (env var: 'CONSUL_HOST') (without port) "
76 + "is missing from environment variables."));
79 private static String getConsulProtocol(Map<String, String> environmentVariables) {
80 return getProtocolFrom("CONSUL_PROTOCOL", environmentVariables);
83 private static String getCbsProtocol(Map<String, String> environmentVariables) {
84 return getProtocolFrom("CBS_PROTOCOL", environmentVariables);
87 private static String getProtocolFrom(String variableName, Map<String, String> environmentVariables) {
88 return environmentVariables.get(variableName)
89 .onEmpty(() -> log.warn("Consul protocol (env var: '" + variableName + "') is missing "
90 + "from environment variables."))