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)},
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)})
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) {
91 return doOperation(requestId, "delete group failed", () -> provider.deleteGroup(groupName));
95 * Undeploys the latest version of a policy from the PDPs.
97 * @param requestId request ID used in ONAP logging
98 * @param policyName name of the PDP Policy to be deleted
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),
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)})
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) {
129 return doUndeployOperation(requestId, "undeploy policy failed",
130 () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, null)));
134 * Undeploys a specific version of a policy from the PDPs.
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
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),
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)})
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) {
171 return doUndeployOperation(requestId, "undeploy policy failed",
172 () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, version)));
176 * Invokes an operation.
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
183 private Response doOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
186 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.OK)), requestId)
187 .entity(new PdpGroupDeleteResponse()).build();
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();
199 * Invokes the undeployment operation.
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
206 private Response doUndeployOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
209 return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.ACCEPTED)), requestId)
210 .entity(new PdpGroupDeployResponse(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG,
211 PdpGroupDeployControllerV1.POLICY_STATUS_URI))
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();