Implement second part of dynamic DMaaP config
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / controller / EnvPropertiesReader.java
1 /*-
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.dcae.controller;
22
23 import static io.vavr.API.List;
24 import static io.vavr.API.Try;
25 import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f;
26
27 import io.vavr.collection.Map;
28 import io.vavr.control.Option;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 final class EnvPropertiesReader {
33
34     private final static Logger log = LoggerFactory.getLogger(EnvPropertiesReader.class);
35
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         Option<String> consulHost = getConsulHost(environmentVariables);
40         Option<String> cbsServiceName = getCBSName(environmentVariables);
41         Option<String> vesCollectorAppName = getAppName(environmentVariables);
42         return Option.sequence(List(consulHost, cbsServiceName, vesCollectorAppName))
43             .map(e -> new EnvProps(e.get(0), consulPort, e.get(1), e.get(2)))
44             .onEmpty(() -> log.warn("Some required environment variables are missing"))
45             .peek(props -> log.info(f("Discovered following environment variables: '%s'", props)));
46     }
47
48     private static Option<String> getAppName(Map<String, String> environmentVariables) {
49         return environmentVariables.get("HOSTNAME")
50             .orElse(environmentVariables.get("SERVICE_NAME"))
51             .onEmpty(() -> log.warn("App service name (as registered in CBS) (env var: 'HOSTNAME' / 'SERVICE_NAME') "
52                 + "is missing error environment variables."));
53     }
54
55     private static Option<String> getCBSName(Map<String, String> environmentVariables) {
56         return environmentVariables.get("CONFIG_BINDING_SERVICE")
57             .onEmpty(() -> log.warn("Name of CBS Service (as registered in Consul) (env var: 'CONFIG_BINDING_SERVICE') "
58                 + "is missing from environment variables."));
59     }
60
61     private static Integer getConsulPort(Map<String, String> environmentVariables) {
62         return environmentVariables.get("CONSUL_PORT")
63             .flatMap(str -> Try(() -> Integer.valueOf(str))
64                 .onFailure(exc -> log.warn("Consul port is not an integer value", exc))
65                 .toOption())
66             .onEmpty(() -> log.warn("Consul port (env var: 'CONSUL_PORT') is missing from environment variables. "
67                 + "Using default value of 8500"))
68             .getOrElse(8500);
69     }
70
71     private static Option<String> getConsulHost(Map<String, String> environmentVariables) {
72         return environmentVariables.get("CONSUL_HOST")
73             .onEmpty(() -> log.warn("Consul host (env var: 'CONSUL_HOST') (without port) "
74                 + "is missing from environment variables."));
75     }
76
77 }