Minor Improvement
[clamp.git] / src / main / java / org / onap / clamp / clds / service / CldsService.java
index cbed0c8..7cc9af9 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;
@@ -121,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() {
@@ -355,6 +353,8 @@ public class CldsService extends SecureServiceBase {
                 cldsModel.setBpmnText(template.getBpmnText());
             }
         }
+        updateAndInsertNewEvent(cldsModel.getName(), cldsModel.getControlNamePrefix(), cldsModel.getEvent(),
+                CldsEvent.ACTION_MODIFY);
         cldsModel.save(cldsDao, getUserId());
         // audit log
         LoggingUtils.setTimeContext(startTime, new Date());
@@ -444,6 +444,7 @@ public class CldsService extends SecureServiceBase {
             this.fillInCldsModel(model);
             // save model to db
             model.setName(modelName);
+            updateAndInsertNewEvent(modelName, model.getControlNamePrefix(), model.getEvent(), CldsEvent.ACTION_MODIFY);
             model.save(cldsDao, getUserId());
             // get vars and format if necessary
             String prop = model.getPropText();
@@ -479,14 +480,16 @@ public class CldsService extends SecureServiceBase {
             logger.info("docText - " + docText);
             try {
                 String result = camelProxy.submit(actionCd, prop, bpmnJson, modelName, controlName, docText, isTest,
-                        userId, isInsertTestEvent);
+                        userId, isInsertTestEvent, model.getEvent().getActionCd());
                 logger.info("Starting Camel flow on request, result is: ", result);
             } catch (SdcCommunicationException | PolicyClientException | BadRequestException e) {
                 errorCase = true;
                 logger.error("Exception occured during invoking Camel process", e);
             }
-            // refresh model info from db (get fresh event info)
-            retrievedModel = CldsModel.retrieve(cldsDao, modelName, false);
+            if (!actionCd.equalsIgnoreCase(CldsEvent.ACTION_DELETE)) {
+                // refresh model info from db (get fresh event info)
+                retrievedModel = CldsModel.retrieve(cldsDao, modelName, false);
+            }
             if (!isTest && (actionCd.equalsIgnoreCase(CldsEvent.ACTION_SUBMIT)
                     || actionCd.equalsIgnoreCase(CldsEvent.ACTION_RESUBMIT)
                     || actionCd.equalsIgnoreCase(CldsEvent.ACTION_SUBMITDCAE))) {
@@ -494,7 +497,7 @@ public class CldsService extends SecureServiceBase {
                     // 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.getName() + ".yml");
+                    dcaeEvent.setArtifactName(retrievedModel.getControlName() + ".yml");
                     dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION);
                     CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(),
                             CldsEvent.ACTION_STATE_RECEIVED, null);
@@ -837,6 +840,10 @@ public class CldsService extends SecureServiceBase {
             fillInCldsModel(model);
             String bpmnJson = cldsBpmnTransformer.doXslTransformToString(model.getBpmnText());
             logger.info("PUT bpmnJson={}", bpmnJson);
+            SecureServicePermission permisionManage = SecureServicePermission.create(cldsPermissionTypeClManage,
+                    cldsPermissionInstance, CldsEvent.ACTION_DEPLOY);
+            isAuthorized(permisionManage);
+            isAuthorizedForVf(model);
             ModelProperties modelProp = new ModelProperties(modelName, model.getControlName(), CldsEvent.ACTION_DEPLOY,
                     false, bpmnJson, model.getPropText());
             checkForDuplicateServiceVf(modelName, model.getPropText());
@@ -849,17 +856,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) {
@@ -902,16 +899,13 @@ public class CldsService extends SecureServiceBase {
         LoggingUtils.setRequestContext("CldsService: Undeploy model", getPrincipalName());
         Boolean errorCase = false;
         try {
+            SecureServicePermission permisionManage = SecureServicePermission.create(cldsPermissionTypeClManage,
+                    cldsPermissionInstance, CldsEvent.ACTION_UNDEPLOY);
+            isAuthorized(permisionManage);
+            isAuthorizedForVf(model);
             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) {
@@ -966,4 +960,19 @@ public class CldsService extends SecureServiceBase {
             }
         }
     }
+
+    private void updateAndInsertNewEvent(String cldsModelName, String cldsControlNamePrfx, CldsEvent event,
+            String newAction) {
+        // If model action is in submit/resubmit/distributed and user try
+        // to save then we are changing action back to create.
+        if (event != null && (CldsEvent.ACTION_SUBMIT.equalsIgnoreCase(event.getActionCd())
+                || CldsEvent.ACTION_RESUBMIT.equalsIgnoreCase(event.getActionCd())
+                || CldsEvent.ACTION_DISTRIBUTE.equalsIgnoreCase(event.getActionCd()))) {
+            CldsEvent newEvent = new CldsEvent();
+            newEvent.setUserid(getUserId());
+            newEvent.setActionCd(newAction);
+            newEvent.setActionStateCd(CldsEvent.ACTION_STATE_COMPLETED);
+            cldsDao.insEvent(cldsModelName, cldsControlNamePrfx, null, newEvent);
+        }
+    }
 }
\ No newline at end of file