Merge "Send pdp-update if PDP response doesn't match DB"
[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 = {
81             @Extension(name = EXTENSION_NAME,
82                 properties = {
83                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
84                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
85                 })
86             })
87     @ApiResponses(value = {
88         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
89         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
90         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
91     })
92     // @formatter:on
93
94     public Response deleteGroup(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
95                     @ApiParam(value = "PDP Group Name", required = true) @PathParam("name") String groupName) {
96
97         return doOperation(requestId, "delete group failed", () -> provider.deleteGroup(groupName));
98     }
99
100     /**
101      * Undeploys the latest version of a policy from the PDPs.
102      *
103      * @param requestId request ID used in ONAP logging
104      * @param policyName name of the PDP Policy to be deleted
105      * @return a response
106      */
107     // @formatter:off
108     @DELETE
109     @Path("pdps/policies/{name}")
110     @ApiOperation(value = "Undeploy a PDP Policy from PDPs",
111         notes = "Undeploys the latest version of a policy from the PDPs, returning optional error details",
112         response = PdpGroupDeployResponse.class,
113         tags = {"Policy Administration (PAP) API"},
114         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
115         responseHeaders = {
116             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
117                             response = String.class),
118             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
119                             response = String.class),
120             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
121                             response = String.class),
122             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
123                             response = UUID.class)},
124         extensions = {
125             @Extension(name = EXTENSION_NAME,
126                 properties = {
127                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
128                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
129                 })
130             })
131     @ApiResponses(value = {
132         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
133         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
134         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
135     })
136     // @formatter:on
137
138     public Response deletePolicy(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
139                     @ApiParam(value = "PDP Policy Name", required = true) @PathParam("name") String policyName) {
140
141         return doUndeployOperation(requestId, "undeploy policy failed",
142             () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, null)));
143     }
144
145     /**
146      * Undeploys a specific version of a policy from the PDPs.
147      *
148      * @param requestId request ID used in ONAP logging
149      * @param policyName name of the PDP Policy to be deleted
150      * @param version version to be deleted
151      * @return a response
152      */
153     // @formatter:off
154     @DELETE
155     @Path("pdps/policies/{name}/versions/{version}")
156     @ApiOperation(value = "Undeploy version of a PDP Policy from PDPs",
157         notes = "Undeploys a specific version of a policy from the PDPs, returning optional error details",
158         response = PdpGroupDeployResponse.class,
159         tags = {"Policy Administration (PAP) API"},
160         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
161         responseHeaders = {
162             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
163                             response = String.class),
164             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
165                             response = String.class),
166             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
167                             response = String.class),
168             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
169                             response = UUID.class)},
170         extensions = {
171             @Extension(name = EXTENSION_NAME,
172                 properties = {
173                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
174                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
175                 })
176             })
177     @ApiResponses(value = {
178         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
179         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
180         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
181     })
182     // @formatter:on
183
184     public Response deletePolicyVersion(
185                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
186                     @ApiParam(value = "PDP Policy Name", required = true) @PathParam("name") String policyName,
187                     @ApiParam(value = "PDP Policy Version", required = true) @PathParam("version") String version) {
188
189         return doUndeployOperation(requestId, "undeploy policy failed",
190             () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, version)));
191     }
192
193     /**
194      * Invokes an operation.
195      *
196      * @param requestId request ID
197      * @param errmsg error message to log if the operation throws an exception
198      * @param runnable operation to invoke
199      * @return a {@link PdpGroupDeleteResponse} response entity
200      */
201     private Response doOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
202         try {
203             runnable.run();
204             return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId)
205                             .entity(new PdpGroupDeleteResponse()).build();
206
207         } catch (PfModelException | PfModelRuntimeException e) {
208             logger.warn(errmsg, e);
209             var resp = new PdpGroupDeleteResponse();
210             resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
211             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
212                             requestId).entity(resp).build();
213         }
214     }
215
216     /**
217      * Invokes the undeployment operation.
218      *
219      * @param requestId request ID
220      * @param errmsg error message to log if the operation throws an exception
221      * @param runnable operation to invoke
222      * @return a {@link PdpGroupDeployResponse} response entity
223      */
224     private Response doUndeployOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
225         try {
226             runnable.run();
227             return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.ACCEPTED)), requestId)
228                 .entity(new PdpGroupDeployResponse(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG,
229                     PdpGroupDeployControllerV1.POLICY_STATUS_URI))
230                 .build();
231
232         } catch (PfModelException | PfModelRuntimeException e) {
233             logger.warn(errmsg, e);
234             var resp = new PdpGroupDeployResponse();
235             resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
236             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
237                             requestId).entity(resp).build();
238         }
239     }
240 }