diff options
Diffstat (limited to 'src/main')
3 files changed, 72 insertions, 44 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<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; } /** 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 @@ </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&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"/> + 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"/> <doFinally> <to uri="direct:reset-raise-http-exception-flag"/> @@ -648,9 +648,9 @@ </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&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"/> + 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"/> <setProperty propertyName="logMessage"> <simple>${exchangeProperty[policyName]} PDP Group removal status </simple> |