Fix sonars in policy-pap
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupDeleteControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiParam;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29 import io.swagger.annotations.Authorization;
30 import io.swagger.annotations.Extension;
31 import io.swagger.annotations.ExtensionProperty;
32 import io.swagger.annotations.ResponseHeader;
33 import java.util.UUID;
34 import javax.ws.rs.DELETE;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.Response.Status;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.base.PfModelRuntimeException;
42 import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
43 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Class to provide REST end points for PAP component to delete a PDP group.
50  */
51 public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
52     private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeleteControllerV1.class);
53
54     private final PdpGroupDeleteProvider provider = new PdpGroupDeleteProvider();
55
56     /**
57      * Deletes a PDP group.
58      *
59      * @param requestId request ID used in ONAP logging
60      * @param groupName name of the PDP group to be deleted
61      * @return a response
62      */
63     // @formatter:off
64     @DELETE
65     @Path("pdps/groups/{name}")
66     @ApiOperation(value = "Delete PDP Group",
67         notes = "Deletes a PDP Group, returning optional error details",
68         response = PdpGroupDeleteResponse.class,
69         tags = {"Policy Administration (PAP) API"},
70         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
71         responseHeaders = {
72             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
73                             response = String.class),
74             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
75                             response = String.class),
76             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
77                             response = String.class),
78             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
79                             response = UUID.class)},
80         extensions = {@Extension(name = EXTENSION_NAME,
81             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
82                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
83     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
84                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
85                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
86     // @formatter:on
87
88     public Response deleteGroup(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
89                     @ApiParam(value = "PDP Group Name", required = true) @PathParam("name") String groupName) {
90
91         return doOperation(requestId, "delete group failed", () -> provider.deleteGroup(groupName));
92     }
93
94     /**
95      * Undeploys the latest version of a policy from the PDPs.
96      *
97      * @param requestId request ID used in ONAP logging
98      * @param policyName name of the PDP Policy to be deleted
99      * @return a response
100      */
101     // @formatter:off
102     @DELETE
103     @Path("pdps/policies/{name}")
104     @ApiOperation(value = "Undeploy a PDP Policy from PDPs",
105         notes = "Undeploys the latest version of a policy from the PDPs, returning optional error details",
106         response = PdpGroupDeployResponse.class,
107         tags = {"Policy Administration (PAP) API"},
108         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
109         responseHeaders = {
110             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
111                             response = String.class),
112             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
113                             response = String.class),
114             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
115                             response = String.class),
116             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
117                             response = UUID.class)},
118         extensions = {@Extension(name = EXTENSION_NAME,
119             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
120                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
121     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
122                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
123                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
124     // @formatter:on
125
126     public Response deletePolicy(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
127                     @ApiParam(value = "PDP Policy Name", required = true) @PathParam("name") String policyName) {
128
129         return doUndeployOperation(requestId, "undeploy policy failed",
130             () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, null)));
131     }
132
133     /**
134      * Undeploys a specific version of a policy from the PDPs.
135      *
136      * @param requestId request ID used in ONAP logging
137      * @param policyName name of the PDP Policy to be deleted
138      * @param version version to be deleted
139      * @return a response
140      */
141     // @formatter:off
142     @DELETE
143     @Path("pdps/policies/{name}/versions/{version}")
144     @ApiOperation(value = "Undeploy version of a PDP Policy from PDPs",
145         notes = "Undeploys a specific version of a policy from the PDPs, returning optional error details",
146         response = PdpGroupDeployResponse.class,
147         tags = {"Policy Administration (PAP) API"},
148         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
149         responseHeaders = {
150             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
151                             response = String.class),
152             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
153                             response = String.class),
154             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
155                             response = String.class),
156             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
157                             response = UUID.class)},
158         extensions = {@Extension(name = EXTENSION_NAME,
159             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
160                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
161     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
162                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
163                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
164     // @formatter:on
165
166     public Response deletePolicyVersion(
167                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
168                     @ApiParam(value = "PDP Policy Name", required = true) @PathParam("name") String policyName,
169                     @ApiParam(value = "PDP Policy Version", required = true) @PathParam("version") String version) {
170
171         return doUndeployOperation(requestId, "undeploy policy failed",
172             () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, version)));
173     }
174
175     /**
176      * Invokes an operation.
177      *
178      * @param requestId request ID
179      * @param errmsg error message to log if the operation throws an exception
180      * @param runnable operation to invoke
181      * @return a {@link PdpGroupDeleteResponse} response entity
182      */
183     private Response doOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
184         try {
185             runnable.run();
186             return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId)
187                             .entity(new PdpGroupDeleteResponse()).build();
188
189         } catch (PfModelException | PfModelRuntimeException e) {
190             logger.warn(errmsg, e);
191             var resp = new PdpGroupDeleteResponse();
192             resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
193             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
194                             requestId).entity(resp).build();
195         }
196     }
197
198     /**
199      * Invokes the undeployment operation.
200      *
201      * @param requestId request ID
202      * @param errmsg error message to log if the operation throws an exception
203      * @param runnable operation to invoke
204      * @return a {@link PdpGroupDeployResponse} response entity
205      */
206     private Response doUndeployOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
207         try {
208             runnable.run();
209             return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.ACCEPTED)), requestId)
210                 .entity(new PdpGroupDeployResponse(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG,
211                     PdpGroupDeployControllerV1.POLICY_STATUS_URI))
212                 .build();
213
214         } catch (PfModelException | PfModelRuntimeException e) {
215             logger.warn(errmsg, e);
216             var resp = new PdpGroupDeployResponse();
217             resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
218             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
219                             requestId).entity(resp).build();
220         }
221     }
222 }