Fix container startup
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / NokiaSvnfmApplication.java
index e75159f..ceb0335 100644 (file)
 
 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia;
 
-import org.apache.log4j.Logger;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager;
+import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -28,12 +33,17 @@ import org.springframework.context.annotation.Profile;
 import org.springframework.context.event.ContextClosedEvent;
 import org.springframework.stereotype.Component;
 
+import static java.util.concurrent.Executors.newCachedThreadPool;
+
+import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
+import static org.slf4j.LoggerFactory.getLogger;
+
 /**
  * Represents the spring boot application
  */
 @SpringBootApplication
 public class NokiaSvnfmApplication {
-    private static Logger logger = Logger.getLogger(NokiaSvnfmApplication.class);
+    private static Logger logger = getLogger(NokiaSvnfmApplication.class);
 
     /**
      * Entry point for the Spring boot application
@@ -41,7 +51,7 @@ public class NokiaSvnfmApplication {
      * @param args arguments for the application (not used)
      */
     public static void main(String[] args) {
-        SpringApplication.run(NokiaSvnfmApplication.class, args);
+        systemFunctions().newSpringApplication(NokiaSvnfmApplication.class).run(args);
     }
 
     /**
@@ -53,19 +63,45 @@ public class NokiaSvnfmApplication {
     @Component
     @Profile("!test")
     public static class SelfRegistrationTrigger implements ApplicationListener<ApplicationReadyEvent> {
+        private final SelfRegistrationManager selfRegistrationManager;
+        private final JobManager jobManager;
+        /**
+         * Runs the registration process
+         */
+        private ExecutorService executorService = newCachedThreadPool();
+
         @Autowired
-        private SelfRegistrationManager selfRegistrationManager;
+        SelfRegistrationTrigger(SelfRegistrationManager selfRegistrationManager, JobManager jobManager){
+            this.jobManager = jobManager;
+            this.selfRegistrationManager = selfRegistrationManager;
+        }
 
         @Override
         public void onApplicationEvent(ApplicationReadyEvent contextRefreshedEvent) {
-            logger.info("Self registration started");
-            try {
-                selfRegistrationManager.register();
-                logger.info("Self registration finished");
-            } catch (RuntimeException e) {
-                logger.error("Self registration failed", e);
-                throw e;
-            }
+            Callable<Boolean> singleRegistration = () -> {
+                logger.info("Self registration started");
+                try {
+                    selfRegistrationManager.register();
+                    logger.info("Self registration finished");
+                } catch (RuntimeException e) {
+                    logger.error("Self registration failed", e);
+                    throw e;
+                }
+                return true;
+            };
+            executorService.submit(() -> {
+                while(!jobManager.isPreparingForShutDown()){
+                    try{
+                        executorService.submit(singleRegistration).get();
+                        //registration successful
+                        return;
+                    }
+                    catch (Exception e){
+                        logger.warn("Unable to execute self registration process", e);
+                    }
+                    systemFunctions().sleep(5000);
+                }
+            });
         }
     }
 
@@ -77,10 +113,14 @@ public class NokiaSvnfmApplication {
     @Component
     @Profile("!test")
     public static class SelfDeRegistrationTrigger implements ApplicationListener<ContextClosedEvent> {
+        private final SelfRegistrationManager selfRegistrationManager;
+        private final JobManager jobManager;
+
         @Autowired
-        private SelfRegistrationManager selfRegistrationManager;
-        @Autowired
-        private JobManager jobManager;
+        SelfDeRegistrationTrigger(SelfRegistrationManager selfRegistrationManager, JobManager jobManager){
+            this.jobManager = jobManager;
+            this.selfRegistrationManager = selfRegistrationManager;
+        }
 
         @Override
         public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {