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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 package org.onap.dcaegen2.collectors.datafile.configuration;
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;
31 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
33 class EnvironmentProcessor {
35 private static final int DEFAULT_CONSUL_PORT = 8500;
36 private static final Logger logger = LoggerFactory.getLogger(EnvironmentProcessor.class);
38 private EnvironmentProcessor() {
41 static Mono<EnvProperties> evaluate(Properties systemEnvironment) {
42 logger.info("Loading configuration from system environment variables");
43 EnvProperties envProperties;
45 envProperties = ImmutableEnvProperties.builder().consulHost(getConsulHost(systemEnvironment))
46 .consulPort(getConsultPort(systemEnvironment)).cbsName(getConfigBindingService(systemEnvironment))
47 .appName(getService(systemEnvironment)).build();
48 } catch (EnvironmentLoaderException e) {
51 logger.info("Evaluated environment system variables {}", envProperties);
52 return Mono.just(envProperties);
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"));
60 private static Integer getConsultPort(Properties systemEnvironments) {
61 return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_PORT")).map(Integer::valueOf)
62 .orElseGet(EnvironmentProcessor::getDefaultPortOfConsul);
65 private static String getConfigBindingService(Properties systemEnvironments) throws EnvironmentLoaderException {
66 return Optional.ofNullable(systemEnvironments.getProperty("CONFIG_BINDING_SERVICE"))
68 () -> new EnvironmentLoaderException("$CONFIG_BINDING_SERVICE environment has not been defined"));
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"));
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;