From c405a83ec84c2bc1c090f06f9ac6207469e09cbb Mon Sep 17 00:00:00 2001 From: xuegao Date: Thu, 19 Mar 2020 16:24:10 +0100 Subject: Fix loop submit bugs 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 --- .../loop/components/external/PolicyComponent.java | 93 +++++++++++++++------- src/main/java/org/onap/clamp/policy/Policy.java | 15 +--- .../resources/clds/camel/routes/policy-flows.xml | 8 +- .../tosca/micro-service-policy-payload.json | 1 + .../tosca/micro-service-policy-properties.json | 44 +++++----- .../resources/tosca/pdp-group-policy-payload.json | 12 +-- 6 files changed, 102 insertions(+), 71 deletions(-) diff --git a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java index 4cabe7f1..99b02194 100644 --- a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java +++ b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java @@ -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>> pdpGroupMap = new HashMap >>(); 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>> updatePdpGroupMap (String pdpGroup, String pdpSubGroup, String policyName, + String policyModelVersion, HashMap>> pdpGroupMap){ + + JsonObject policyJson = new JsonObject(); + policyJson.addProperty("name", policyName); + policyJson.addProperty("version", policyModelVersion); + HashMap> pdpSubGroupMap; + List policyList; + if (pdpGroupMap.get(pdpGroup) == null) { + pdpSubGroupMap = new HashMap >(); + policyList = new LinkedList(); + } else { + pdpSubGroupMap = pdpGroupMap.get(pdpGroup); + if (pdpSubGroupMap.get(pdpSubGroup) == null) { + policyList = new LinkedList(); + } else { + policyList = (List)pdpSubGroupMap.get(pdpSubGroup); + } + } + policyList.add(policyJson); + pdpSubGroupMap.put(pdpSubGroup, policyList); + pdpGroupMap.put(pdpGroup, pdpSubGroupMap); + + return pdpGroupMap; + } + + private static JsonObject generateActivatePdpGroupPayload(HashMap>> pdpGroupMap) { + JsonArray payloadArray = new JsonArray(); + for (Entry>> 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> 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; } /** diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java index 87d36f3d..abb16d73 100644 --- a/src/main/java/org/onap/clamp/policy/Policy.java +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -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; diff --git a/src/main/resources/clds/camel/routes/policy-flows.xml b/src/main/resources/clds/camel/routes/policy-flows.xml index 2b704ace..a2c7a4ca 100644 --- a/src/main/resources/clds/camel/routes/policy-flows.xml +++ b/src/main/resources/clds/camel/routes/policy-flows.xml @@ -597,9 +597,9 @@ + message="Endpoint to add policies to PDP Group: {{clamp.config.policy.pap.url}}/policy/pap/v1/pdps/deployments/batch"> + uri="{{clamp.config.policy.pap.url}}/policy/pap/v1/pdps/deployments/batch?bridgeEndpoint=true&throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&useSystemProperties=true&authUsername={{clamp.config.policy.pap.userName}}&authPassword={{clamp.config.policy.pap.password}}&connectionTimeToLive=5000&httpClient.connectTimeout=10000&httpClient.socketTimeout=20000&authenticationPreemptive=true&connectionClose=true"/> @@ -648,9 +648,9 @@ + message="Endpoint to delete policy from PDP Group: {{clamp.config.policy.pap.url}}/pdps/deployments/batch/${exchangeProperty[policyName]}/versions/1.0.0"> + uri="{{clamp.config.policy.pap.url}}/policy/pap/v1/pdps/deployments/batch/${exchangeProperty[policyName]}/versions/1.0.0?bridgeEndpoint=true&useSystemProperties=true&mapHttpMessageHeaders=false&throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&authUsername={{clamp.config.policy.pap.userName}}&authPassword={{clamp.config.policy.pap.password}}&connectionTimeToLive=5000&httpClient.connectTimeout=10000&httpClient.socketTimeout=20000&authenticationPreemptive=true&connectionClose=true"/> ${exchangeProperty[policyName]} PDP Group removal status diff --git a/src/test/resources/tosca/micro-service-policy-payload.json b/src/test/resources/tosca/micro-service-policy-payload.json index 2de06b08..2533a541 100644 --- a/src/test/resources/tosca/micro-service-policy-payload.json +++ b/src/test/resources/tosca/micro-service-policy-payload.json @@ -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" diff --git a/src/test/resources/tosca/micro-service-policy-properties.json b/src/test/resources/tosca/micro-service-policy-properties.json index 6baa3294..04fe0cc7 100644 --- a/src/test/resources/tosca/micro-service-policy-properties.json +++ b/src/test/resources/tosca/micro-service-policy-properties.json @@ -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" + } + ] + } } diff --git a/src/test/resources/tosca/pdp-group-policy-payload.json b/src/test/resources/tosca/pdp-group-policy-payload.json index 93a85457..4ea746de 100644 --- a/src/test/resources/tosca/pdp-group-policy-payload.json +++ b/src/test/resources/tosca/pdp-group-policy-payload.json @@ -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" } ] @@ -16,14 +16,14 @@ ] }, { - "name": "pdpGroup1", + "name": "pdpGroup2", "deploymentSubgroups": [ { - "pdpType": "pdpSubgroup1", + "pdpType": "pdpSubgroup2", "action": "POST", "policies": [ { - "name": "onap.policies.monitoring.test", + "name": "opPolicy", "version": "1.0.0" } ] -- cgit 1.2.3-korg