Fix loop submit bugs 74/103974/4
authorxuegao <xg353y@intl.att.com>
Thu, 19 Mar 2020 15:24:10 +0000 (16:24 +0100)
committerxuegao <xg353y@intl.att.com>
Thu, 19 Mar 2020 16:01:59 +0000 (17:01 +0100)
Update the payload for policy creation and pdp group activation and
update the policy url accordingly.

Issue-ID: CLAMP-787
Change-Id: If39eeaccf780f1222b99ffc4dd5d7ef1460c4962
Signed-off-by: xuegao <xg353y@intl.att.com>
src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java
src/main/java/org/onap/clamp/policy/Policy.java
src/main/resources/clds/camel/routes/policy-flows.xml
src/test/resources/tosca/micro-service-policy-payload.json
src/test/resources/tosca/micro-service-policy-properties.json
src/test/resources/tosca/pdp-group-policy-payload.json

index 4cabe7f..99b0219 100644 (file)
@@ -30,7 +30,10 @@ import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Map.Entry;
 
 import javax.persistence.Transient;
 
@@ -79,44 +82,78 @@ public class PolicyComponent extends ExternalComponent {
      * @return The json, payload to send
      */
     public static String createPoliciesPayloadPdpGroup(Loop loop) {
-        JsonObject jsonObject = new JsonObject();
-        JsonArray jsonArray = new JsonArray();
-        jsonObject.add("groups", jsonArray);
-
+        HashMap<String,HashMap<String, List<JsonObject>>> pdpGroupMap = new HashMap <String,HashMap<String, List<JsonObject>>>();
         for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) {
-            jsonArray.add(createPdpDeploymentPayload(opPolicy.getPdpGroup(), opPolicy.getPdpSubgroup(),
-                    opPolicy.getPolicyModel().getPolicyModelType(), opPolicy.getPolicyModel().getVersion()));
+            pdpGroupMap = updatePdpGroupMap(opPolicy.getPdpGroup(), opPolicy.getPdpSubgroup(),
+                  opPolicy.getName(),
+                  opPolicy.getPolicyModel().getVersion(), pdpGroupMap);
         }
 
         for (MicroServicePolicy msPolicy : loop.getMicroServicePolicies()) {
-            jsonArray.add(createPdpDeploymentPayload(msPolicy.getPdpGroup(), msPolicy.getPdpSubgroup(),
-                    msPolicy.getPolicyModel().getPolicyModelType(), msPolicy.getPolicyModel().getVersion()));
+            pdpGroupMap = updatePdpGroupMap(msPolicy.getPdpGroup(), msPolicy.getPdpSubgroup(),
+                    msPolicy.getName(),
+                    msPolicy.getPolicyModel().getVersion(), pdpGroupMap);
         }
 
-        String payload = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
+        String payload = new GsonBuilder().setPrettyPrinting().create()
+              .toJson(generateActivatePdpGroupPayload(pdpGroupMap));
         logger.info("PdpGroup policy payload: " + payload);
         return payload;
     }
 
