Fix the Sdc Controller
[clamp.git] / src / main / java / org / onap / clamp / clds / model / CldsModel.java
index 59db9b5..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;
 
+import javax.ws.rs.BadRequestException;
 import javax.ws.rs.NotFoundException;
 
-import org.jboss.resteasy.spi.BadRequestException;
 import org.onap.clamp.clds.dao.CldsDao;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
+import org.onap.clamp.clds.util.JacksonUtils;
 
 /**
  * Represent a CLDS Model.
  */
 public class CldsModel {
-    protected static final EELFLogger logger             = EELFManager.getInstance().getLogger(CldsModel.class);
-    protected static final EELFLogger metricsLogger      = EELFManager.getInstance().getMetricsLogger();
-
-    private static final int        UUID_LENGTH        = 36;
-
-    public static final String      STATUS_DESIGN      = "DESIGN";
-    public static final String      STATUS_DISTRIBUTED = "DISTRIBUTED";
-    public static final String      STATUS_ACTIVE      = "ACTIVE";
-    public static final String      STATUS_STOPPED     = "STOPPED";
-    public static final String      STATUS_DELETING    = "DELETING";
-    public static final String      STATUS_ERROR       = "ERROR";                                             // manual
-                                                                                                              // intervention
-                                                                                                              // required
-    public static final String      STATUS_UNKNOWN     = "UNKNOWN";
-
-    private String                  id;
-    private String                  templateId;
-    private String                  templateName;
-    private String                  name;
-    private String                  controlNamePrefix;
-    private String                  controlNameUuid;
-    private String                  bpmnId;
-    private String                  bpmnUserid;
-    private String                  bpmnText;
-    private String                  propId;
-    private String                  propUserid;
-    private String                  propText;
-    private String                  imageId;
-    private String                  imageUserid;
-    private String                  imageText;
-    private String                  docId;
-    private String                  docUserid;
-    private String                  docText;
-    private String                  blueprintId;
-    private String                  blueprintUserid;
-    private String                  blueprintText;
-    private CldsEvent               event;
-    private String                  status;
-    private List<String>            permittedActionCd;
-    private List<CldsModelInstance> cldsModelInstanceList;
-
-    private String                  typeId;
-    private String                  typeName;
 
-    private String                  dispatcherResponse;
-
-    private String                  deploymentId;
-
-    private boolean                 userAuthorizedToUpdate;
+    private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsModel.class);
+    private static final int UUID_LENGTH = 36;
+    private static final String STATUS_DESIGN = "DESIGN";
+    private static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
+    private static final String STATUS_ACTIVE = "ACTIVE";
+    private static final String STATUS_STOPPED = "STOPPED";
+    private static final String STATUS_DELETING = "DELETING";
+    private static final String STATUS_ERROR = "ERROR";
+    private static final String STATUS_UNKNOWN = "UNKNOWN";
+    private String id;
+    private String templateId;
+    private String templateName;
+    private String name;
+    private String controlNamePrefix;
+    private String controlNameUuid;
+    private String bpmnText;
+    private String propText;
+    private String imageText;
+    private String docText;
+    private String blueprintText;
+    private CldsEvent event;
+    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;
 
     /**
      * Construct empty model.
@@ -98,10 +83,6 @@ public class CldsModel {
 
     /**
      * Retrieve from DB.
-     *
-     * @param cldsDao
-     * @param name
-     * @return
      */
     public static CldsModel retrieve(CldsDao cldsDao, String name, boolean okIfNotFound) {
         // get from db
@@ -116,9 +97,9 @@ public class CldsModel {
 
     public boolean canInventoryCall() {
         boolean canCall = false;
-        /* Below checks the clds ecent is submit/resubmit */
-
-        if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT))) {
+        /* Below checks the clds event is submit/resubmit */
+        if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT)
+                || event.isActionCd(CldsEvent.ACTION_SUBMITDCAE))) {
             canCall = true;
         }
         return canCall;
@@ -126,47 +107,18 @@ public class CldsModel {
 
     /**
      * Save model to DB.
-     *
-     * @param cldsDao
-     * @param userid
      */
-    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();
-    }
-
-    /**
-     * Insert a new event for the new action. Throw IllegalArgumentException if
-     * requested actionCd is not permitted.
-     *
-     * @param cldsDao
-     * @param userid
-     * @param actionCd
-     * @param actionStateCd
-     */
-    public void insEvent(CldsDao cldsDao, String userid, String actionCd, String actionStateCd) {
-        validateAction(actionCd);
-        event = CldsEvent.insEvent(cldsDao, this, userid, actionCd, actionStateCd, null);
-        determineStatus();
-        determinePermittedActionCd();
-    }
-
-    /**
-     * Update event with processInstanceId
-     *
-     * @param cldsDao
-     * @param processInstanceId
-     */
-    public void updEvent(CldsDao cldsDao, String processInstanceId) {
-        cldsDao.updEvent(event.getId(), processInstanceId);
+        return cldsModel;
     }
 
     /**
      * set the status in the model
      */
     private void determineStatus() {
-
         status = STATUS_UNKNOWN;
         if (event == null || event.getActionCd() == null) {
             status = STATUS_DESIGN;
@@ -175,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)
@@ -190,14 +143,11 @@ public class CldsModel {
         } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
             status = STATUS_STOPPED;
         }
