2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.policyagent.tasks;
23 import java.util.Optional;
24 import java.util.Properties;
26 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
28 import org.oransc.policyagent.exceptions.EnvironmentLoaderException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import reactor.core.publisher.Mono;
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> readEnvironmentVariables(Properties systemEnvironment) {
43 EnvProperties envProperties;
45 envProperties = ImmutableEnvProperties.builder() //
46 .consulHost(getConsulHost(systemEnvironment)) //
47 .consulPort(getConsultPort(systemEnvironment)) //
48 .cbsName(getConfigBindingService(systemEnvironment)) //
49 .appName(getService(systemEnvironment)) //
51 } catch (EnvironmentLoaderException e) {
54 logger.trace("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")) //
65 .map(Integer::valueOf) //
66 .orElseGet(EnvironmentProcessor::getDefaultPortOfConsul);
69 private static String getConfigBindingService(Properties systemEnvironments) throws EnvironmentLoaderException {
70 return Optional.ofNullable(systemEnvironments.getProperty("CONFIG_BINDING_SERVICE")) //
72 () -> new EnvironmentLoaderException("$CONFIG_BINDING_SERVICE environment has not been defined"));
75 private static String getService(Properties systemEnvironments) throws EnvironmentLoaderException {
77 .ofNullable(Optional.ofNullable(systemEnvironments.getProperty("HOSTNAME"))
78 .orElse(systemEnvironments.getProperty("SERVICE_NAME")))
79 .orElseThrow(() -> new EnvironmentLoaderException(
80 "Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment"));
83 private static Integer getDefaultPortOfConsul() {
84 logger.warn("$CONSUL_PORT variable will be set to default port {}", DEFAULT_CONSUL_PORT);
85 return DEFAULT_CONSUL_PORT;