diff options
author | Jim Hahn <jrh3@att.com> | 2019-04-29 13:02:35 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-04-29 15:32:13 -0400 |
commit | 3bca010bdcb7b14d7bd3e7cd7b39ee1a562b27b4 (patch) | |
tree | b3f9b7ddeaa2d77ac3d6f086fb91dc89da5d4a3e /main/src | |
parent | cccfb11b59becaaf86adc4c88600bd70f2519b0d (diff) |
Sonar fixes for PAP "duplicate code"
Extracted common code from "delete" controller.
Extracted common code from "deploy" controller.
Not sure if this will actually fix the sonar issue or not. If it's
really complaining about the code, then this should fix it. On
the other hand, if it's complaining about the annotations, then
I don't think there's a way around it.
Change-Id: I7c68393f3fc1b9719987d850582bd8ed335b9663
Issue-ID: POLICY-1711
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src')
3 files changed, 42 insertions, 40 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PapRestControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestControllerV1.java index ccdebc33..c4b7b4a4 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/PapRestControllerV1.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestControllerV1.java @@ -33,6 +33,7 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response.ResponseBuilder; +import org.onap.policy.models.base.PfModelException; /** * Version v1 common superclass to provide REST endpoints for PAP component. @@ -117,4 +118,12 @@ public class PapRestControllerV1 { return respBuilder.header(REQUEST_ID_NAME, requestId); } + + /** + * Functions that throw {@link PfModelException}. + */ + @FunctionalInterface + public static interface RunnableWithPfEx { + public void run() throws PfModelException; + } } diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteControllerV1.java index ad8b5081..47b77bdc 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteControllerV1.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteControllerV1.java @@ -86,18 +86,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) { - try { - provider.deleteGroup(groupName); - return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId) - .entity(new PdpGroupDeleteResponse()).build(); - - } catch (PfModelException | PfModelRuntimeException e) { - logger.warn("delete group failed", e); - PdpGroupDeleteResponse resp = new PdpGroupDeleteResponse(); - resp.setErrorDetails(e.getErrorResponse().getErrorMessage()); - return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())), - requestId).entity(resp).build(); - } + return doOperation(requestId, "delete group failed", () -> provider.deleteGroup(groupName)); } /** @@ -135,18 +124,8 @@ 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) { - try { - provider.undeploy(new ToscaPolicyIdentifierOptVersion(policyName, null)); - return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId) - .entity(new PdpGroupDeleteResponse()).build(); - - } catch (PfModelException | PfModelRuntimeException e) { - logger.warn("undeploy policy failed", e); - PdpGroupDeleteResponse resp = new PdpGroupDeleteResponse(); - resp.setErrorDetails(e.getErrorResponse().getErrorMessage()); - return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())), - requestId).entity(resp).build(); - } + return doOperation(requestId, "undeploy policy failed", + () -> provider.undeploy(new ToscaPolicyIdentifierOptVersion(policyName, null))); } /** @@ -187,13 +166,26 @@ 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) { + return doOperation(requestId, "undeploy policy failed", + () -> provider.undeploy(new ToscaPolicyIdentifierOptVersion(policyName, version))); + } + + /** + * Invokes an operation. + * + * @param requestId request ID + * @param errmsg error message to log if the operation throws an exception + * @param runnable operation to invoke + * @return a {@link PdpGroupDeleteResponse} response entity + */ + private Response doOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) { try { - provider.undeploy(new ToscaPolicyIdentifierOptVersion(policyName, version)); + runnable.run(); return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId) .entity(new PdpGroupDeleteResponse()).build(); } catch (PfModelException | PfModelRuntimeException e) { - logger.warn("undeploy policy failed", e); + logger.warn(errmsg, e); PdpGroupDeleteResponse resp = new PdpGroupDeleteResponse(); resp.setErrorDetails(e.getErrorResponse().getErrorMessage()); return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())), diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployControllerV1.java index d3e6c006..a9e3d5c7 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployControllerV1.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployControllerV1.java @@ -86,18 +86,7 @@ public class PdpGroupDeployControllerV1 extends PapRestControllerV1 { public Response deployGroup(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId, @ApiParam(value = "List of PDP Group Configuration", required = true) PdpGroups groups) { - try { - provider.createOrUpdateGroups(groups); - return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId) - .entity(new PdpGroupDeployResponse()).build(); - - } catch (PfModelException | PfModelRuntimeException e) { - logger.warn("create groups failed", e); - PdpGroupDeployResponse resp = new PdpGroupDeployResponse(); - resp.setErrorDetails(e.getErrorResponse().getErrorMessage()); - return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())), - requestId).entity(resp).build(); - } + return doOperation(requestId, "create groups failed", () -> provider.createOrUpdateGroups(groups)); } /** @@ -136,13 +125,25 @@ public class PdpGroupDeployControllerV1 extends PapRestControllerV1 { @ApiParam(value = "PDP Policies; only the name is required", required = true) PdpDeployPolicies policies) { + return doOperation(requestId, "deploy policies failed", () -> provider.deployPolicies(policies)); + } + + /** + * Invokes an operation. + * + * @param requestId request ID + * @param errmsg error message to log if the operation throws an exception + * @param runnable operation to invoke + * @return a {@link PdpGroupDeployResponse} response entity + */ + private Response doOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) { try { - provider.deployPolicies(policies); + runnable.run(); return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId) .entity(new PdpGroupDeployResponse()).build(); } catch (PfModelException | PfModelRuntimeException e) { - logger.warn("deploy policies failed", e); + logger.warn(errmsg, e); PdpGroupDeployResponse resp = new PdpGroupDeployResponse(); resp.setErrorDetails(e.getErrorResponse().getErrorMessage()); return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())), |