From 3854d7c1008aa947e4b532bbef9d9efc738f6928 Mon Sep 17 00:00:00 2001 From: sebdet Date: Wed, 15 Apr 2020 00:43:20 +0200 Subject: Fix legacy policy submit issues Operational legacy policy does not support semantic versionin for get status and delete policies, it has to be changed to integer Issue-ID: CLAMP-820 Signed-off-by: sebdet Change-Id: I78ff69b7502900d96fcc605fe997dd564dfa3e98 --- .../loop/components/external/PolicyComponent.java | 46 ++- src/main/java/org/onap/clamp/policy/Policy.java | 2 + .../policy/microservice/MicroServicePolicy.java | 5 + .../resources/clds/camel/routes/loop-flows.xml | 23 +- .../resources/clds/camel/routes/policy-flows.xml | 322 ++++----------------- .../org/onap/clamp/loop/PolicyComponentTest.java | 17 +- .../resources/tosca/pdp-group-policy-payload.json | 8 + 7 files changed, 128 insertions(+), 295 deletions(-) (limited to 'src') 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 8270a96e3..227f40a72 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 @@ -28,7 +28,6 @@ import com.att.eelf.configuration.EELFManager; import com.google.gson.GsonBuilder; 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; @@ -76,25 +75,33 @@ public class PolicyComponent extends ExternalComponent { /** * Generates the Json that must be sent to policy to add all policies to Active * PDP group. - * + * @param loop the loop object + * @param action POST (to add policy to group) or DELETE (to delete policy from group) * @return The json, payload to send */ - public static String createPoliciesPayloadPdpGroup(Loop loop) { + public static String createPoliciesPayloadPdpGroup(Loop loop, String action) { Map>> pdpGroupMap = new HashMap<>(); for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) { updatePdpGroupMap(opPolicy.getPdpGroup(), opPolicy.getPdpSubgroup(), opPolicy.getName(), - opPolicy.getPolicyModel().getVersion(), pdpGroupMap); + "1.0.0", pdpGroupMap); + if (opPolicy.isLegacy()) { + for (String guardName:opPolicy.createGuardPolicyPayloads().keySet()) { + updatePdpGroupMap(opPolicy.getPdpGroup(), opPolicy.getPdpSubgroup(), + guardName, + "1.0.0", pdpGroupMap); + } + } } for (MicroServicePolicy msPolicy : loop.getMicroServicePolicies()) { updatePdpGroupMap(msPolicy.getPdpGroup(), msPolicy.getPdpSubgroup(), msPolicy.getName(), - msPolicy.getPolicyModel().getVersion(), pdpGroupMap); + "1.0.0", pdpGroupMap); } String payload = new GsonBuilder().setPrettyPrinting().create() - .toJson(generateActivatePdpGroupPayload(pdpGroupMap)); + .toJson(generateActivatePdpGroupPayload(pdpGroupMap, action)); logger.info("PdpGroup policy payload: " + payload); return payload; } @@ -102,12 +109,12 @@ public class PolicyComponent extends ExternalComponent { private static void updatePdpGroupMap(String pdpGroup, String pdpSubGroup, String policyName, - String policyModelVersion, + String policyVersion, Map>> pdpGroupMap) { JsonObject policyJson = new JsonObject(); policyJson.addProperty("name", policyName); - policyJson.addProperty("version", policyModelVersion); + policyJson.addProperty("version", policyVersion); Map> pdpSubGroupMap; List policyList; if (pdpGroupMap.get(pdpGroup) == null) { @@ -129,7 +136,7 @@ public class PolicyComponent extends ExternalComponent { } private static JsonObject generateActivatePdpGroupPayload( - Map>> pdpGroupMap) { + Map>> pdpGroupMap, String action) { JsonArray payloadArray = new JsonArray(); for (Entry>> pdpGroupInfo : pdpGroupMap.entrySet()) { JsonObject pdpGroupNode = new JsonObject(); @@ -141,7 +148,7 @@ public class PolicyComponent extends ExternalComponent { JsonObject pdpSubGroupNode = new JsonObject(); subPdpArray.add(pdpSubGroupNode); pdpSubGroupNode.addProperty("pdpType", pdpSubGroupInfo.getKey()); - pdpSubGroupNode.addProperty("action", "POST"); + pdpSubGroupNode.addProperty("action", action); JsonArray policyArray = new JsonArray(); pdpSubGroupNode.add("policies", policyArray); @@ -157,25 +164,6 @@ public class PolicyComponent extends ExternalComponent { return jsonObject; } - /** - * Generates the list of policy names that must be send/remove to/from active - * PDP group. - * - * @return A list of policy names - */ - public static List listPolicyNamesPdpGroup(Loop loop) { - List policyNamesList = new ArrayList<>(); - for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) { - policyNamesList.add(opPolicy.getName()); - policyNamesList.addAll(opPolicy.createGuardPolicyPayloads().keySet()); - } - for (MicroServicePolicy microServicePolicy : loop.getMicroServicePolicies()) { - policyNamesList.add(microServicePolicy.getName()); - } - logger.info("Policies that will be removed from PDP: " + policyNamesList); - return policyNamesList; - } - private static ExternalComponentState findNewState(boolean found, boolean deployed) { ExternalComponentState newState = NOT_SENT; diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java index d84f2c8a0..65e88d182 100644 --- a/src/main/java/org/onap/clamp/policy/Policy.java +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -284,4 +284,6 @@ public abstract class Policy extends AuditEntity { .append(blueprintFilename.replaceAll(".yaml", "")); return buffer.toString().replace('.', '_').replaceAll(" ", ""); } + + public abstract Boolean isLegacy(); } diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java index 77627a31a..127f495cc 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -157,6 +157,11 @@ public class MicroServicePolicy extends Policy implements Serializable { this.getPolicyModel().getPolicyModelType(), serviceModel)); } + @Override + public Boolean isLegacy() { + return false; + } + public Boolean getShared() { return shared; } diff --git a/src/main/resources/clds/camel/routes/loop-flows.xml b/src/main/resources/clds/camel/routes/loop-flows.xml index 8c22743a3..2f4d9c8cf 100644 --- a/src/main/resources/clds/camel/routes/loop-flows.xml +++ b/src/main/resources/clds/camel/routes/loop-flows.xml @@ -40,6 +40,9 @@ ${body.getPolicyModel().getPolicyModelType()} + + ${body.getPolicyModel().getVersion()} + 1.0.0 @@ -61,9 +64,22 @@ ${body.getPolicyModel().getPolicyModelType()} - - 1.0.0 + + ${body.getPolicyModel().getVersion()} + + + ${body.isLegacy()} == true + + 1 + + + + + 1.0.0 + + + ${body} @@ -84,6 +100,9 @@ onap.policies.controlloop.Guard + + 1.0.0 + 1 diff --git a/src/main/resources/clds/camel/routes/policy-flows.xml b/src/main/resources/clds/camel/routes/policy-flows.xml index 80926722f..a61bc67fb 100644 --- a/src/main/resources/clds/camel/routes/policy-flows.xml +++ b/src/main/resources/clds/camel/routes/policy-flows.xml @@ -54,9 +54,9 @@ + message="Endpoint to get policy: {{clamp.config.policy.api.url}}/policy/api/v1/policytypes/${exchangeProperty[policyType]}/versions/${exchangeProperty[policyTypeVersion]}/policies/${exchangeProperty[policyName]}/versions/${exchangeProperty[policyVersion]}"> + uri="{{clamp.config.policy.api.url}}/policy/api/v1/policytypes/${exchangeProperty[policyType]}/versions/${exchangeProperty[policyTypeVersion]}/policies/${exchangeProperty[policyName]}/versions/${exchangeProperty[policyVersion]}?bridgeEndpoint=true&useSystemProperties=true&throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&authMethod=Basic&authUsername={{clamp.config.policy.api.userName}}&authPassword={{clamp.config.policy.api.password}}&connectionTimeToLive=5000&httpClient.connectTimeout=10000&httpClient.socketTimeout=20000&authenticationPreemptive=true&connectionClose=true"/> ${exchangeProperty[X-ONAP-PartnerName]} + + + ${body.isLegacy()} == true + + 1 + + + + + 1.0.0 + + + + message="Endpoint to delete policy: {{clamp.config.policy.api.url}}/policy/api/v1/policytypes/${exchangeProperty[policy].getPolicyModel().getPolicyModelType()}/versions/${exchangeProperty[policy].getPolicyModel().getVersion()}/policies/${exchangeProperty[policy].getName()}/versions/${exchangeProperty[policyVersion]}"> + uri="{{clamp.config.policy.api.url}}/policy/api/v1/policytypes/${exchangeProperty[policy].getPolicyModel().getPolicyModelType()}/versions/${exchangeProperty[policy].getPolicyModel().getVersion()}/policies/${exchangeProperty[policy].getName()}/versions/${exchangeProperty[policyVersion]}?bridgeEndpoint=true&useSystemProperties=true&mapHttpMessageHeaders=false&throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&deleteWithBody=false&mapHttpMessageBody=false&mapHttpMessageFormUrlEncodedBody=false&authUsername={{clamp.config.policy.api.userName}}&authPassword={{clamp.config.policy.api.password}}&connectionTimeToLive=5000&httpClient.connectTimeout=10000&httpClient.socketTimeout=20000&authenticationPreemptive=true&connectionClose=true"/> @@ -279,199 +292,6 @@ - - - - - - - ${exchangeProperty[microServicePolicy].createPolicyPayload()} - - - - POST - - - application/json - - - ${exchangeProperty[X-ONAP-RequestID]} - - - - ${exchangeProperty[X-ONAP-InvocationID]} - - - - ${exchangeProperty[X-ONAP-PartnerName]} - - - - - - - - - ${exchangeProperty[microServicePolicy].getName()} creation - status - - - - POLICY - - - - - - - - - - - - - null - - - DELETE - - - ${exchangeProperty[X-ONAP-RequestID]} - - - - ${exchangeProperty[X-ONAP-InvocationID]} - - - - ${exchangeProperty[X-ONAP-PartnerName]} - - - - - - - - - - ${exchangeProperty[microServicePolicy].getName()} removal - status - - - - POLICY - - - - - - - - - - - - - ${exchangeProperty[operationalPolicy].createPolicyPayload()} - - - - POST - - - application/json - - - ${exchangeProperty[X-ONAP-RequestID]} - - - - ${exchangeProperty[X-ONAP-InvocationID]} - - - - ${exchangeProperty[X-ONAP-PartnerName]} - - - - - - - - - ${exchangeProperty[operationalPolicy].getName()} creation - status - - - - POLICY - - - - - - - - - - - - - null - - - DELETE - - - ${exchangeProperty[X-ONAP-RequestID]} - - - - ${exchangeProperty[X-ONAP-InvocationID]} - - - - ${exchangeProperty[X-ONAP-PartnerName]} - - - - - - - - - ${exchangeProperty[operationalPolicy].getName()} removal - status - - - - POLICY - - - - - - @@ -575,7 +395,7 @@ - ${exchangeProperty[loopObject].getComponent("POLICY").createPoliciesPayloadPdpGroup(exchangeProperty[loopObject])} + ${exchangeProperty[loopObject].getComponent("POLICY").createPoliciesPayloadPdpGroup(exchangeProperty[loopObject],"POST")} @@ -618,67 +438,51 @@ - - - - - ${exchangeProperty[loopObject].getComponent("POLICY").listPolicyNamesPdpGroup(exchangeProperty[loopObject])} - - - ${body} - - - null - - - DELETE - - - ${exchangeProperty[X-ONAP-RequestID]} - - - - ${exchangeProperty[X-ONAP-InvocationID]} - - - - ${exchangeProperty[X-ONAP-PartnerName]} - - - - - - ${exchangeProperty[policyName]} PDP Group removal status - - - - POLICY - - - - - java.lang.Exception - - false - - - PDP Group removal, Error reported: ${exception} - - - POLICY - - - - - - - - + + + + + ${exchangeProperty[loopObject].getComponent("POLICY").createPoliciesPayloadPdpGroup(exchangeProperty[loopObject],"DELETE")} + + + + POST + + + application/json + + + ${exchangeProperty[X-ONAP-RequestID]} + + + + ${exchangeProperty[X-ONAP-InvocationID]} + + + + ${exchangeProperty[X-ONAP-PartnerName]} + + + + + + + + + + PDP Group remove ALL status + + + POLICY + + + + diff --git a/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java b/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java index 432de6065..6449a8966 100644 --- a/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java +++ b/src/test/java/org/onap/clamp/loop/PolicyComponentTest.java @@ -24,7 +24,6 @@ package org.onap.clamp.loop; import static org.assertj.core.api.Assertions.assertThat; - import com.google.gson.Gson; import com.google.gson.JsonObject; import java.io.IOException; @@ -32,6 +31,7 @@ import org.apache.camel.Exchange; import org.apache.camel.Message; import org.junit.Test; import org.mockito.Mockito; +import org.onap.clamp.clds.config.LegacyOperationalPolicyController; import org.onap.clamp.clds.util.ResourceFileUtil; import org.onap.clamp.loop.components.external.ExternalComponentState; import org.onap.clamp.loop.components.external.PolicyComponent; @@ -267,7 +267,8 @@ public class PolicyComponentTest { new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), null, "pdpGroup2", "pdpSubgroup1"); loopTest.addMicroServicePolicy(microServicePolicy2); - PolicyModel policyModel2 = new PolicyModel("onap.policies.controlloop.Operational", null, "1.0.0"); + PolicyModel policyModel2 = new PolicyModel(LegacyOperationalPolicyController.OPERATIONAL_POLICY_LEGACY, null, + "1.0.0"); OperationalPolicy opPolicy = new OperationalPolicy("opPolicy", new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), new Gson().fromJson("{\"jsonschema\":\"schema\"}", JsonObject.class), policyModel2, null, @@ -275,16 +276,22 @@ public class PolicyComponentTest { "pdpSubgroup2"); loopTest.addOperationalPolicy(opPolicy); + OperationalPolicy opLegacyPolicy = + new OperationalPolicy("opLegacyPolicy", new Gson().fromJson( + "{\"guard_policies\":[{\"policy-id\":\"guard1\"}]}", JsonObject.class), + new Gson().fromJson("{\"jsonschema\":\"schema\"}", JsonObject.class), policyModel2, null, + "pdpGroup2", + "pdpSubgroup2"); + + loopTest.addOperationalPolicy(opLegacyPolicy); LoopTemplate loopTemplate = new LoopTemplate("test", "yaml", "svg", 1, null); loopTemplate.setDcaeBlueprintId("UUID-blueprint"); loopTest.setLoopTemplate(loopTemplate); - String payload = PolicyComponent.createPoliciesPayloadPdpGroup(loopTest); + String payload = PolicyComponent.createPoliciesPayloadPdpGroup(loopTest, "POST"); String expectedRes = ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"); assertThat(payload).isEqualTo(expectedRes); - - assertThat(PolicyComponent.listPolicyNamesPdpGroup(loopTest)).containsExactlyInAnyOrder("opPolicy","configPolicyTest","configPolicyTest2"); } } diff --git a/src/test/resources/tosca/pdp-group-policy-payload.json b/src/test/resources/tosca/pdp-group-policy-payload.json index c81440e73..dce740fcd 100644 --- a/src/test/resources/tosca/pdp-group-policy-payload.json +++ b/src/test/resources/tosca/pdp-group-policy-payload.json @@ -22,6 +22,14 @@ "pdpType": "pdpSubgroup2", "action": "POST", "policies": [ + { + "name": "opLegacyPolicy", + "version": "1.0.0" + }, + { + "name": "guard1", + "version": "1.0.0" + }, { "name": "opPolicy", "version": "1.0.0" -- cgit 1.2.3-korg