diff options
author | xuegao <xg353y@intl.att.com> | 2020-03-19 16:24:10 +0100 |
---|---|---|
committer | xuegao <xg353y@intl.att.com> | 2020-03-19 17:01:59 +0100 |
commit | c405a83ec84c2bc1c090f06f9ac6207469e09cbb (patch) | |
tree | fc1a74f824cf69380bedb65b739ab33fdfeaf902 /src/main/java/org/onap | |
parent | a2625fe6c6819fc41a6f4f5a9625cd9ba753bc72 (diff) |
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 <xg353y@intl.att.com>
Diffstat (limited to 'src/main/java/org/onap')
-rw-r--r-- | src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java | 93 | ||||
-rw-r--r-- | src/main/java/org/onap/clamp/policy/Policy.java | 15 |
2 files changed, 68 insertions, 40 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 4cabe7f1f..99b021947 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 87d36f3d2..abb16d73f 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; |