Update Config Acquisation

Issue-ID: DCAEGEN2-2341
Change-Id: I3fc431d563e668cbc2bd4a1dd03e30ca908fed0b
Signed-off-by: JoeOLeary <joseph.o.leary@est.tech>
diff --git a/pom.xml b/pom.xml
index 516e223..60504ed 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,7 +26,7 @@
 
     <groupId>org.onap.dcaegen2.services</groupId>
     <artifactId>pm-mapper</artifactId>
-    <version>1.3.2-SNAPSHOT</version>
+    <version>1.4.1-SNAPSHOT</version>
 
     <parent>
         <groupId>org.onap.oparent</groupId>
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java
index 6aeeaba..7aab08a 100644
--- a/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java
+++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java
@@ -53,6 +53,7 @@
 import org.slf4j.MDC;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.FluxSink;
+import reactor.core.scheduler.Scheduler;
 import reactor.core.scheduler.Schedulers;
 
 import javax.net.ssl.SSLContext;
@@ -60,6 +61,7 @@
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 @Data
 public class App {
@@ -70,6 +72,8 @@
     private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(App.class));
     private static final int HTTP_PORT = 8081;
     private static final int HTTPS_PORT = 8443;
+    private static final int INITIAL_RECONFIGURATION_PERIOD = 60;
+    private static final int RECONFIGURATION_PERIOD = 60;
     private static Path templates = Paths.get("/opt/app/pm-mapper/etc/templates/");
     private static Path schemas = Paths.get("/opt/app/pm-mapper/etc/schemas/");
 
@@ -91,6 +95,7 @@
     private List<ServerResource> serverResources;
     private Flux<Event> flux;
     private FluxSink<Event> fluxSink;
+    private Scheduler configScheduler;
 
     /**
      * Creates an instance of the application.
@@ -117,6 +122,7 @@
         this.validator = new XMLValidator(schemasDirectory);
         this.vesPublisher = new VESPublisher(mapperConfig);
         this.flux = Flux.create(eventFluxSink -> this.fluxSink = eventFluxSink);
+        this.configScheduler = Schedulers.newSingle("Config");
 
         this.flux.onBackpressureDrop(App::handleBackPressure)
                 .doOnNext(App::receiveRequest)
@@ -133,6 +139,7 @@
                 .concatMap(this.vesPublisher::publish)
                 .subscribe(event -> App.sendEventProcessed(this.mapperConfig, event));
 
+        this.configScheduler.schedulePeriodically(this::reconfigure, INITIAL_RECONFIGURATION_PERIOD, RECONFIGURATION_PERIOD, TimeUnit.SECONDS);
         this.healthCheckHandler = new HealthCheckHandler();
         this.deliveryHandler = new DeliveryHandler(fluxSink::next);
         this.dynamicConfiguration = new DynamicConfiguration(Arrays.asList(mapperConfig), mapperConfig);
@@ -150,6 +157,7 @@
      */
     public void start() {
         this.applicationServer.start();
+        this.configScheduler.start();
     }
 
     /**
@@ -157,6 +165,7 @@
      */
     public void stop() {
         this.applicationServer.stop();
+        this.configScheduler.dispose();
     }
 
     private Undertow server(MapperConfig config, List<ServerResource> serverResources) throws IOException {
@@ -174,6 +183,14 @@
                 .build();
     }
 
+    private void reconfigure() {
+        try {
+            this.dynamicConfiguration.reconfigure();
+        } catch (Exception e) {
+            logger.unwrap().error("Failed to reconfigure service.", e);
+        }
+    }
+
     public static void main(String[] args) {
         new App(templates, schemas, HTTP_PORT, HTTPS_PORT, new ConfigHandler()).start();
     }
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfiguration.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfiguration.java
index 420081a..eee7d27 100644
--- a/src/main/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfiguration.java
+++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfiguration.java
@@ -67,26 +67,34 @@
     public void handleRequest(HttpServerExchange httpServerExchange) throws Exception {
         try {
             logger.entering(new HttpServerExchangeAdapter(httpServerExchange));
-            MapperConfig config = configHandler.getMapperConfig();
-            int responseCode = StatusCodes.OK;
-            String responseMessage = StatusCodes.OK_STRING;
-
-            if (!this.originalConfig.equals(config)) {
-                logger.unwrap().info("Configuration update detected.");
-                logger.unwrap().info("Reconfiguring configurables");
-                try {
-                    applyConfiguration(config);
-                    this.originalConfig = config;
-                } catch (ReconfigurationException e) {
-                    responseCode = StatusCodes.INTERNAL_SERVER_ERROR;
-                    responseMessage = StatusCodes.INTERNAL_SERVER_ERROR_STRING;
-                    logger.unwrap().error("Failed to apply configuration update, reverting to original config", e);
-                    applyConfiguration(this.originalConfig);
-                }
-            }
+            boolean reconfigured = reconfigure();
+            int responseCode = reconfigured? StatusCodes.OK : StatusCodes.INTERNAL_SERVER_ERROR;
+            String responseMessage = reconfigured ? StatusCodes.OK_STRING : StatusCodes.INTERNAL_SERVER_ERROR_STRING;
             httpServerExchange.setStatusCode(responseCode).getResponseSender().send(responseMessage);
         } finally {
             logger.exiting();
         }
     }
+
+    /**
+     * @return Boolean to indicate if configuration attempt was successful
+     * @throws Exception If environment config cannot be read, or if re-applying the original config fails
+     */
+    public boolean reconfigure() throws Exception {
+        boolean success = true;
+        MapperConfig config = configHandler.getMapperConfig();
+        if (!this.originalConfig.equals(config)) {
+            logger.unwrap().info("Configuration update detected.");
+            logger.unwrap().info("Reconfiguring configurables");
+            try {
+                applyConfiguration(config);
+                this.originalConfig = config;
+            } catch (ReconfigurationException e) {
+                success = false;
+                logger.unwrap().error("Failed to apply configuration update, reverting to original config", e);
+                applyConfiguration(this.originalConfig);
+            }
+        }
+        return success;
+    }
 }
diff --git a/src/main/resources/Dockerfile b/src/main/resources/Dockerfile
index d86e2c3..f0f827d 100644
--- a/src/main/resources/Dockerfile
+++ b/src/main/resources/Dockerfile
@@ -57,5 +57,5 @@
       git.commit.user.name="${git.commit.user.name}"
 
 
-ENTRYPOINT ["/bin/sh", "-c", "nohup sh etc/reconfigure.sh & /usr/local/openjdk-11/bin/java -jar ${project.artifactId}-${project.version}.jar"]
+ENTRYPOINT ["/usr/local/openjdk-11/bin/java", "-jar", "${project.artifactId}-${project.version}.jar"]
 ARG JAR
diff --git a/src/main/resources/reconfigure.sh b/src/main/resources/reconfigure.sh
deleted file mode 100644
index 1998e5f..0000000
--- a/src/main/resources/reconfigure.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/usr/bin/env sh
-while true
-do
-    sleep 60
-    echo $(curl -sI -X GET https://localhost:8443/reconfigure -k | head -n1) >> /var/log/ONAP/dcaegen2/services/pm-mapper/reconfigure.log
-done
diff --git a/version.properties b/version.properties
index ef20baa..f352992 100644
--- a/version.properties
+++ b/version.properties
@@ -1,6 +1,6 @@
 major=1
-minor=3
-patch=2
+minor=4
+patch=1
 base_version=${major}.${minor}.${patch}
 release_version=${base_version}
 snapshot_version=${base_version}-SNAPSHOT