62af92a847c3218de06a9159065a94e867d6a768
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START========================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved.
4  * =================================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. 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 distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END==========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.configuration;
18
19 import java.util.Map;
20 import java.util.Optional;
21 import java.util.Properties;
22
23 import org.onap.dcaegen2.collectors.datafile.exceptions.EnvironmentLoaderException;
24 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
25 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.slf4j.MDC;
29
30 import reactor.core.publisher.Mono;
31
32 /**
33  * Handling the Consul connection.
34  *
35  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
36  */
37 class EnvironmentProcessor {
38
39     private static final int DEFAULT_CONSUL_PORT = 8500;
40     private static final Logger logger = LoggerFactory.getLogger(EnvironmentProcessor.class);
41
42     private EnvironmentProcessor() {}
43
44     static Mono<EnvProperties> readEnvironmentVariables(Properties systemEnvironment, Map<String, String> contextMap) {
45         MDC.setContextMap(contextMap);
46         logger.trace("Loading configuration from system environment variables");
47         EnvProperties envProperties;
48         try {
49             envProperties = ImmutableEnvProperties.builder() //
50                     .consulHost(getConsulHost(systemEnvironment)) //
51                     .consulPort(getConsultPort(systemEnvironment)) //
52                     .cbsName(getConfigBindingService(systemEnvironment)) //
53                     .appName(getService(systemEnvironment)) //
54                     .build();
55         } catch (EnvironmentLoaderException e) {
56             return Mono.error(e);
57         }
58         logger.trace("Evaluated environment system variables {}", envProperties);
59         return Mono.just(envProperties);
60     }
61
62     private static String getConsulHost(Properties systemEnvironments) throws EnvironmentLoaderException {
63         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_HOST"))
64                 .orElseThrow(() -> new EnvironmentLoaderException("$CONSUL_HOST environment has not been defined"));
65     }
66
67     private static Integer getConsultPort(Properties systemEnvironments) {
68         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_PORT")) //
69                 .map(Integer::valueOf) //
70                 .orElseGet(EnvironmentProcessor::getDefaultPortOfConsul);
71     }
72
73     private static String getConfigBindingService(Properties systemEnvironments) throws EnvironmentLoaderException {
74         return Optional.ofNullable(systemEnvironments.getProperty("CONFIG_BINDING_SERVICE")) //
75                 .orElseThrow(() -> new EnvironmentLoaderException(
76                         "$CONFIG_BINDING_SERVICE environment has not been defined"));
77     }
78
79     private static String getService(Properties systemEnvironments) throws EnvironmentLoaderException {
80         return Optional
81                 .ofNullable(Optional.ofNullable(systemEnvironments.getProperty("HOSTNAME"))
82                         .orElse(systemEnvironments.getProperty("SERVICE_NAME")))
83                 .orElseThrow(() -> new EnvironmentLoaderException(
84                         "Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment"));
85     }
86
87     private static Integer getDefaultPortOfConsul() {
88         logger.warn("$CONSUL_PORT variable will be set to default port {}", DEFAULT_CONSUL_PORT);
89         return DEFAULT_CONSUL_PORT;
90     }
91 }