Current draft of undeploy 00/84300/2
authorJim Hahn <jrh3@att.com>
Wed, 10 Apr 2019 13:26:59 +0000 (09:26 -0400)
committerJim Hahn <jrh3@att.com>
Wed, 10 Apr 2019 13:31:53 +0000 (09:31 -0400)
Change-Id: If3f01a6e92bef9eb889066115537f599e6b09229
Issue-ID: POLICY-1541
Signed-off-by: Jim Hahn <jrh3@att.com>
main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java
main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteProvider.java [deleted file]
main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteControllerV1.java [moved from main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteControllerV1.java with 91% similarity]
main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteProvider.java [new file with mode: 0644]
main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteControllerV1.java [moved from main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteControllerV1.java with 85% similarity]

index 05ba290..01c3361 100644 (file)
@@ -30,6 +30,7 @@ import org.onap.policy.common.endpoints.http.server.HttpServletServer;
 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
 import org.onap.policy.common.gson.GsonMessageBodyHandler;
 import org.onap.policy.pap.main.parameters.RestServerParameters;
+import org.onap.policy.pap.main.rest.depundep.PdpGroupDeleteControllerV1;
 import org.onap.policy.pap.main.rest.depundep.PdpGroupDeployControllerV1;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteProvider.java
deleted file mode 100644 (file)
index 2a1d240..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * ONAP PAP
- * ================================================================================
- * Copyright (C) 2019 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.pap.main.rest;
-
-import javax.ws.rs.core.Response;
-import org.apache.commons.lang3.tuple.Pair;
-import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
-
-/**
- * Provider for PAP component to delete PDP groups.
- */
-public class PdpGroupDeleteProvider {
-
-    /**
-     * Deletes a PDP group.
-     *
-     * @param groupName name of the PDP group to be deleted
-     * @param version group version to delete; may be {@code null} if the group has only
-     *        one version
-     * @return a pair containing the status and the response
-     */
-    public Pair<Response.Status, PdpGroupDeleteResponse> deleteGroup(String groupName, String version) {
-
-        /*
-         * TODO Lock for updates - return error if already locked.
-         */
-
-        /*
-         * TODO Make updates - sending initial messages to PDPs and arranging for
-         * listeners to complete the deletion actions (in the background). The final step
-         * for the listener is to unlock.
-         */
-
-        /*
-         * TODO Return error if unable to send updates to all PDPs.
-         */
-
-        return Pair.of(Response.Status.OK, new PdpGroupDeleteResponse());
-    }
-
-    /**
-     * Deletes a PDP policy.
-     *
-     * @param policyId id of the policy to be deleted
-     * @param version policy version to delete; may be {@code null} if the policy has only
-     *        one version
-     * @return a pair containing the status and the response
-     */
-    public Pair<Response.Status, PdpGroupDeleteResponse> deletePolicy(String policyId, String version) {
-
-        /*
-         * TODO Lock for updates - return error if already locked.
-         */
-
-        /*
-         * TODO Make updates - sending initial messages to PDPs and arranging for
-         * listeners to complete the deletion actions (in the background). The final step
-         * for the listener is to unlock.
-         */
-
-        /*
-         * TODO Return error if unable to send updates to all PDPs.
-         */
-
-        return Pair.of(Response.Status.OK, new PdpGroupDeleteResponse());
-    }
-}
@@ -18,7 +18,7 @@
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.policy.pap.main.rest;
+package org.onap.policy.pap.main.rest.depundep;
 
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
@@ -37,6 +37,8 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 import org.apache.commons.lang3.tuple.Pair;
 import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
