From f1fa71ddf3c324305ab068a9fd5e7075b7e2caf7 Mon Sep 17 00:00:00 2001 From: waynedunican Date: Wed, 8 Sep 2021 17:36:49 +0100 Subject: Add policy deployment/undeployment to policy-participant Add policy deployment and undeployment to the policy-participant Allow user to specify pdpGroup and pdpdType as part of the deployment or undeployment of policies Change-Id: I8b37ee96305d35db0784cde3e4564150feaf477d Issue-ID: POLICY-3584 Signed-off-by: Wayne Dunican --- .../policy/client/PolicyPapHttpClient.java | 87 ++++++++++++++++++++++ .../main/handler/ControlLoopElementHandler.java | 62 ++++++++++++--- .../parameters/ParticipantPolicyParameters.java | 11 +++ .../src/main/resources/config/application.yaml | 10 +++ .../policy/main/parameters/CommonTestData.java | 20 +++++ 5 files changed, 179 insertions(+), 11 deletions(-) create mode 100644 participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyPapHttpClient.java (limited to 'participant/participant-impl/participant-impl-policy') diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyPapHttpClient.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyPapHttpClient.java new file mode 100644 index 000000000..3cce3bd8a --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyPapHttpClient.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.participant.policy.client; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; +import org.springframework.stereotype.Component; + +@Component +public class PolicyPapHttpClient extends AbstractHttpClient { + + private static final String PAP_URI = "/policy/pap/v1/"; + private final String pdpGroup; + private final String pdpType; + + /** + * Constructor. + * + * @param parameters the policy participant parameters + */ + public PolicyPapHttpClient(ParticipantPolicyParameters parameters) { + super(parameters.getPolicyPapParameters()); + this.pdpGroup = parameters.getPdpGroup(); + this.pdpType = parameters.getPdpType(); + } + + /** + * Deploy or undeploy Policies. + * + * @param policyName the name of the policy to be deployed/undeployed + * @param policyVersion the version of the policy to be deployed/undeployed + * @param action the action to deploy/undeploy policy + * @return Response + */ + public Response handlePolicyDeployOrUndeploy(final String policyName, final String policyVersion, + final String action) { + // policies + JsonObject policyArrayBody = new JsonObject(); + policyArrayBody.addProperty("name", policyName); + policyArrayBody.addProperty("version", policyVersion); + JsonArray policyArr = new JsonArray(); + policyArr.add(policyArrayBody); + + // deploymentSubgroups + JsonObject deploymentSubGrpBody = new JsonObject(); + deploymentSubGrpBody.addProperty("pdpType", pdpType); + deploymentSubGrpBody.addProperty("action", action); + deploymentSubGrpBody.add("policies", policyArr); + JsonArray deployArr = new JsonArray(); + deployArr.add(deploymentSubGrpBody); + + // groups + JsonObject groupArrayBody = new JsonObject(); + groupArrayBody.addProperty("name", pdpGroup); + groupArrayBody.add("deploymentSubgroups", deployArr); + JsonArray groupArr = new JsonArray(); + groupArr.add(groupArrayBody); + + // main json + JsonObject mainJson = new JsonObject(); + mainJson.add("groups", groupArr); + + return executePost(PAP_URI + "pdps/deployments/batch", Entity.entity(mainJson, MediaType.APPLICATION_JSON)); + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java index 880514110..29a7852f1 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java @@ -36,6 +36,7 @@ import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.Parti import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; import org.onap.policy.clamp.controlloop.participant.policy.client.PolicyApiHttpClient; +import org.onap.policy.clamp.controlloop.participant.policy.client.PolicyPapHttpClient; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; @@ -58,6 +59,7 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { private final Map policyMap = new LinkedHashMap<>(); private final PolicyApiHttpClient apiHttpClient; + private final PolicyPapHttpClient papHttpClient; @Setter private ParticipantIntermediaryApi intermediaryApi; @@ -66,14 +68,17 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { * constructor. * * @param apiHttpClient the Policy Api Http Client + * @param papHttpClient the Policy Pap Http Client */ - public ControlLoopElementHandler(PolicyApiHttpClient apiHttpClient) { + public ControlLoopElementHandler(PolicyApiHttpClient apiHttpClient, PolicyPapHttpClient papHttpClient) { + this.papHttpClient = papHttpClient; this.apiHttpClient = apiHttpClient; } /** * Callback method to handle a control loop element state change. * + * @param controlLoopId the ID of the control loop * @param controlLoopElementId the ID of the control loop element * @param currentState the current state of the control loop element * @param orderedState the state to which the control loop element is changing to @@ -95,14 +100,21 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { } break; case PASSIVE: + try { + undeployPolicies(controlLoopElementId, orderedState); + } catch (PfModelRuntimeException e) { + LOGGER.debug("Undeploying policies failed - no policies to undeploy {}", controlLoopElementId); + } intermediaryApi.updateControlLoopElementState(controlLoopId, - controlLoopElementId, orderedState, ControlLoopState.PASSIVE, - ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); + controlLoopElementId, orderedState, ControlLoopState.PASSIVE, + ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); break; case RUNNING: - intermediaryApi.updateControlLoopElementState(controlLoopId, - controlLoopElementId, orderedState, ControlLoopState.RUNNING, - ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); + try { + deployPolicies(controlLoopId, controlLoopElementId, orderedState); + } catch (PfModelRuntimeException e) { + LOGGER.debug("Deploying policies failed {}", controlLoopElementId); + } break; default: LOGGER.debug("Unknown orderedstate {}", orderedState); @@ -111,7 +123,7 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { } private void deletePolicyData(ToscaConceptIdentifier controlLoopId, - UUID controlLoopElementId, ControlLoopOrderedState newState) { + UUID controlLoopElementId, ControlLoopOrderedState newState) { // Delete all policies of this controlLoop from policy framework for (Entry policy : policyMap.entrySet()) { apiHttpClient.deletePolicy(policy.getKey(), policy.getValue()); @@ -123,8 +135,36 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { } policyTypeMap.clear(); intermediaryApi.updateControlLoopElementState(controlLoopId, - controlLoopElementId, newState, ControlLoopState.UNINITIALISED, - ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); + controlLoopElementId, newState, ControlLoopState.UNINITIALISED, + ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); + } + + private void deployPolicies(ToscaConceptIdentifier controlLoopId, UUID controlLoopElementId, + ControlLoopOrderedState newState) { + // Deploy all policies of this controlLoop from Policy Framework + if (policyMap.entrySet() != null) { + for (Entry policy : policyMap.entrySet()) { + papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(), "POST"); + } + LOGGER.debug("Policies deployed to {} successfully", controlLoopElementId); + } else { + LOGGER.debug("No policies to deploy to {}", controlLoopElementId); + } + intermediaryApi.updateControlLoopElementState(controlLoopId, + controlLoopElementId, newState, ControlLoopState.RUNNING, + ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); + } + + private void undeployPolicies(UUID controlLoopElementId, ControlLoopOrderedState newState) { + // Undeploy all policies of this controlloop from Policy Framework + if (policyMap.entrySet() != null) { + for (Entry policy : policyMap.entrySet()) { + papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(), "DELETE"); + } + LOGGER.debug("Undeployed policies from {} successfully", controlLoopElementId); + } else { + LOGGER.debug("No policies are deployed to {}", controlLoopElementId); + } } /** @@ -136,7 +176,7 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { */ @Override public void controlLoopElementUpdate(ToscaConceptIdentifier controlLoopId, ControlLoopElement element, - ToscaNodeTemplate clElementDefinition) + ToscaNodeTemplate clElementDefinition) throws PfModelException { intermediaryApi.updateControlLoopElementState(controlLoopId, element.getId(), element.getOrderedState(), ControlLoopState.PASSIVE, ParticipantMessageType.CONTROL_LOOP_UPDATE); @@ -179,4 +219,4 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { intermediaryApi.updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics); } } -} +} \ No newline at end of file diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameters.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameters.java index ced9d1bd0..8e1de36b9 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameters.java +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameters.java @@ -21,6 +21,7 @@ package org.onap.policy.clamp.controlloop.participant.policy.main.parameters; import javax.validation.Valid; +import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; @@ -48,4 +49,14 @@ public class ParticipantPolicyParameters implements ParticipantParameters { @NotNull @ParameterGroupConstraint private RestClientParameters policyApiParameters; + + @NotNull + @ParameterGroupConstraint + private RestClientParameters policyPapParameters; + + @NotBlank + private String pdpGroup; + + @NotBlank + private String pdpType; } diff --git a/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml b/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml index 32cd28cd3..e5b2b2ab1 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml +++ b/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml @@ -2,6 +2,8 @@ server: port: 8082 participant: + pdpGroup: defaultGroup + pdpType: apex policyApiParameters: clientName: api hostname: policy-api @@ -10,6 +12,14 @@ participant: password: zb!XztG34 https: true allowSelfSignedCerts: true + policyPapParameters: + clientName: pap + hostname: policy-pap + port: 6969 + userName: healthcheck + password: zb!XztG34 + https: true + allowSelfSignedCerts: true intermediaryParameters: reportingTimeIntervalMs: 120000 description: Participant Description diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java index a34c9cda5..41af8a6ec 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java @@ -68,6 +68,26 @@ public class CommonTestData { map.put("name", name); map.put("intermediaryParameters", getIntermediaryParametersMap(false)); map.put("policyApiParameters", getPolicyApiParametersMap()); + map.put("policyPapParameters", getPolicyPapParametersMap()); + map.put("pdpGroup", "defaultGroup"); + map.put("pdpType", "apex"); + return map; + } + + /** + * Returns a property map for a policyPapParameters map for test cases. + * + * @return a property map suitable for constructing an object + */ + public Map getPolicyPapParametersMap() { + final Map map = new TreeMap<>(); + map.put("clientName", "pap"); + map.put("hostname", "localhost"); + map.put("port", 6968); + map.put("userName", "healthcheck"); + map.put("password", "zb!XztG34"); + map.put("https", false); + map.put("allowSelfSignedCerts", true); return map; } -- cgit 1.2.3-korg