Add policy deployment/undeployment to policy-participant 55/124055/5
authorwaynedunican <wayne.dunican@est.tech>
Wed, 8 Sep 2021 16:36:49 +0000 (17:36 +0100)
committerWayne Dunican <wayne.dunican@est.tech>
Wed, 15 Sep 2021 07:44:11 +0000 (08:44 +0100)
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 <wayne.dunican@est.tech>
common/src/main/resources/tosca/PolicyControlLoopElementType.yaml
packages/policy-clamp-tarball/src/main/resources/etc/PolicyParticipantParameters.yaml
participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyPapHttpClient.java [new file with mode: 0644]
participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java
participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameters.java
participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml
participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java

index bf66718..b4631ba 100644 (file)
@@ -37,3 +37,7 @@ node_types:
         default: defaultGroup
         description: The PDP group to which the policy should be deployed. This parameter is used when the policy is
                      deployed to PAP. The value defaults to the "defaultGroup", which always exists.
+      pdpType:
+        type: string
+        required: true
+        description: The PDP type to which the policy will run on. This parameter is used when the policy is deployed to PAP.
\ No newline at end of file
index 1ea5d68..66e2d95 100644 (file)
@@ -1,5 +1,7 @@
 
 participant:
+  pdpGroup: defaultGroup
+  pdpType: apex
   policyApiParameters:
     clientName: api
     hostname: policy-api
@@ -8,6 +10,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/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 (file)
index 0000000..3cce3bd
--- /dev/null
@@ -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));
+    }
+}
index 8805141..29a7852 100644 (file)
@@ -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<String, String> 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<String, String> 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<String, String> 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<String, String> 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
index ced9d1b..8e1de36 100644 (file)
@@ -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;
 }
index 32cd28c..e5b2b2a 100644 (file)
@@ -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
index a34c9cd..41af8a6 100644 (file)
@@ -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<String, Object> getPolicyPapParametersMap() {
+        final Map<String, Object> 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;
     }