-    private static JsonObject createPdpDeploymentPayload(String pdpGroup, String pdpSubGroup,
-            String policyType, String version) {
-        JsonObject pdpGroupNode = new JsonObject();
-        JsonArray subPdpArray = new JsonArray();
-        pdpGroupNode.addProperty("name", pdpGroup);
-        pdpGroupNode.add("deploymentSubgroups", subPdpArray);
-
-        JsonObject pdpSubGroupNode = new JsonObject();
-        subPdpArray.add(pdpSubGroupNode);
-        pdpSubGroupNode.addProperty("pdpType", pdpSubGroup);
-        pdpSubGroupNode.addProperty("action", "POST");
-
-        JsonArray policyArray = new JsonArray();
-        pdpSubGroupNode.add("policies", policyArray);
-        JsonObject policyNode = new JsonObject();
-        policyNode.addProperty("name", policyType);
-        policyNode.addProperty("version", version);
-        policyArray.add(policyNode);
-        return pdpGroupNode;
+    private static HashMap<String,HashMap<String, List<JsonObject>>> updatePdpGroupMap (String pdpGroup, String pdpSubGroup, String policyName,
+        String policyModelVersion, HashMap<String,HashMap<String, List<JsonObject>>> pdpGroupMap){
+
+        JsonObject policyJson = new JsonObject();
+        policyJson.addProperty("name", policyName);
+        policyJson.addProperty("version", policyModelVersion);
+        HashMap<String, List<JsonObject>> pdpSubGroupMap;
+        List<JsonObject> policyList;
+        if (pdpGroupMap.get(pdpGroup) == null) {
+            pdpSubGroupMap = new HashMap <String, List<JsonObject>>();
+            policyList = new LinkedList<JsonObject>();
+        } else {
+            pdpSubGroupMap = pdpGroupMap.get(pdpGroup);
+            if (pdpSubGroupMap.get(pdpSubGroup) == null) {
+                policyList = new LinkedList<JsonObject>();
+            } else {
+                policyList = (List<JsonObject>)pdpSubGroupMap.get(pdpSubGroup);
+            }
+        }
+        policyList.add(policyJson);
+        pdpSubGroupMap.put(pdpSubGroup, policyList);
+        pdpGroupMap.put(pdpGroup, pdpSubGroupMap);
+
+        return pdpGroupMap;
+    }
+
+    private static JsonObject generateActivatePdpGroupPayload(HashMap<String,HashMap<String, List<JsonObject>>> pdpGroupMap) {
+        JsonArray payloadArray = new JsonArray();
+        for (Entry<String, HashMap<String, List<JsonObject>>> pdpGroupInfo : pdpGroupMap.entrySet()) {
+            JsonObject pdpGroupNode = new JsonObject();
+            JsonArray subPdpArray = new JsonArray();
+            pdpGroupNode.addProperty("name", pdpGroupInfo.getKey());
+            pdpGroupNode.add("deploymentSubgroups", subPdpArray);
+
+            JsonObject pdpSubGroupNode = new JsonObject();
+            subPdpArray.add(pdpSubGroupNode);
+
+            for (Entry<String, List<JsonObject>> pdpSubGroupInfo : pdpGroupInfo.getValue().entrySet()) {
+                pdpSubGroupNode.addProperty("pdpType", pdpSubGroupInfo.getKey());
+                pdpSubGroupNode.addProperty("action", "POST");
+
+                JsonArray policyArray = new JsonArray();
+                pdpSubGroupNode.add("policies", policyArray);
+
+                for (JsonObject policy : pdpSubGroupInfo.getValue()) {
+                    policyArray.add(policy);
+                }
+            }
+            payloadArray.add(pdpGroupNode);
+        }
+        JsonObject jsonObject = new JsonObject();
+        jsonObject.add("groups", payloadArray);
+        return jsonObject;
     }
 
     /**
index 87d36f3..abb16d7 100644 (file)
@@ -97,13 +97,6 @@ public abstract class Policy extends AuditEntity {
         return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
     }
 
-    private String getModelPropertyNameFromTosca(JsonObject object, String policyModelType) {
-        return object.getAsJsonObject("policy_types").getAsJsonObject(policyModelType)
-                .getAsJsonObject(
-                        "properties")
-                .keySet().toArray(new String[1])[0];
-    }
-
     /**
      * This method create the policy payload that must be sent to PEF.
      *
@@ -129,17 +122,15 @@ public abstract class Policy extends AuditEntity {
         JsonObject policyDetails = new JsonObject();
         thisPolicy.add(this.getName(), policyDetails);
         policyDetails.addProperty("type", this.getPolicyModel().getPolicyModelType());
+        policyDetails.addProperty("type_version", this.getPolicyModel().getVersion());
         policyDetails.addProperty("version", this.getPolicyModel().getVersion());
 
         JsonObject policyMetadata = new JsonObject();
         policyDetails.add("metadata", policyMetadata);
         policyMetadata.addProperty("policy-id", this.getName());
 
-        JsonObject policyProperties = new JsonObject();
-        policyDetails.add("properties", policyProperties);
-        policyProperties
-                .add(this.getModelPropertyNameFromTosca(toscaJson, this.getPolicyModel().getPolicyModelType()),
-                        this.getConfigurationsJson());
+        policyDetails.add("properties", this.getConfigurationsJson());
+
         String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
         logger.info("Policy payload: " + policyPayload);
         return policyPayload;
index 2b704ac..a2c7a4c 100644 (file)
                                                                </simple>
                                                </setHeader>
                                                <log loggingLevel="INFO"
-                                                               message="Endpoint to add policies to PDP Group: {{clamp.config.policy.pap.url}}/policy/pap/v1/pdps/policies"></log>
+                                                               message="Endpoint to add policies to PDP Group: {{clamp.config.policy.pap.url}}/policy/pap/v1/pdps/deployments/batch"></log>
                                                <toD
-                                                               uri="{{clamp.config.policy.pap.url}}/policy/pap/v1/pdps/policies?bridgeEndpoint=true&amp;throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&amp;useSystemProperties=true&amp;authUsername={{clamp.config.policy.pap.userName}}&amp;authPassword={{clamp.config.policy.pap.password}}&amp;connectionTimeToLive=5000&amp;httpClient.connectTimeout=10000&amp;httpClient.socketTimeout=20000&amp;authenticationPreemptive=true&amp;connectionClose=true"/>
+                                                               uri="{{clamp.config.policy.pap.url}}/policy/pap/v1/pdps/deployments/batch?bridgeEndpoint=true&amp;throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&amp;useSystemProperties=true&amp;authUsername={{clamp.config.policy.pap.userName}}&amp;authPassword={{clamp.config.policy.pap.password}}&amp;connectionTimeToLive=5000&amp;httpClient.connectTimeout=10000&amp;httpClient.socketTimeout=20000&amp;authenticationPreemptive=true&amp;connectionClose=true"/>
 
                                                <doFinally>
                                                                <to uri="direct:reset-raise-http-exception-flag"/>
                                                                                </simple>
                                                                </setHeader>
                                                                <log loggingLevel="INFO"
-                                                                               message="Endpoint to delete policy from PDP Group: {{clamp.config.policy.pap.url}}/pdps/policies/${exchangeProperty[policyName]}/versions/1.0.0"></log>
+                                                                               message="Endpoint to delete policy from PDP Group: {{clamp.config.policy.pap.url}}/pdps/deployments/batch/${exchangeProperty[policyName]}/versions/1.0.0"></log>
                                                                <toD
-                                                                               uri="{{clamp.config.policy.pap.url}}/policy/pap/v1/pdps/policies/${exchangeProperty[policyName]}/versions/1.0.0?bridgeEndpoint=true&amp;useSystemProperties=true&amp;mapHttpMessageHeaders=false&amp;throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&amp;authUsername={{clamp.config.policy.pap.userName}}&amp;authPassword={{clamp.config.policy.pap.password}}&amp;connectionTimeToLive=5000&amp;httpClient.connectTimeout=10000&amp;httpClient.socketTimeout=20000&amp;authenticationPreemptive=true&amp;connectionClose=true"/>
+                                                                               uri="{{clamp.config.policy.pap.url}}/policy/pap/v1/pdps/deployments/batch/${exchangeProperty[policyName]}/versions/1.0.0?bridgeEndpoint=true&amp;useSystemProperties=true&amp;mapHttpMessageHeaders=false&amp;throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&amp;authUsername={{clamp.config.policy.pap.userName}}&amp;authPassword={{clamp.config.policy.pap.password}}&amp;connectionTimeToLive=5000&amp;httpClient.connectTimeout=10000&amp;httpClient.socketTimeout=20000&amp;authenticationPreemptive=true&amp;connectionClose=true"/>
                                                                <setProperty propertyName="logMessage">
                                                                                <simple>${exchangeProperty[policyName]} PDP Group removal status
                                                                                </simple>
index 2de06b0..2533a54 100644 (file)
@@ -5,6 +5,7 @@
                        {
                                "testPolicy": {
                                        "type": "onap.policies.monitoring.cdap.tca.hi.lo.app",
+                                       "type_version": "1.0.0",
                                        "version": "1.0.0",
                                        "metadata": {
                                                "policy-id": "testPolicy"
index 6baa329..04fe0cc 100644 (file)
@@ -1,23 +1,25 @@
 {
-  "domain": "measurementsForVfScaling",
-  "metricsPerEventName": [
-    {
-      "policyVersion": "1.0.0",
-      "thresholds": [
-        {
-          "severity": "CRITICAL",
-          "fieldPath": "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta",
-          "thresholdValue": 1,
-          "closedLoopEventStatus": "ONSET",
-          "closedLoopControlName": "test",
-          "version": "1.0.0",
-          "direction": "LESS"
-        }
-      ],
-      "policyName": "test",
-      "controlLoopSchemaType": "VM",
-      "policyScope": "test",
-      "eventName": "test"
-    }
-  ]
+  "tca_policy": {
+    "domain": "measurementsForVfScaling",
+    "metricsPerEventName": [
+      {
+        "policyVersion": "1.0.0",
+        "thresholds": [
+          {
+            "severity": "CRITICAL",
+            "fieldPath": "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta",
+            "thresholdValue": 1,
+            "closedLoopEventStatus": "ONSET",
+            "closedLoopControlName": "test",
+            "version": "1.0.0",
+            "direction": "LESS"
+          }
+        ],
+        "policyName": "test",
+        "controlLoopSchemaType": "VM",
+        "policyScope": "test",
+        "eventName": "test"
+      }
+    ]
+  }
 }
index 93a8545..4ea746d 100644 (file)
@@ -1,14 +1,14 @@
 {
   "groups": [
     {
-      "name": "pdpGroup2",
+      "name": "pdpGroup1",
       "deploymentSubgroups": [
         {
-          "pdpType": "pdpSubgroup2",
+          "pdpType": "pdpSubgroup1",
           "action": "POST",
           "policies": [
             {
-              "name": "onap.policies.controlloop.Operational",
+              "name": "configPolicyTest",
               "version": "1.0.0"
             }
           ]
       ]
     },
     {
-      "name": "pdpGroup1",
+      "name": "pdpGroup2",
       "deploymentSubgroups": [
         {
-          "pdpType": "pdpSubgroup1",
+          "pdpType": "pdpSubgroup2",
           "action": "POST",
           "policies": [
             {
-              "name": "onap.policies.monitoring.test",
+              "name": "opPolicy",
               "version": "1.0.0"
             }
           ]