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