Integrate Config Binding Service client
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / configuration / EnvironmentProcessor.java
  */
 
 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<Variables> readEnvironmentVariables(Properties systemEnvironment) {
-        logger.trace("Loading system environment variables");
+    static Mono<EnvProperties> 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() {