From 7f685aa342813c611b6e43d7b4a869c7454baddc Mon Sep 17 00:00:00 2001 From: sebdet Date: Thu, 25 Mar 2021 18:19:29 +0100 Subject: Rework the backend to support PDP updates Rework Pdp payloads builder so that we can integrate different actions in the same batch + Add a new endpoint to support pdp payload&query to PEF from UI Issue-ID: POLICY-2930 Issue-ID: POLICY-2931 Signed-off-by: sebdet Change-Id: I3c933272419770595b706f6950f821220a76f778 --- .../loop/components/external/PolicyComponent.java | 22 +-- .../clamp/policy/pdpgroup/PdpGroupPayload.java | 149 ++++++++++++--------- .../policy/pdpgroup/PdpGroupPayloadException.java | 54 ++++++++ 3 files changed, 150 insertions(+), 75 deletions(-) create mode 100644 src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayloadException.java (limited to 'src/main/java/org/onap') diff --git a/src/main/java/org/onap/policy/clamp/loop/components/external/PolicyComponent.java b/src/main/java/org/onap/policy/clamp/loop/components/external/PolicyComponent.java index ff4e2643c..27e8e1a13 100644 --- a/src/main/java/org/onap/policy/clamp/loop/components/external/PolicyComponent.java +++ b/src/main/java/org/onap/policy/clamp/loop/components/external/PolicyComponent.java @@ -27,10 +27,7 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import javax.persistence.Transient; import org.apache.camel.Exchange; -import org.onap.policy.clamp.clds.util.JsonUtils; import org.onap.policy.clamp.loop.Loop; -import org.onap.policy.clamp.policy.microservice.MicroServicePolicy; -import org.onap.policy.clamp.policy.operational.OperationalPolicy; import org.onap.policy.clamp.policy.pdpgroup.PdpGroupPayload; /** @@ -81,19 +78,14 @@ public class PolicyComponent extends ExternalComponent { */ public static String createPoliciesPayloadPdpGroup(Loop loop, String action) { PdpGroupPayload pdpGroupPayload = new PdpGroupPayload(); - for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) { - pdpGroupPayload - .updatePdpGroupMap(opPolicy.getPdpGroup(), opPolicy.getPdpSubgroup(), opPolicy.getName(), "1.0.0"); - } - - for (MicroServicePolicy msPolicy : loop.getMicroServicePolicies()) { - pdpGroupPayload - .updatePdpGroupMap(msPolicy.getPdpGroup(), msPolicy.getPdpSubgroup(), msPolicy.getName(), "1.0.0"); - } + loop.getOperationalPolicies().stream().forEach(opPolicy -> pdpGroupPayload + .updatePdpGroupMap(opPolicy.getPdpGroup(), opPolicy.getPdpSubgroup(), opPolicy.getName(), "1.0.0", + action)); - String payload = JsonUtils.GSON.toJson(pdpGroupPayload.generateActivatePdpGroupPayload(action)); - logger.info("PdpGroup policy payload: " + payload); - return payload; + loop.getMicroServicePolicies().stream().forEach(msPolicy -> pdpGroupPayload + .updatePdpGroupMap(msPolicy.getPdpGroup(), msPolicy.getPdpSubgroup(), msPolicy.getName(), "1.0.0", + action)); + return pdpGroupPayload.generatePdpGroupPayload(); } private static ExternalComponentState findNewState(boolean found, boolean deployed) { diff --git a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java index c89395044..a10f6df7d 100644 --- a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java +++ b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java @@ -23,86 +23,115 @@ package org.onap.policy.clamp.policy.pdpgroup; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.gson.JsonElement; +import java.util.ArrayList; +import java.util.Arrays; +import org.onap.policy.clamp.clds.util.JsonUtils; +import org.onap.policy.models.pdp.concepts.DeploymentGroup; +import org.onap.policy.models.pdp.concepts.DeploymentGroups; +import org.onap.policy.models.pdp.concepts.DeploymentSubGroup; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; /** * This is an utility class that build the PDP group policy payload. * This is used when policies have to be deployed to PDP group/subgroups on the Policy Engine. + * Currently it does not group the queries per pdpgroup/subgroups/action. + * This is currently NOT thread safe, do not use parallel streams to update the structure. */ public class PdpGroupPayload { - private Map>> pdpGroupMap = new HashMap<>(); + private static final EELFLogger logger = EELFManager.getInstance().getLogger(PdpGroupPayload.class); /** - * This method updates the pdpGroupMap structure for a specific policy/version/pdpdGroup/PdpSubGroup. + * The default node that will contain the actions array. + */ + public static final String PDP_ACTIONS = "PdpActions"; + + private final DeploymentGroups deploymentGroups = new DeploymentGroups(); + + /** + * Default constructor. + */ + public PdpGroupPayload() { + deploymentGroups.setGroups(new ArrayList<>()); + } + + /** + * Constructor that takes a list of actions in input. * - * @param pdpGroup The pdp Group in String - * @param pdpSubGroup The pdp Sub Group in String - * @param policyName The policy name - * @param policyVersion The policy Version + * @param listOfPdpActions The list of actions that needs to be done. + * e.g: {"Pdpactions":["DELETE/PdpGroup1/PdpSubGroup1/PolicyName1/1.0.0",....]} + * @throws PdpGroupPayloadException in case of issues to read the listOfActions */ - public void updatePdpGroupMap(String pdpGroup, - String pdpSubGroup, - String policyName, - String policyVersion) { - JsonObject policyJson = new JsonObject(); - policyJson.addProperty("name", policyName); - policyJson.addProperty("version", policyVersion); - Map> 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<>(); + public PdpGroupPayload(final JsonElement listOfPdpActions) throws PdpGroupPayloadException { + this(); + this.readListOfActions(listOfPdpActions); + } + + /** + * This method converts the list of actions directly to the pdp payload query as String. + * + * @param listOfPdpActions The list of actions that needs to be done. + * e.g: {"Pdpactions":["DELETE/PdpGroup1/PdpSubGroup1/PolicyName1/1.0.0",....]} + * @return The string containing the PDP payload that can be sent directly + * @throws PdpGroupPayloadException in case of issues to read the listOfActions + */ + public static String generatePdpGroupPayloadFromList(final JsonElement listOfPdpActions) + throws PdpGroupPayloadException { + return new PdpGroupPayload(listOfPdpActions).generatePdpGroupPayload(); + } + + + private void readListOfActions(final JsonElement listOfPdpActions) throws PdpGroupPayloadException { + for (JsonElement action : listOfPdpActions.getAsJsonObject().getAsJsonArray(PDP_ACTIONS)) { + String[] opParams = action.getAsString().split("/"); + if (opParams.length == 5) { + this.updatePdpGroupMap(opParams[1], opParams[2], opParams[3], opParams[4], opParams[0]); } else { - policyList = pdpSubGroupMap.get(pdpSubGroup); + logger.error("One PDP push command does not contain the right number of arguments: " + action); + throw new PdpGroupPayloadException( + "One PDP push command does not contain the right number of arguments: " + action); } } - policyList.add(policyJson); - pdpSubGroupMap.put(pdpSubGroup, policyList); - pdpGroupMap.put(pdpGroup, pdpSubGroupMap); + } + + /** + * This method updates the pdpGroupMap structure for a specific policy/version/pdpdGroup/PdpSubGroup. + * + * @param pdpGroup The pdp Group in String + * @param pdpSubGroup The pdp Sub Group in String + * @param policyName The policy name + * @param policyVersion The policy Version + * @param action DELETE or POST + */ + public void updatePdpGroupMap(String pdpGroup, + String pdpSubGroup, + String policyName, + String policyVersion, String action) { + // create subgroup + DeploymentSubGroup newSubGroup = new DeploymentSubGroup(); + newSubGroup.setPdpType(pdpSubGroup); + newSubGroup.setAction(DeploymentSubGroup.Action.valueOf(action)); + newSubGroup.setPolicies(Arrays.asList(new ToscaConceptIdentifier(policyName, policyVersion))); + // Then the group + DeploymentGroup newGroup = new DeploymentGroup(); + newGroup.setName(pdpGroup); + newGroup.setDeploymentSubgroups(Arrays.asList(newSubGroup)); + // Add to deployment Groups structure + this.deploymentGroups.getGroups().add(newGroup); } /** * This method generates the Payload in Json from the pdp Group structure containing the policies/versions * that must be sent to the policy framework. * - * @param action The action to do, either a POST or a DELETE - * @return The Json that can be sent to policy framework as JsonObject + * @return The Json that can be sent to policy framework as String */ - public JsonObject generateActivatePdpGroupPayload(String action) { - JsonArray payloadArray = new JsonArray(); - for (Map.Entry>> pdpGroupInfo : pdpGroupMap.entrySet()) { - JsonObject pdpGroupNode = new JsonObject(); - JsonArray subPdpArray = new JsonArray(); - pdpGroupNode.addProperty("name", pdpGroupInfo.getKey()); - pdpGroupNode.add("deploymentSubgroups", subPdpArray); - - for (Map.Entry> pdpSubGroupInfo : pdpGroupInfo.getValue().entrySet()) { - JsonObject pdpSubGroupNode = new JsonObject(); - subPdpArray.add(pdpSubGroupNode); - pdpSubGroupNode.addProperty("pdpType", pdpSubGroupInfo.getKey()); - pdpSubGroupNode.addProperty("action", action); - - 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; + public String generatePdpGroupPayload() { + String payload = JsonUtils.GSON.toJson(this.deploymentGroups); + logger.info("PdpGroup policy payload: " + payload); + return payload; } } diff --git a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayloadException.java b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayloadException.java new file mode 100644 index 000000000..4ce0721b2 --- /dev/null +++ b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayloadException.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP POLICY-CLAMP + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.policy.clamp.policy.pdpgroup; + +/** + * Exception during Pdp Group payload construction. + */ +public class PdpGroupPayloadException extends Exception { + + /** + * serialization id. + */ + private static final long serialVersionUID = -5676848693241134101L; + + /** + * This constructor can be used to create a new PdpGroupPayloadException. + * + * @param message The message to dump + */ + public PdpGroupPayloadException(final String message) { + super(message); + } + + /** + * This constructor can be used to create a new PdpGroupPayloadException. + * + * @param message The message to dump + * @param cause The Throwable cause object + */ + public PdpGroupPayloadException(final String message, final Throwable cause) { + super(message, cause); + } +} -- cgit 1.2.3-korg