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
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;
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;
33 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
35 class EnvironmentProcessor {
37 private static final int DEFAULT_CONSUL_PORT = 8500;
38 private static final Logger logger = LoggerFactory.getLogger(EnvironmentProcessor.class);
40 private EnvironmentProcessor() {
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;
48 envProperties = ImmutableEnvProperties.builder().consulHost(getConsulHost(systemEnvironment))
49 .consulPort(getConsultPort(systemEnvironment)).cbsName(getConfigBindingService(systemEnvironment))
50 .appName(getService(systemEnvironment)).build();
51 } catch (EnvironmentLoaderException e) {
54 logger.info("Evaluated environment system variables {}", envProperties);
55 return Mono.just(envProperties);
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"));
63 private static Integer getConsultPort(Properties systemEnvironments) {
64 return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_PORT")).map(Integer::valueOf)
65 .orElseGet(EnvironmentProcessor::getDefaultPortOfConsul);
68 private static String getConfigBindingService(Properties systemEnvironments) throws EnvironmentLoaderException {
69 return Optional.ofNullable(systemEnvironments.getProperty("CONFIG_BINDING_SERVICE"))
71 () -> new EnvironmentLoaderException("$CONFIG_BINDING_SERVICE environment has not been defined"));
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"));
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;