-
     }
 
     /**
      * Get the actionCd from current event. If none, default value is
      * CldsEvent.ACTION_CREATE
-     *
-     * @return
      */
     private String getCurrentActionCd() {
         // current default actionCd is CREATE
@@ -211,8 +161,6 @@ public class CldsModel {
     /**
      * Get the actionStateCd from current event. If none, default value is
      * CldsEvent.ACTION_STATE_COMPLETED
-     *
-     * @return
      */
     private String getCurrentActionStateCd() {
         // current default actionStateCd is CREATE
@@ -225,13 +173,18 @@ public class CldsModel {
 
     /**
      * Determine permittedActionCd list using the actionCd from the current
-     * event.
+     * event. It's a states graph, given the next action that can be executed
+     * from the one that has been executed (described in the event object).
+     * ACTION_CREATE being the first one.
      */
     private void determinePermittedActionCd() {
         String actionCd = getCurrentActionCd();
         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:
@@ -239,19 +192,32 @@ 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);
+                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, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
+                permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UNDEPLOY,
+                        CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP);
                 break;
             case CldsEvent.ACTION_RESTART:
             case CldsEvent.ACTION_UPDATE:
                 // for 1702 delete is not currently implemented
-                permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE, CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
+                permittedActionCd = Arrays.asList(CldsEvent.ACTION_DEPLOY, CldsEvent.ACTION_UPDATE,
+                        CldsEvent.ACTION_STOP, CldsEvent.ACTION_UNDEPLOY);
                 break;
             case CldsEvent.ACTION_DELETE:
                 if (getCurrentActionStateCd().equals(CldsEvent.ACTION_STATE_SENT)) {
@@ -270,12 +236,26 @@ 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
      * requested actionCd is not permitted.
-     *
-     * @param requestedActionCd
      */
     public void validateAction(String requestedActionCd) {
         determinePermittedActionCd();
@@ -291,24 +271,16 @@ public class CldsModel {
      * + controlNameUuid). No fields are populated other than controlNamePrefix
      * and controlNameUuid. Throws BadRequestException if length of given
      * control name is less than UUID_LENGTH.
-     *
-     * @param fullControlName
-     * @return
      */
     public static CldsModel createUsingControlName(String fullControlName) {
-
-        int len = 0;
-
-        if (fullControlName != null) {
-            len = fullControlName.length();
-        }
-        if (len < UUID_LENGTH) {
+        if (fullControlName == null || fullControlName.length() < UUID_LENGTH) {
             throw new BadRequestException(
-                    "closed loop id / control name length, " + len + ", less than the minimum of: " + UUID_LENGTH);
+                    "closed loop id / control name length, " + (fullControlName != null ? fullControlName.length() : 0)
+                            + ", less than the minimum of: " + UUID_LENGTH);
         }
         CldsModel model = new CldsModel();
-        model.setControlNamePrefix(fullControlName.substring(0, len - UUID_LENGTH));
-        model.setControlNameUuid(fullControlName.substring(len - UUID_LENGTH));
+        model.setControlNamePrefix(fullControlName.substring(0, fullControlName.length() - UUID_LENGTH));
+        model.setControlNameUuid(fullControlName.substring(fullControlName.length() - UUID_LENGTH));
         return model;
     }
 
@@ -321,9 +293,6 @@ public class CldsModel {
 
     /**
      * To insert modelInstance to the database
-     *
-     * @param cldsDao
-     * @param dcaeEvent
      */
     public static CldsModel insertModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent, String userid) {
         String controlName = dcaeEvent.getControlName();
@@ -340,24 +309,6 @@ public class CldsModel {
         return cldsModel;
     }
 
-    /**
-     * To remove modelInstance from the database This method is defunct - DCAE
-     * Proxy will not undeploy individual instances. It will send an empty list
-     * of deployed instances to indicate all have been removed. Or it will send
-     * an updated list to indicate those that are still deployed with any not on
-     * the list considered undeployed.
-     *
-     * @param cldsDao
-     * @param dcaeEvent
-     */
-    @SuppressWarnings("unused")
-    private static CldsModel removeModelInstance(CldsDao cldsDao, DcaeEvent dcaeEvent) {
-        String controlName = dcaeEvent.getControlName();
-        // cldsModel = cldsDao.delModelInstance(cldsModel.getControlNameUuid(),
-        // dcaeEvent.getInstances() );
-        return createUsingControlName(controlName);
-    }
-
     /**
      * @return the name
      */
@@ -380,10 +331,6 @@ public class CldsModel {
         return typeName;
     }
 
-    /**
-     * @param name
-     *            the typeName to set
-     */
     public void setTypeName(String typeName) {
         this.typeName = typeName;
     }
@@ -426,21 +373,6 @@ public class CldsModel {
         this.controlNameUuid = controlNameUuid;
     }
 
-    /**
-     * @return the propUserid
-     */
-    public String getPropUserid() {
-        return propUserid;
-    }
-
-    /**
-     * @param propUserid
-     *            the propUserid to set
-     */
-    public void setPropUserid(String propUserid) {
-        this.propUserid = propUserid;
-    }
-
     /**
      * @return the propText
      */
@@ -479,14 +411,6 @@ public class CldsModel {
         this.templateName = templateName;
     }
 
-    public String getPropId() {
-        return propId;
-    }
-
-    public void setPropId(String propId) {
-        this.propId = propId;
-    }
-
     /**
      * @param event
      *            the event to set
@@ -510,37 +434,6 @@ public class CldsModel {
         this.status = status;
     }
 
-    /**
-     * @return the permittedActionCd
-     */
-    public List<String> getPermittedActionCd() {
-        return permittedActionCd;
-    }
-
-    /**
-     * @param permittedActionCd
-     *            the permittedActionCd to set
-     */
-    public void setPermittedActionCd(List<String> permittedActionCd) {
-        this.permittedActionCd = permittedActionCd;
-    }
-
-    public String getBlueprintId() {
-        return blueprintId;
-    }
-
-    public void setBlueprintId(String blueprintId) {
-        this.blueprintId = blueprintId;
-    }
-
-    public String getBlueprintUserid() {
-        return blueprintUserid;
-    }
-
-    public void setBlueprintUserid(String blueprintUserid) {
-        this.blueprintUserid = blueprintUserid;
-    }
-
     public String getBlueprintText() {
         return blueprintText;
     }
@@ -549,22 +442,6 @@ public class CldsModel {
         this.blueprintText = blueprintText;
     }
 
-    public String getBpmnId() {
-        return bpmnId;
-    }
-
-    public void setBpmnId(String bpmnId) {
-        this.bpmnId = bpmnId;
-    }
-
-    public String getBpmnUserid() {
-        return bpmnUserid;
-    }
-
-    public void setBpmnUserid(String bpmnUserid) {
-        this.bpmnUserid = bpmnUserid;
-    }
-
     public String getBpmnText() {
         return bpmnText;
     }
@@ -573,22 +450,6 @@ public class CldsModel {
         this.bpmnText = bpmnText;
     }
 
-    public String getImageId() {
-        return imageId;
-    }
-
-    public void setImageId(String imageId) {
-        this.imageId = imageId;
-    }
-
-    public String getImageUserid() {
-        return imageUserid;
-    }
-
-    public void setImageUserid(String imageUserid) {
-        this.imageUserid = imageUserid;
-    }
-
     public String getImageText() {
         return imageText;
     }
@@ -597,22 +458,6 @@ public class CldsModel {
         this.imageText = imageText;
     }
 
-    public String getDocId() {
-        return docId;
-    }
-
-    public void setDocId(String docId) {
-        this.docId = docId;
-    }
-
-    public String getDocUserid() {
-        return docUserid;
-    }
-
-    public void setDocUserid(String docUserid) {
-        this.docUserid = docUserid;
-    }
-
     public String getDocText() {
         return docText;
     }
@@ -636,19 +481,6 @@ public class CldsModel {
         return cldsModelInstanceList;
     }
 
-    public void setCldsModelInstanceList(List<CldsModelInstance> cldsModelInstanceList) {
-        this.cldsModelInstanceList = cldsModelInstanceList;
-    }
-
-    public void setDispatcherResponse(String dispatcherResponse) {
-        this.dispatcherResponse = dispatcherResponse;
-
-    }
-
-    public String getDispatcherResponse() {
-        return this.dispatcherResponse;
-    }
-
     public String getDeploymentId() {
         return deploymentId;
     }
@@ -657,11 +489,7 @@ public class CldsModel {
         this.deploymentId = deploymentId;
     }
 
-    public boolean isUserAuthorizedToUpdate() {
-        return userAuthorizedToUpdate;
-    }
-
-    public void setUserAuthorizedToUpdate(boolean userAuthorizedToUpdate) {
-        this.userAuthorizedToUpdate = userAuthorizedToUpdate;
+    public List<String> getPermittedActionCd() {
+        return permittedActionCd;
     }
 }