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