Add simple deploy/undeploy REST API 06/82206/1
authorJim Hahn <jrh3@att.com>
Wed, 13 Mar 2019 21:58:17 +0000 (17:58 -0400)
committerJim Hahn <jrh3@att.com>
Thu, 14 Mar 2019 01:51:14 +0000 (21:51 -0400)
Change-Id: I4b789bfe4f92dae930f3675c6648b90f3c9fc9e4
Issue-ID: POLICY-1542
Signed-off-by: Jim Hahn <jrh3@att.com>
main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteControllerV1.java
main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeleteProvider.java
main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployControllerV1.java
main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java
main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteControllerV1.java
main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java

index 4698218..1671611 100644 (file)
@@ -80,7 +80,7 @@ public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
     public Response deleteGroup(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
                     @ApiParam(value = "PDP Group Name", required = true) @PathParam("name") String groupName) {
 
-        Pair<Status, PdpGroupDeleteResponse> pair = provider.delete(groupName, null);
+        Pair<Status, PdpGroupDeleteResponse> pair = provider.deleteGroup(groupName, null);
 
         return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
                         .entity(pair.getRight()).build();
@@ -124,7 +124,92 @@ public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
                     @ApiParam(value = "PDP Group Name", required = true) @PathParam("name") String groupName,
                     @ApiParam(value = "PDP Group Version", required = true) @PathParam("version") String version) {
 
-        Pair<Status, PdpGroupDeleteResponse> pair = provider.delete(groupName, version);
+        Pair<Status, PdpGroupDeleteResponse> pair = provider.deleteGroup(groupName, version);
+
+        return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
+                        .entity(pair.getRight()).build();
+    }
+
+    /**
+     * Deletes a PDP policy.
+     *
+     * @param requestId request ID used in ONAP logging
+     * @param policyName name of the PDP Policy to be deleted
+     * @return a response
+     */
+    // @formatter:off
+    @DELETE
+    @Path("pdps/policies/{name}")
+    @ApiOperation(value = "Delete PDP Policy",
+        notes = "Deletes a PDP Policy, returning optional error details",
+        response = PdpGroupDeleteResponse.class,
+        tags = {"Policy Administration (PAP) API"},
+        authorizations = @Authorization(value = AUTHORIZATION_TYPE),
+        responseHeaders = {
+            @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
+                            response = String.class),
+            @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
+                            response = String.class),
+            @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
+                            response = String.class),
+            @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
+                            response = UUID.class)},
+        extensions = {@Extension(name = EXTENSION_NAME,
+            properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
+                @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
+    @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
+                    @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
+                    @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
+    // @formatter:on
+
+    public Response deletePolicies(@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);
+
+        return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
+                        .entity(pair.getRight()).build();
+    }
+
+    /**
+     * Deletes a PDP policy.
+     *
+     * @param requestId request ID used in ONAP logging
+     * @param policyName name of the PDP Policy to be deleted
+     * @param version version to be deleted
+     * @return a response
+     */
+    // @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",
+        response = PdpGroupDeleteResponse.class,
+        tags = {"Policy Administration (PAP) API"},
+        authorizations = @Authorization(value = AUTHORIZATION_TYPE),
+        responseHeaders = {
+            @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
+                            response = String.class),
+            @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
+                            response = String.class),
+            @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
+                            response = String.class),
+            @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
+                            response = UUID.class)},
+        extensions = {@Extension(name = EXTENSION_NAME,
+            properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
+                @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
+    @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
+                    @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
+                    @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
+    // @formatter:on
+
+    public Response deletePoliciesVersion(
+                    @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
+                    @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);
 
         return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
                         .entity(pair.getRight()).build();
index e40b053..f9619df 100644 (file)
@@ -37,7 +37,34 @@ public class PdpGroupDeleteProvider {
      *        one version
      * @return a pair containing the status and the response
      */
-    public Pair<Response.Status, PdpGroupDeleteResponse> delete(String groupName, String version) {
+    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 group.
+     *
+     * @param policyId id of the policy 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> deletePolicy(String policyId, String version) {
 
         /*
          * TODO Lock for updates - return error if already locked.
index 228d97c..a0a2ee8 100644 (file)
@@ -37,6 +37,7 @@ import javax.ws.rs.core.Response.Status;
 import org.apache.commons.lang3.tuple.Pair;
 import org.onap.policy.models.pap.concepts.PdpGroup;
 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
+import org.onap.policy.models.pap.concepts.PdpPolicies;
 
 /**
  * Class to provide REST end points for PAP component to deploy a PDP group.
@@ -77,10 +78,52 @@ public class PdpGroupDeployControllerV1 extends PapRestControllerV1 {
                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
     // @formatter:on
 
-    public Response deploy(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
+    public Response deployGroup(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
                     @ApiParam(value = "PDP Group Configuration", required = true) PdpGroup group) {
 
-        Pair<Status, PdpGroupDeployResponse> pair = provider.deploy(group);
+        Pair<Status, PdpGroupDeployResponse> pair = provider.deployGroup(group);
+
+        return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
+                        .entity(pair.getRight()).build();
+    }
+
+    /**
+     * Deploys or updates PDP policies.
+     *
+     * @param requestId request ID used in ONAP logging
+     * @param policies PDP policies
+     * @return a response
+     */
+    // @formatter:off
+    @POST
+    @Path("pdps/policies")
+    @ApiOperation(value = "Deploy or update PDP Policies",
+        notes = "Deploys or updates PDP Policies, returning optional error details",
+        response = PdpGroupDeployResponse.class,
+        tags = {"Policy Administration (PAP) API"},
+        authorizations = @Authorization(value = AUTHORIZATION_TYPE),
+        responseHeaders = {
+            @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
+                            response = String.class),
+            @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
+                            response = String.class),
+            @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
+                            response = String.class),
+            @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
+                            response = UUID.class)},
+        extensions = {@Extension(name = EXTENSION_NAME,
+            properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
+                @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
+    @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
+                    @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
+                    @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
+    // @formatter:on
+
+    public Response deployPolicies(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
+                    @ApiParam(value = "PDP Policies; only the name and policyVersion are required",
+                                    required = true) PdpPolicies policies) {
+
+        Pair<Status, PdpGroupDeployResponse> pair = provider.deployPolicies(policies);
 
         return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
                         .entity(pair.getRight()).build();
index da3b2de..e8ea357 100644 (file)
@@ -24,6 +24,7 @@ import javax.ws.rs.core.Response;
 import org.apache.commons.lang3.tuple.Pair;
 import org.onap.policy.models.pap.concepts.PdpGroup;
 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
+import org.onap.policy.models.pap.concepts.PdpPolicies;
 
 /**
  * Provider for PAP component to deploy PDP groups.
@@ -36,7 +37,32 @@ public class PdpGroupDeployProvider {
      * @param group PDP group configuration
      * @return a pair containing the status and the response
      */
-    public Pair<Response.Status, PdpGroupDeployResponse> deploy(PdpGroup group) {
+    public Pair<Response.Status, PdpGroupDeployResponse> deployGroup(PdpGroup group) {
+
+        /*
+         * TODO Lock for updates - return error if already locked.
+         */
+
+        /*
+         * TODO Make updates - sending initial messages to PDPs and arranging for
+         * listeners to complete the deployment 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 PdpGroupDeployResponse());
+    }
+
+    /**
+     * Deploys or updates PDP policies.
+     *
+     * @param policies PDP policies
+     * @return a pair containing the status and the response
+     */
+    public Pair<Response.Status, PdpGroupDeployResponse> deployPolicies(PdpPolicies policies) {
 
         /*
          * TODO Lock for updates - return error if already locked.
index 107c46a..0384bf7 100644 (file)
@@ -31,17 +31,21 @@ import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
 
 public class TestPdpGroupDeleteControllerV1 extends CommonPapRestServer {
 
-    private static final String DELETE_ENDPOINT = "pdps/groups";
+    private static final String DELETE_GROUP_ENDPOINT = "pdps/groups";
+    private static final String DELETE_POLICIES_ENDPOINT = "pdps/policies";
 
     @Test
     public void testSwagger() throws Exception {
-        super.testSwagger(DELETE_ENDPOINT + "/{name}");
-        super.testSwagger(DELETE_ENDPOINT + "/{name}/versions/{version}");
+        super.testSwagger(DELETE_GROUP_ENDPOINT + "/{name}");
+        super.testSwagger(DELETE_GROUP_ENDPOINT + "/{name}/versions/{version}");
+
+        super.testSwagger(DELETE_POLICIES_ENDPOINT + "/{name}");
+        super.testSwagger(DELETE_POLICIES_ENDPOINT + "/{name}/versions/{version}");
     }
 
     @Test
     public void testDeleteGroup() throws Exception {
-        String uri = DELETE_ENDPOINT + "/my-name";
+        String uri = DELETE_GROUP_ENDPOINT + "/my-name";
 
         Invocation.Builder invocationBuilder = sendRequest(uri);
         Response rawresp = invocationBuilder.delete();
@@ -60,7 +64,45 @@ public class TestPdpGroupDeleteControllerV1 extends CommonPapRestServer {
 
     @Test
     public void testDeleteGroupVersion() throws Exception {
-        String uri = DELETE_ENDPOINT + "/my-name/versions/1.2.3";
+        String uri = DELETE_GROUP_ENDPOINT + "/my-name/versions/1.2.3";
+
+        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());
+
+        rawresp = invocationBuilder.delete();
+        resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
+        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
+        assertNull(resp.getErrorDetails());
+
+        // verify it fails when no authorization info is included
+        checkUnauthRequest(uri, req -> req.delete());
+    }
+
+    @Test
+    public void testDeletePolicies() throws Exception {
+        String uri = DELETE_POLICIES_ENDPOINT + "/my-name";
+
+        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());
+
+        rawresp = invocationBuilder.delete();
+        resp = rawresp.readEntity(PdpGroupDeleteResponse.class);
+        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
+        assertNull(resp.getErrorDetails());
+
+        // verify it fails when no authorization info is included
+        checkUnauthRequest(uri, req -> req.delete());
+    }
+
+    @Test
+    public void testDeletePoliciesVersion() throws Exception {
+        String uri = DELETE_POLICIES_ENDPOINT + "/my-name/versions/3";
 
         Invocation.Builder invocationBuilder = sendRequest(uri);
         Response rawresp = invocationBuilder.delete();
index d49f00c..0fc3577 100644 (file)
@@ -32,22 +32,26 @@ import javax.ws.rs.core.Response;
 import org.junit.Test;
 import org.onap.policy.models.pap.concepts.PdpGroup;
 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
+import org.onap.policy.models.pap.concepts.PdpPolicies;
 import org.onap.policy.models.pap.concepts.PdpSubGroup;
+import org.onap.policy.pdp.common.models.Policy;
 
 public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
 
-    private static final String DEPLOY_ENDPOINT = "pdps";
+    private static final String DEPLOY_GROUP_ENDPOINT = "pdps";
+    private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies";
 
     @Test
     public void testSwagger() throws Exception {
-        super.testSwagger(DEPLOY_ENDPOINT);
+        super.testSwagger(DEPLOY_GROUP_ENDPOINT);
+        super.testSwagger(DEPLOY_POLICIES_ENDPOINT);
     }
 
     @Test
-    public void testDeploy() throws Exception {
+    public void testDeployGroup() throws Exception {
         Entity<PdpGroup> entgrp = makePdpGroupEntity();
 
-        Invocation.Builder invocationBuilder = sendRequest(DEPLOY_ENDPOINT);
+        Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
         Response rawresp = invocationBuilder.post(entgrp);
         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
         assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
@@ -59,7 +63,26 @@ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
         assertNull(resp.getErrorDetails());
 
         // verify it fails when no authorization info is included
-        checkUnauthRequest(DEPLOY_ENDPOINT, req -> req.post(entgrp));
+        checkUnauthRequest(DEPLOY_GROUP_ENDPOINT, req -> req.post(entgrp));
+    }
+
+    @Test
+    public void testDeployPolicies() throws Exception {
+        Entity<PdpPolicies> entgrp = makePdpPoliciesEntity();
+
+        Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
+        Response rawresp = invocationBuilder.post(entgrp);
+        PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
+        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
+        assertNull(resp.getErrorDetails());
+
+        rawresp = invocationBuilder.post(entgrp);
+        resp = rawresp.readEntity(PdpGroupDeployResponse.class);
+        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
+        assertNull(resp.getErrorDetails());
+
+        // verify it fails when no authorization info is included
+        checkUnauthRequest(DEPLOY_POLICIES_ENDPOINT, req -> req.post(entgrp));
     }
 
     private Entity<PdpGroup> makePdpGroupEntity() {
@@ -72,7 +95,20 @@ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
         group.setVersion("my-version");
         group.setPdpSubgroups(Arrays.asList(subgrp));
 
-        Entity<PdpGroup> entgrp = Entity.entity(group, MediaType.APPLICATION_JSON);
-        return entgrp;
+        return Entity.entity(group, MediaType.APPLICATION_JSON);
+    }
+
+    private Entity<PdpPolicies> makePdpPoliciesEntity() {
+        Policy pol1 = new Policy();
+        pol1.setName("policy-a");
+        pol1.setPolicyVersion("1");
+
+        Policy pol2 = new Policy();
+        pol2.setName("policy-b");
+
+        PdpPolicies policies = new PdpPolicies();
+        policies.setPolicies(Arrays.asList(pol1, pol2));
+
+        return Entity.entity(policies, MediaType.APPLICATION_JSON);
     }
 }