2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.pap.main.rest;
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;
49 * Class to provide REST end points for PAP component to delete a PDP group.
51 public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
52 private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeleteControllerV1.class);
54 private final PdpGroupDeleteProvider provider = new PdpGroupDeleteProvider();
57 * Deletes a PDP group.
59 * @param requestId request ID used in ONAP logging
60 * @param groupName name of the PDP group to be deleted
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),
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)},
81 @Extension(name = EXTENSION_NAME,
83 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
84 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
97 return doOperation(requestId, "delete group failed", () -> provider.deleteGroup(groupName));
101 * Undeploys the latest version of a policy from the PDPs.
103 * @param requestId request ID used in ONAP logging
104 * @param policyName name of the PDP Policy to be deleted
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),
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)},
125 @Extension(name = EXTENSION_NAME,
127 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
128 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
141 return doUndeployOperation(requestId, "undeploy policy failed",
142 () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, null), getPrincipal()));
146 * Undeploys a specific version of a policy from the PDPs.
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
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),
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)},
171 @Extension(name = EXTENSION_NAME,
173 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
174 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
189 return doUndeployOperation(requestId, "undeploy policy failed",
190 () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, version), getPrincipal()));
194 * Invokes an operation.
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
201 private Response doOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
204 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId)
205 .entity(new PdpGroupDeleteResponse()).build();
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();
217 * Invokes the undeployment operation.
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
224 private Response doUndeployOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
227 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.ACCEPTED)), requestId)
228 .entity(new PdpGroupDeployResponse(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG,
229 PdpGroupDeployControllerV1.POLICY_STATUS_URI))
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();