8f417261232c6e71036d989eaf424d689c4fa743
[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");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.configuration;
20
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.Properties;
24 import org.onap.dcaegen2.collectors.datafile.exceptions.EnvironmentLoaderException;
25 import org.onap.dcaegen2.collectors.datafile.model.logging.MdcVariables;
26 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import reactor.core.publisher.Mono;
31
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
43     static Mono<EnvProperties> evaluate(Properties systemEnvironment, Map<String, String> contextMap) {
44         MdcVariables.setMdcContextMap(contextMap);
45         logger.info("Loading configuration from system environment variables");
46         EnvProperties envProperties;
47         try {
48             envProperties = ImmutableEnvProperties.builder().consulHost(getConsulHost(systemEnvironment))
49                 .consulPort(getConsultPort(systemEnvironment)).cbsName(getConfigBindingService(systemEnvironment))
50                 .appName(getService(systemEnvironment)).build();
51         } catch (EnvironmentLoaderException e) {
52             return Mono.error(e);
53         }
54         logger.info("Evaluated environment system variables {}", envProperties);
55         return Mono.just(envProperties);
56     }
57
58     private static String getConsulHost(Properties systemEnvironments) throws EnvironmentLoaderException {
59         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_HOST"))
60             .orElseThrow(() -> new EnvironmentLoaderException("$CONSUL_HOST environment has not been defined"));
61     }
62
63     private static Integer getConsultPort(Properties systemEnvironments) {
64         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_PORT")).map(Integer::valueOf)
65             .orElseGet(EnvironmentProcessor::getDefaultPortOfConsul);
66     }
67
68     private static String getConfigBindingService(Properties systemEnvironments) throws EnvironmentLoaderException {
69         return Optional.ofNullable(systemEnvironments.getProperty("CONFIG_BINDING_SERVICE"))
70             .orElseThrow(
71                 () -> new EnvironmentLoaderException("$CONFIG_BINDING_SERVICE environment has not been defined"));
72     }
73
74     private static String getService(Properties systemEnvironments) throws EnvironmentLoaderException {
75         return Optional.ofNullable(Optional.ofNullable(systemEnvironments.getProperty("HOSTNAME"))
76             .orElse(systemEnvironments.getProperty("SERVICE_NAME")))
77             .orElseThrow(() -> new EnvironmentLoaderException(
78                 "Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment"));
79     }
80
81     private static Integer getDefaultPortOfConsul() {
82         logger.warn("$CONSUL_PORT environment has not been defined");
83         logger.warn("$CONSUL_PORT variable will be set to default port {}", DEFAULT_CONSUL_PORT);
84         return DEFAULT_CONSUL_PORT;
85     }
86 }