+import org.onap.policy.pap.main.rest.PapRestControllerV1;
 
 /**
  * Class to provide REST end points for PAP component to delete a PDP group.
@@ -131,7 +133,7 @@ public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
     }
 
     /**
-     * Deletes a PDP policy.
+     * Undeploys the latest version of a policy from the PDPs.
      *
      * @param requestId request ID used in ONAP logging
      * @param policyName name of the PDP Policy to be deleted
@@ -140,8 +142,8 @@ public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
     // @formatter:off
     @DELETE
     @Path("pdps/policies/{name}")
-    @ApiOperation(value = "Delete PDP Policy",
-        notes = "Deletes a PDP Policy, returning optional error details",
+    @ApiOperation(value = "Undeploy a PDP Policy from PDPs",
+        notes = "Undeploys the latest version of a policy from the PDPs, returning optional error details",
         response = PdpGroupDeleteResponse.class,
         tags = {"Policy Administration (PAP) API"},
         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
@@ -165,14 +167,15 @@ public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
     public Response deletePolicy(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
                     @ApiParam(value = "PDP Policy Name", required = true) @PathParam("name") String policyName) {
 
-        Pair<Status, PdpGroupDeleteResponse> pair = provider.deletePolicy(policyName, null);
+        Pair<Status, PdpGroupDeleteResponse> pair =
+                        provider.undeploy(new ToscaPolicyIdentifierOptVersion(policyName, null));
 
         return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
                         .entity(pair.getRight()).build();
     }
 
     /**
-     * Deletes a specific version of a PDP policy.
+     * Undeploys a specific version of a policy from the PDPs.
      *
      * @param requestId request ID used in ONAP logging
      * @param policyName name of the PDP Policy to be deleted
@@ -182,8 +185,8 @@ public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
     // @formatter:off
     @DELETE
     @Path("pdps/policies/{name}/versions/{version}")
-    @ApiOperation(value = "Delete version of a PDP Policy",
-        notes = "Deletes a version of a PDP Policy, returning optional error details",
+    @ApiOperation(value = "Undeploy version of a PDP Policy from PDPs",
+        notes = "Undeploys a specific version of a policy from the PDPs, returning optional error details",
         response = PdpGroupDeleteResponse.class,
         tags = {"Policy Administration (PAP) API"},
         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
@@ -209,7 +212,8 @@ public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
                     @ApiParam(value = "PDP Policy Name", required = true) @PathParam("name") String policyName,
                     @ApiParam(value = "PDP Policy Version", required = true) @PathParam("version") String version) {
 
-        Pair<Status, PdpGroupDeleteResponse> pair = provider.deletePolicy(policyName, version);
+        Pair<Status, PdpGroupDeleteResponse> pair =
+                        provider.undeploy(new ToscaPolicyIdentifierOptVersion(policyName, version));
 
         return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
                         .entity(pair.getRight()).build();
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteProvider.java
new file mode 100644 (file)
index 0000000..d5acc62
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP PAP
+ * ================================================================================
+ * Copyright (C) 2019 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.pap.main.rest.depundep;
+
+import java.util.function.BiFunction;
+import javax.ws.rs.core.Response;
+import org.apache.commons.lang3.tuple.Pair;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
+import org.onap.policy.models.pdp.concepts.PdpGroup;
+import org.onap.policy.models.pdp.concepts.PdpSubGroup;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
+import org.onap.policy.pap.main.PolicyPapRuntimeException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Provider for PAP component to delete PDP groups.
+ */
+public class PdpGroupDeleteProvider extends ProviderBase<PdpGroupDeleteResponse> {
+    private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeleteProvider.class);
+
+
+    /**
+     * Constructs the object.
+     */
+    public PdpGroupDeleteProvider() {
+        super();
+    }
+
+    /**
+     * Deletes a PDP group.
+     *
+     * @param groupName name of the PDP group to be deleted
+     * @param version group version to delete; may be {@code null} if the group has only
+     *        one version
+     * @return a pair containing the status and the response
+     */
+    public Pair<Response.Status, PdpGroupDeleteResponse> deleteGroup(String groupName, String version) {
+
+        return Pair.of(Response.Status.OK, new PdpGroupDeleteResponse());
+    }
+
+    /**
+     * Deletes a PDP policy.
+     *
+     * @param policyIdent identifier of the policy to be undeployed
+     * @return a pair containing the status and the response
+     */
+    public Pair<Response.Status, PdpGroupDeleteResponse> undeploy(ToscaPolicyIdentifierOptVersion policyIdent) {
+
+        return process(policyIdent, this::deletePolicy);
+    }
+
+    /**
+     * Deletes a policy from its groups.
+     *
+     * @param data session data
+     * @param ident identifier of the policy to be deleted
+     */
+    private void deletePolicy(SessionData data, ToscaPolicyIdentifierOptVersion ident) {
+        try {
+            processPolicy(data, ident);
+
+        } catch (PfModelException e) {
+            // no need to log the error here, as it will be logged by the invoker
+            logger.warn("failed to deploy policy: {}", ident);
+            throw new PolicyPapRuntimeException(DB_ERROR_MSG, e);
+
+        } catch (RuntimeException e) {
+            // no need to log the error here, as it will be logged by the invoker
+            logger.warn("failed to deploy policy: {}", ident);
+            throw e;
+        }
+    }
+
+    @Override
+    public PdpGroupDeleteResponse makeResponse(String errorMsg) {
+        PdpGroupDeleteResponse resp = new PdpGroupDeleteResponse();
+        resp.setErrorDetails(errorMsg);
+        return resp;
+    }
+
+    @Override
+    protected BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy) {
+        ToscaPolicyIdentifier desiredIdent = policy.getIdentifier();
+
+        return (group, subgroup) -> {
+
+            if (!subgroup.getPolicies().contains(desiredIdent)) {
+                // doesn't have the policy
+                return false;
+            }
+
+            // remove the policy from the subgroup
+            subgroup.getPolicies().remove(desiredIdent);
+            return true;
+        };
+    }
+}
@@ -19,7 +19,7 @@
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.policy.pap.main.rest;
+package org.onap.policy.pap.main.rest.depundep;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
@@ -28,6 +28,7 @@ import javax.ws.rs.client.Invocation;
 import javax.ws.rs.core.Response;
 import org.junit.Test;
 import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
+import org.onap.policy.pap.main.rest.CommonPapRestServer;
 
 public class TestPdpGroupDeleteControllerV1 extends CommonPapRestServer {
 
@@ -88,13 +89,13 @@ public class TestPdpGroupDeleteControllerV1 extends CommonPapRestServer {
         Invocation.Builder invocationBuilder = sendRequest(uri);
         Response rawresp = invocationBuilder.delete();
         PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
-        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
-        assertNull(resp.getErrorDetails());
+        assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
+        assertEquals("cannot find policy: my-name", resp.getErrorDetails());
 
         rawresp = invocationBuilder.delete();
         resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
-        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
-        assertNull(resp.getErrorDetails());
+        assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
+        assertEquals("cannot find policy: my-name", resp.getErrorDetails());
 
         // verify it fails when no authorization info is included
         checkUnauthRequest(uri, req -> req.delete());
@@ -107,13 +108,13 @@ public class TestPdpGroupDeleteControllerV1 extends CommonPapRestServer {
         Invocation.Builder invocationBuilder = sendRequest(uri);
         Response rawresp = invocationBuilder.delete();
         PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
-        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
-        assertNull(resp.getErrorDetails());
+        assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
+        assertEquals("cannot find policy: my-name 3", resp.getErrorDetails());
 
         rawresp = invocationBuilder.delete();
         resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
-        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
-        assertNull(resp.getErrorDetails());
+        assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus());
+        assertEquals("cannot find policy: my-name 3", resp.getErrorDetails());
 
         // verify it fails when no authorization info is included
         checkUnauthRequest(uri, req -> req.delete());