71003f80a6180c1a7ce6e4028f4ba5f42f5b013d
[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 import org.onap.dcaegen2.collectors.datafile.exceptions.EnvironmentLoaderException;
23 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
24 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.slf4j.MDC;
28 import reactor.core.publisher.Mono;
29
30 /**
31  * Handling the Consul connection.
32  *
33  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
34  */
35 class EnvironmentProcessor {
36
37     private static final int DEFAULT_CONSUL_PORT = 8500;
38     private static final Logger logger = LoggerFactory.getLogger(EnvironmentProcessor.class);
39
40     private EnvironmentProcessor() {}
41
42     static Mono<EnvProperties> readEnvironmentVariables(Properties systemEnvironment, Map<String, String> contextMap) {
43         MDC.setContextMap(contextMap);
44         logger.trace("Loading configuration from system environment variables");
45         EnvProperties envProperties;
46         try {
47             envProperties = ImmutableEnvProperties.builder() //
48                     .consulHost(getConsulHost(systemEnvironment)) //
49                     .consulPort(getConsultPort(systemEnvironment)) //
50                     .cbsName(getConfigBindingService(systemEnvironment)) //
51                     .appName(getService(systemEnvironment)) //
52                     .build();
53         } catch (EnvironmentLoaderException e) {
54             return Mono.error(e);
55         }
56         logger.info("Evaluated environment system variables {}", envProperties);
57         return Mono.just(envProperties);
58     }
59
60     private static String getConsulHost(Properties systemEnvironments) throws EnvironmentLoaderException {
61         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_HOST"))
62                 .orElseThrow(() -> new EnvironmentLoaderException("$CONSUL_HOST environment has not been defined"));
63     }
64
65     private static Integer getConsultPort(Properties systemEnvironments) {
66         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_PORT")) //
67                 .map(Integer::valueOf) //
68                 .orElseGet(EnvironmentProcessor::getDefaultPortOfConsul);
69     }
70
71     private static String getConfigBindingService(Properties systemEnvironments) throws EnvironmentLoaderException {
72         return Optional.ofNullable(systemEnvironments.getProperty("CONFIG_BINDING_SERVICE")) //
73                 .orElseThrow(() -> new EnvironmentLoaderException(
74                         "$CONFIG_BINDING_SERVICE environment has not been defined"));
75     }
76
77     private static String getService(Properties systemEnvironments) throws EnvironmentLoaderException {
78         return Optional
79                 .ofNullable(Optional.ofNullable(systemEnvironments.getProperty("HOSTNAME"))
80                         .orElse(systemEnvironments.getProperty("SERVICE_NAME")))
81                 .orElseThrow(() -> new EnvironmentLoaderException(
82                         "Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment"));
83     }
84
85     private static Integer getDefaultPortOfConsul() {
86         logger.warn("$CONSUL_PORT variable will be set to default port {}", DEFAULT_CONSUL_PORT);
87         return DEFAULT_CONSUL_PORT;
88     }
89 }