Rework the policy refresh
[clamp.git] / src / main / java / org / onap / clamp / loop / template / LoopElementModel.java
index 7f00c42..4a46a95 100644 (file)
 package org.onap.clamp.loop.template;
 
 import com.google.gson.annotations.Expose;
-
 import java.io.Serializable;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
-
 import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -41,18 +39,22 @@ import javax.persistence.JoinTable;
 import javax.persistence.ManyToMany;
 import javax.persistence.OneToMany;
 import javax.persistence.Table;
-
 import org.hibernate.annotations.SortNatural;
+import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
+import org.onap.clamp.loop.Loop;
 import org.onap.clamp.loop.common.AuditEntity;
+import org.onap.clamp.policy.Policy;
+import org.onap.clamp.policy.microservice.MicroServicePolicy;
+import org.onap.clamp.policy.operational.OperationalPolicy;
 
 /**
- * This class represents a micro service model for a loop template.
+ * This class represents a micro service/operational/... model for a loop template.
+ * So it's an element in the flow (a box shown in the loop).
  */
 
 @Entity
 @Table(name = "loop_element_models")
 public class LoopElementModel extends AuditEntity implements Serializable {
-    public static final String DEFAULT_GROUP_NAME = "DEFAULT";
     /**
      * The serial version id.
      */
@@ -63,28 +65,45 @@ public class LoopElementModel extends AuditEntity implements Serializable {
     @Column(nullable = false, name = "name", unique = true)
     private String name;
 
+    @Expose
+    @Column(name = "dcae_blueprint_id")
+    private String dcaeBlueprintId;
+
     /**
-     * Here we store the blueprint coming from DCAE.
+     * Here we store the blueprint coming from DCAE, it can be null if this is not a micro service model.
      */
-    @Column(nullable = false, name = "blueprint_yaml")
+    @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml")
     private String blueprint;
 
+    public static String MICRO_SERVICE_TYPE = "MICRO_SERVICE_TYPE";
+    public static String OPERATIONAL_POLICY_TYPE = "OPERATIONAL_POLICY_TYPE";
     /**
      * The type of element.
      */
     @Column(nullable = false, name = "loop_element_type")
     private String loopElementType;
 
+    /**
+     * This variable is used to display the micro-service name in the SVG.
+     */
+    @Expose
+    @Column(name = "short_name")
+    private String shortName;
+
     /**
      * This variable is used to store the type mentioned in the micro-service
      * blueprint.
      */
     @Expose
-    @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
-    @JoinTable(name = "loopelementmodels_to_policymodels",
+    @ManyToMany(
+            fetch = FetchType.EAGER,
+            cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
+    @JoinTable(
+            name = "loopelementmodels_to_policymodels",
             joinColumns = @JoinColumn(name = "loop_element_name", referencedColumnName = "name"),
-            inverseJoinColumns = { @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
-                @JoinColumn(name = "policy_model_version", referencedColumnName = "version") })
+            inverseJoinColumns = {
+                    @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
+                    @JoinColumn(name = "policy_model_version", referencedColumnName = "version")})
     @SortNatural
     private SortedSet<PolicyModel> policyModels = new TreeSet<>();
 
@@ -93,7 +112,7 @@ public class LoopElementModel extends AuditEntity implements Serializable {
 
     /**
      * policyModels getter.
-     * 
+     *
      * @return the policyModel
      */
     public SortedSet<PolicyModel> getPolicyModels() {
@@ -102,7 +121,7 @@ public class LoopElementModel extends AuditEntity implements Serializable {
 
     /**
      * Method to add a new policyModel to the list.
-     * 
+     *
      * @param policyModel The policy model
      */
     public void addPolicyModel(PolicyModel policyModel) {
@@ -112,7 +131,7 @@ public class LoopElementModel extends AuditEntity implements Serializable {
 
     /**
      * name getter.
-     * 
+     *
      * @return the name
      */
     public String getName() {
@@ -121,7 +140,7 @@ public class LoopElementModel extends AuditEntity implements Serializable {
 
     /**
      * name setter.
-     * 
+     *
      * @param name the name to set
      */
     public void setName(String name) {
@@ -130,7 +149,7 @@ public class LoopElementModel extends AuditEntity implements Serializable {
 
     /**
      * blueprint getter.
-     * 
+     *
      * @return the blueprint
      */
     public String getBlueprint() {
@@ -139,16 +158,34 @@ public class LoopElementModel extends AuditEntity implements Serializable {
 
     /**
      * blueprint setter.
-     * 
+     *
      * @param blueprint the blueprint to set
      */
     public void setBlueprint(String blueprint) {
         this.blueprint = blueprint;
     }
 
+    /**
+     * dcaeBlueprintId getter.
+     *
+     * @return the dcaeBlueprintId
+     */
+    public String getDcaeBlueprintId() {
+        return dcaeBlueprintId;
+    }
+
+    /**
+     * dcaeBlueprintId setter.
+     *
+     * @param dcaeBlueprintId the dcaeBlueprintId to set
+     */
+    public void setDcaeBlueprintId(String dcaeBlueprintId) {
+        this.dcaeBlueprintId = dcaeBlueprintId;
+    }
+
     /**
      * loopElementType getter.
-     * 
+     *
      * @return the loopElementType
      */
     public String getLoopElementType() {
@@ -157,16 +194,32 @@ public class LoopElementModel extends AuditEntity implements Serializable {
 
     /**
      * loopElementType setter.
-     * 
+     *
      * @param loopElementType the loopElementType to set
      */
     public void setLoopElementType(String loopElementType) {
         this.loopElementType = loopElementType;
     }
 
+    /**
+     * shortName getter.
+     *
+     * @return the shortName
+     */
+    public String getShortName() {
+        return shortName;
+    }
+
+    /**
+     * @param shortName the shortName to set.
+     */
+    public void setShortName(String shortName) {
+        this.shortName = shortName;
+    }
+
     /**
      * usedByLoopTemplates getter.
-     * 
+     *
      * @return the usedByLoopTemplates
      */
     public Set<LoopTemplateLoopElementModel> getUsedByLoopTemplates() {
@@ -181,7 +234,7 @@ public class LoopElementModel extends AuditEntity implements Serializable {
 
     /**
      * Constructor.
-     * 
+     *
      * @param name            The name id
      * @param loopElementType The type of loop element
      * @param blueprint       The blueprint defined for dcae that contains the
@@ -193,6 +246,23 @@ public class LoopElementModel extends AuditEntity implements Serializable {
         this.blueprint = blueprint;
     }
 
+    /**
+     * Create a policy instance from the current loop element model.
+     *
+     * @return A Policy object.
+     */
+    public Policy createPolicyInstance(Loop loop, ToscaConverterWithDictionarySupport toscaConverter) {
+        if (LoopElementModel.MICRO_SERVICE_TYPE.equals(this.getLoopElementType())) {
+            return new MicroServicePolicy(loop, loop.getModelService(), this, toscaConverter);
+        }
+        else if (LoopElementModel.OPERATIONAL_POLICY_TYPE.equals(this.getLoopElementType())) {
+            return new OperationalPolicy(loop, loop.getModelService(), this, toscaConverter);
+        }
+        else {
+            return null;
+        }
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -217,7 +287,8 @@ public class LoopElementModel extends AuditEntity implements Serializable {
             if (other.name != null) {
                 return false;
             }
-        } else if (!name.equals(other.name)) {
+        }
+        else if (!name.equals(other.name)) {
             return false;
         }
         return true;