Fix the Sdc Controller
[clamp.git] / src / main / java / org / onap / clamp / clds / model / CldsModel.java
index 55ea4ec..af4d6c6 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP CLAMP
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
  *                             reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * limitations under the License.
  * ============LICENSE_END============================================
  * ===================================================================
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * 
  */
 
 package org.onap.clamp.clds.model;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
+import com.fasterxml.jackson.databind.JsonNode;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -34,6 +36,7 @@ import javax.ws.rs.BadRequestException;
 import javax.ws.rs.NotFoundException;
 
 import org.onap.clamp.clds.dao.CldsDao;
+import org.onap.clamp.clds.util.JacksonUtils;
 
 /**
  * Represent a CLDS Model.
@@ -64,6 +67,9 @@ public class CldsModel {
     private String status;
     private List<String> permittedActionCd;
     private List<CldsModelInstance> cldsModelInstanceList;
+    /**
+     * The service type Id received from DCAE by querying it
+     */
     private String typeId;
     private String typeName;
     private String deploymentId;
@@ -92,8 +98,8 @@ public class CldsModel {
     public boolean canInventoryCall() {
         boolean canCall = false;
         /* Below checks the clds event is submit/resubmit */
-
-        if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT))) {
+        if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT)
+                || event.isActionCd(CldsEvent.ACTION_SUBMITDCAE))) {
             canCall = true;
         }
         return canCall;
@@ -102,17 +108,17 @@ public class CldsModel {
     /**
      * Save model to DB.
      */
-    public void save(CldsDao cldsDao, String userid) {
-        cldsDao.setModel(this, userid);
+    public CldsModel save(CldsDao cldsDao, String userid) {
+        CldsModel cldsModel = cldsDao.setModel(this, userid);
         determineStatus();
         determinePermittedActionCd();
+        return cldsModel;
     }
 
     /**
      * set the status in the model
      */
     private void determineStatus() {
-
         status = STATUS_UNKNOWN;
         if (event == null || event.getActionCd() == null) {
             status = STATUS_DESIGN;
@@ -121,6 +127,7 @@ public class CldsModel {
         } else if (event.isActionAndStateCd(CldsEvent.ACTION_CREATE, CldsEvent.ACTION_STATE_ANY)
                 || event.isActionAndStateCd(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_STATE_ANY)
                 || event.isActionAndStateCd(CldsEvent.ACTION_RESUBMIT, CldsEvent.ACTION_STATE_ANY)
+                || event.isActionAndStateCd(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_STATE_ANY)
                 || event.isActionAndStateCd(CldsEvent.ACTION_DELETE, CldsEvent.ACTION_STATE_RECEIVED)) {
             status = STATUS_DESIGN;
         } else if (event.isActionAndStateCd(CldsEvent.ACTION_DISTRIBUTE, CldsEvent.ACTION_STATE_RECEIVED)
@@ -136,7 +143,6 @@ public class CldsModel {
         } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
             status = STATUS_STOPPED;
         }
-
     }
 
     /**
@@ -176,6 +182,9 @@ public class CldsModel {
         switch (actionCd) {
             case CldsEvent.ACTION_CREATE:
                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMIT, CldsEvent.ACTION_TEST);
+                if (isSimplifiedModel()) {
+                    permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE, CldsEvent.ACTION_TEST);
+                }
                 break;
             case CldsEvent.ACTION_SUBMIT:
             case CldsEvent.ACTION_RESUBMIT:
@@ -183,12 +192,22 @@ public class CldsModel {
                 // requires manually deleting artifact from sdc
                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_RESUBMIT);
                 break;
+            case CldsEvent.ACTION_SUBMITDCAE:
+                permittedActionCd = Arrays.asList(CldsEvent.ACTION_SUBMITDCAE);
+                break;
             case CldsEvent.ACTION_DISTRIBUTE:
                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_RESUBMIT);
+                if (isSimplifiedModel()) {
+                    permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_SUBMITDCAE);
+                }
                 break;
             case CldsEvent.ACTION_UNDEPLOY:
                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
                         CldsEvent.ACTION_RESUBMIT);
+                if (isSimplifiedModel()) {
+                    permittedActionCd = Arrays.asList(CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_DEPLOY,
+                            CldsEvent.ACTION_SUBMITDCAE);
+                }
                 break;
             case CldsEvent.ACTION_DEPLOY:
                 permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
@@ -217,6 +236,22 @@ public class CldsModel {
         }
     }
 
+    private boolean isSimplifiedModel() {
+        boolean result = false;
+        try {
+            if (propText != null) {
+                JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(propText);
+                JsonNode simpleModelJson = modelJson.get("simpleModel");
+                if (simpleModelJson != null && simpleModelJson.asBoolean()) {
+                    result = true;
+                }
+            }
+        } catch (IOException e) {
+            logger.error("Error while parsing propText json", e);
+        }
+        return result;
+    }
+
     /**
      * Validate requestedActionCd - determine permittedActionCd and then check
      * if contained in permittedActionCd Throw IllegalArgumentException if
@@ -454,4 +489,7 @@ public class CldsModel {
         this.deploymentId = deploymentId;
     }
 
+    public List<String> getPermittedActionCd() {
+        return permittedActionCd;
+    }
 }