Fix Sdc controller
[clamp.git] / src / main / java / org / onap / clamp / clds / service / CldsService.java
index c3eb83b..888b782 100644 (file)
@@ -36,7 +36,6 @@ import java.util.Date;
 import java.util.List;
 import java.util.Properties;
 import java.util.UUID;
-import java.util.concurrent.TimeUnit;
 
 import javax.annotation.PostConstruct;
 import javax.ws.rs.BadRequestException;
@@ -83,6 +82,7 @@ import org.onap.clamp.clds.model.properties.ModelProperties;
 import org.onap.clamp.clds.model.sdc.SdcResource;
 import org.onap.clamp.clds.model.sdc.SdcServiceDetail;
 import org.onap.clamp.clds.model.sdc.SdcServiceInfo;
+import org.onap.clamp.clds.sdc.controller.installer.CsarInstallerImpl;
 import org.onap.clamp.clds.transform.XslTransformer;
 import org.onap.clamp.clds.util.JacksonUtils;
 import org.onap.clamp.clds.util.LoggingUtils;
@@ -120,7 +120,6 @@ public class CldsService extends SecureServiceBase {
     private SecureServicePermission permissionUpdateCl;
     private SecureServicePermission permissionReadTemplate;
     private SecureServicePermission permissionUpdateTemplate;
-    private static final long DCAE_DEPLOY_WAITING_TIME = TimeUnit.SECONDS.toNanos(30);
 
     @PostConstruct
     private final void afterConstruction() {
@@ -489,9 +488,21 @@ public class CldsService extends SecureServiceBase {
             if (!isTest && (actionCd.equalsIgnoreCase(CldsEvent.ACTION_SUBMIT)
                     || actionCd.equalsIgnoreCase(CldsEvent.ACTION_RESUBMIT)
                     || actionCd.equalsIgnoreCase(CldsEvent.ACTION_SUBMITDCAE))) {
-                // To verify inventory status and modify model status to
-                // distribute
-                dcaeInventoryServices.setEventInventory(retrievedModel, getUserId());
+                if (retrievedModel.getTemplateName().startsWith(CsarInstallerImpl.TEMPLATE_NAME_PREFIX)) {
+                    // SDC artifact case
+                    logger.info("Skipping DCAE inventory call as closed loop has been created from SDC notification");
+                    DcaeEvent dcaeEvent = new DcaeEvent();
+                    dcaeEvent.setArtifactName(retrievedModel.getControlName() + ".yml");
+                    dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION);
+                    CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(),
+                            CldsEvent.ACTION_STATE_RECEIVED, null);
+                } else {
+                    // This should be done only when the call to DCAE
+                    // has not yet been done. When CL comes from SDC
+                    // this is not required as the DCAE inventory call is done
+                    // during the CL deployment.
+                    dcaeInventoryServices.setEventInventory(retrievedModel, getUserId());
+                }
                 retrievedModel.save(cldsDao, getUserId());
             }
             // audit log
@@ -836,17 +847,7 @@ public class CldsService extends SecureServiceBase {
             }
             String createNewDeploymentStatusUrl = dcaeDispatcherServices.createNewDeployment(deploymentId,
                     model.getTypeId(), modelProp.getGlobal().getDeployParameters());
-            String operationStatus = "processing";
-            long waitingTime = System.nanoTime() + DCAE_DEPLOY_WAITING_TIME;
-            while ("processing".equalsIgnoreCase(operationStatus)) {
-                if (waitingTime < System.nanoTime()) {
-                    logger.info("Waiting is over for DCAE deployment");
-                    break;
-                }
-                logger.info("Waiting 5s before sending query to DCAE");
-                Thread.sleep(5000);
-                operationStatus = dcaeDispatcherServices.getOperationStatus(createNewDeploymentStatusUrl);
-            }
+            String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(createNewDeploymentStatusUrl);
             if ("succeeded".equalsIgnoreCase(operationStatus)) {
                 String artifactName = model.getControlName();
                 if (artifactName != null) {
@@ -891,14 +892,7 @@ public class CldsService extends SecureServiceBase {
         try {
             String operationStatusUndeployUrl = dcaeDispatcherServices.deleteExistingDeployment(model.getDeploymentId(),
                     model.getTypeId());
-            String operationStatus = "processing";
-            long waitingTime = System.nanoTime() + TimeUnit.MINUTES.toNanos(10);
-            while ("processing".equalsIgnoreCase(operationStatus)) {
-                if (waitingTime < System.nanoTime()) {
-                    break;
-                }
-                operationStatus = dcaeDispatcherServices.getOperationStatus(operationStatusUndeployUrl);
-            }
+            String operationStatus = dcaeDispatcherServices.getOperationStatusWithRetry(operationStatusUndeployUrl);
             if ("succeeded".equalsIgnoreCase(operationStatus)) {
                 String artifactName = model.getControlName();
                 if (artifactName != null) {