X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fconfiguration%2FEnvironmentProcessor.java;fp=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fconfiguration%2FEnvironment.java;h=c2033f4edabc368f42442c74f325c621ed9bcf49;hb=6fe1880bfac3daa86c05461da80147325f5f47a2;hp=a6a328ec3227346920ef9105fff8b915e7a8bc0e;hpb=ce1713127bd5445b3890efddf4609cb8b8d58054;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/configuration/Environment.java b/policy-agent/src/main/java/org/oransc/policyagent/configuration/EnvironmentProcessor.java similarity index 50% rename from policy-agent/src/main/java/org/oransc/policyagent/configuration/Environment.java rename to policy-agent/src/main/java/org/oransc/policyagent/configuration/EnvironmentProcessor.java index a6a328ec..c2033f4e 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/configuration/Environment.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/configuration/EnvironmentProcessor.java @@ -19,76 +19,63 @@ */ package org.oransc.policyagent.configuration; - import java.util.Optional; import java.util.Properties; - -import org.oransc.policyagent.exceptions.ServiceException; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties; +import org.oransc.policyagent.exceptions.EnvironmentLoaderException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; -class Environment { - - public static class Variables { - - public final String consulHost; - public final Integer consulPort; - public final String cbsName; - public final String appName; - - public Variables(String consulHost, Integer consulPort, String cbsName, String appName) { - this.consulHost = consulHost; - this.consulPort = consulPort; - this.cbsName = cbsName; - this.appName = appName; - } - } +class EnvironmentProcessor { private static final int DEFAULT_CONSUL_PORT = 8500; - private static final Logger logger = LoggerFactory.getLogger(Environment.class); + private static final Logger logger = LoggerFactory.getLogger(EnvironmentProcessor.class); - private Environment() { + private EnvironmentProcessor() { } - static Mono readEnvironmentVariables(Properties systemEnvironment) { - logger.trace("Loading system environment variables"); + static Mono readEnvironmentVariables(Properties systemEnvironment) { + EnvProperties envProperties; try { - Variables envProperties = new Variables(getConsulHost(systemEnvironment) // - , getConsultPort(systemEnvironment) // - , getConfigBindingService(systemEnvironment) // - , getService(systemEnvironment)); // - - logger.trace("Evaluated environment system variables {}", envProperties); - return Mono.just(envProperties); - } catch (ServiceException e) { + envProperties = ImmutableEnvProperties.builder() // + .consulHost(getConsulHost(systemEnvironment)) // + .consulPort(getConsultPort(systemEnvironment)) // + .cbsName(getConfigBindingService(systemEnvironment)) // + .appName(getService(systemEnvironment)) // + .build(); + } catch (EnvironmentLoaderException e) { return Mono.error(e); } + logger.trace("Evaluated environment system variables {}", envProperties); + return Mono.just(envProperties); } - private static String getConsulHost(Properties systemEnvironments) throws ServiceException { + private static String getConsulHost(Properties systemEnvironments) throws EnvironmentLoaderException { return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_HOST")) - .orElseThrow(() -> new ServiceException("$CONSUL_HOST environment has not been defined")); + .orElseThrow(() -> new EnvironmentLoaderException("$CONSUL_HOST environment has not been defined")); } private static Integer getConsultPort(Properties systemEnvironments) { return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_PORT")) // - .map(Integer::valueOf) // - .orElseGet(Environment::getDefaultPortOfConsul); + .map(Integer::valueOf) // + .orElseGet(EnvironmentProcessor::getDefaultPortOfConsul); } - private static String getConfigBindingService(Properties systemEnvironments) throws ServiceException { + private static String getConfigBindingService(Properties systemEnvironments) throws EnvironmentLoaderException { return Optional.ofNullable(systemEnvironments.getProperty("CONFIG_BINDING_SERVICE")) // - .orElseThrow(() -> new ServiceException("$CONFIG_BINDING_SERVICE environment has not been defined")); + .orElseThrow( + () -> new EnvironmentLoaderException("$CONFIG_BINDING_SERVICE environment has not been defined")); } - private static String getService(Properties systemEnvironments) throws ServiceException { + private static String getService(Properties systemEnvironments) throws EnvironmentLoaderException { return Optional - .ofNullable(Optional.ofNullable(systemEnvironments.getProperty("HOSTNAME")) - .orElse(systemEnvironments.getProperty("SERVICE_NAME"))) - .orElseThrow(() -> new ServiceException( - "Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment")); + .ofNullable(Optional.ofNullable(systemEnvironments.getProperty("HOSTNAME")) + .orElse(systemEnvironments.getProperty("SERVICE_NAME"))) + .orElseThrow(() -> new EnvironmentLoaderException( + "Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment")); } private static Integer getDefaultPortOfConsul() {