Fix the op policy payload
[clamp.git] / src / main / java / org / onap / clamp / loop / Loop.java
index 910c5aa..6de2863 100644 (file)
 
 package org.onap.clamp.loop;
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import com.google.gson.annotations.Expose;
 
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import javax.persistence.CascadeType;
@@ -41,15 +47,17 @@ import javax.persistence.JoinColumn;
 import javax.persistence.JoinTable;
 import javax.persistence.ManyToMany;
 import javax.persistence.OneToMany;
+import javax.persistence.OrderBy;
 import javax.persistence.Table;
+import javax.persistence.Transient;
 
 import org.hibernate.annotations.Type;
 import org.hibernate.annotations.TypeDef;
 import org.hibernate.annotations.TypeDefs;
+import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
 import org.onap.clamp.loop.log.LoopLog;
 import org.onap.clamp.policy.microservice.MicroServicePolicy;
 import org.onap.clamp.policy.operational.OperationalPolicy;
-import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
 
 @Entity
 @Table(name = "loops")
@@ -57,10 +65,13 @@ import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
 public class Loop implements Serializable {
 
     /**
-     *
+     * The serial version id.
      */
     private static final long serialVersionUID = -286522707701388642L;
 
+    @Transient
+    private static final EELFLogger logger = EELFManager.getInstance().getLogger(Loop.class);
+
     @Id
     @Expose
     @Column(nullable = false, name = "name", unique = true)
@@ -78,7 +89,7 @@ public class Loop implements Serializable {
     @Column(name = "dcae_blueprint_id")
     private String dcaeBlueprintId;
 
-    @Column(name = "svg_representation")
+    @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
     private String svgRepresentation;
 
     @Expose
@@ -86,7 +97,12 @@ public class Loop implements Serializable {
     @Column(columnDefinition = "json", name = "global_properties_json")
     private JsonObject globalPropertiesJson;
 
-    @Column(nullable = false, name = "blueprint_yaml")
+    @Expose
+    @Type(type = "json")
+    @Column(columnDefinition = "json", name = "model_properties_json")
+    private JsonObject modelPropertiesJson;
+
+    @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
     private String blueprint;
 
     @Expose
@@ -105,11 +121,15 @@ public class Loop implements Serializable {
 
     @Expose
     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
+    @OrderBy("id DESC")
     private Set<LoopLog> loopLogs = new HashSet<>();
 
     public Loop() {
     }
 
+    /**
+     * Constructor.
+     */
     public Loop(String name, String blueprint, String svgRepresentation) {
         this.name = name;
         this.svgRepresentation = svgRepresentation;
@@ -158,7 +178,7 @@ public class Loop implements Serializable {
         this.blueprint = blueprint;
     }
 
-    LoopState getLastComputedState() {
+    public LoopState getLastComputedState() {
         return lastComputedState;
     }
 
@@ -166,7 +186,7 @@ public class Loop implements Serializable {
         this.lastComputedState = lastComputedState;
     }
 
-    Set<OperationalPolicy> getOperationalPolicies() {
+    public Set<OperationalPolicy> getOperationalPolicies() {
         return operationalPolicies;
     }
 
@@ -174,7 +194,7 @@ public class Loop implements Serializable {
         this.operationalPolicies = operationalPolicies;
     }
 
-    Set<MicroServicePolicy> getMicroServicePolicies() {
+    public Set<MicroServicePolicy> getMicroServicePolicies() {
         return microServicePolicies;
     }
 
@@ -182,7 +202,7 @@ public class Loop implements Serializable {
         this.microServicePolicies = microServicePolicies;
     }
 
-    JsonObject getGlobalPropertiesJson() {
+    public JsonObject getGlobalPropertiesJson() {
         return globalPropertiesJson;
     }
 
@@ -190,7 +210,7 @@ public class Loop implements Serializable {
         this.globalPropertiesJson = globalPropertiesJson;
     }
 
-    Set<LoopLog> getLoopLogs() {
+    public Set<LoopLog> getLoopLogs() {
         return loopLogs;
     }
 
@@ -217,10 +237,79 @@ public class Loop implements Serializable {
         return dcaeBlueprintId;
     }
 
-    public void setDcaeBlueprintId(String dcaeBlueprintId) {
+    void setDcaeBlueprintId(String dcaeBlueprintId) {
         this.dcaeBlueprintId = dcaeBlueprintId;
     }
 
+    public JsonObject getModelPropertiesJson() {
+        return modelPropertiesJson;
+    }
+
+    void setModelPropertiesJson(JsonObject modelPropertiesJson) {
+        this.modelPropertiesJson = modelPropertiesJson;
+    }
+
+    /**
+     * Generate the loop name.
+     *
+     * @param serviceName
+     *        The service name
+     * @param serviceVersion
+     *        The service version
+     * @param resourceName
+     *        The resource name
+     * @param blueprintFileName
+     *        The blueprint file name
+     * @return The generated loop name
+     */
+    static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
+        String blueprintFilename) {
+        StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
+            .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
+        return buffer.toString().replace('.', '_').replaceAll(" ", "");
+    }
+
+    /**
+     * Generates the Json that must be sent to policy to add all policies to Active
+     * PDP group.
+     *
+     * @return The json, payload to send
+     */
+    public String createPoliciesPayloadPdpGroup() {
+        JsonObject jsonObject = new JsonObject();
+        JsonArray jsonArray = new JsonArray();
+        jsonObject.add("policies", jsonArray);
+
+        for (String policyName : this.listPolicyNamesPdpGroup()) {
+            JsonObject policyNode = new JsonObject();
+            jsonArray.add(policyNode);
+            policyNode.addProperty("policy-id", policyName);
+        }
+        String payload = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
+        logger.info("PdpGroup policy payload: " + payload);
+        return payload;
+    }
+
+    /**
+     * Generates the list of policy names that must be send/remove to/from active
+     * PDP group.
+     *
+     * @return A list of policy names
+     */
+    public List<String> listPolicyNamesPdpGroup() {
+        List<String> policyNamesList = new ArrayList<>();
+        for (OperationalPolicy opPolicy : this.getOperationalPolicies()) {
+            policyNamesList.add(opPolicy.getName());
+            for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) {
+                policyNamesList.add(guardName);
+            }
+        }
+        for (MicroServicePolicy microServicePolicy : this.getMicroServicePolicies()) {
+            policyNamesList.add(microServicePolicy.getName());
+        }
+        return policyNamesList;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -231,18 +320,23 @@ public class Loop implements Serializable {
 
     @Override
     public boolean equals(Object obj) {
-        if (this == obj)
+        if (this == obj) {
             return true;
-        if (obj == null)
+        }
+        if (obj == null) {
             return false;
-        if (getClass() != obj.getClass())
+        }
+        if (getClass() != obj.getClass()) {
             return false;
+        }
         Loop other = (Loop) obj;
         if (name == null) {
-            if (other.name != null)
+            if (other.name != null) {
                 return false;
-        } else if (!name.equals(other.name))
+            }
+        } else if (!name.equals(other.name)) {
             return false;
+        }
         return true;
     }