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 lombok.RequiredArgsConstructor;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse;
38 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.DeleteMapping;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.RequestHeader;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RestController;
50 * Class to provide REST end points for PAP component to delete a PDP group.
53 @RequestMapping(path = "/policy/pap/v1")
54 @RequiredArgsConstructor
55 public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
56 private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeleteControllerV1.class);
58 private final PdpGroupDeleteProvider provider;
61 * Deletes a PDP group.
63 * @param requestId request ID used in ONAP logging
64 * @param groupName name of the PDP group to be deleted
68 @DeleteMapping("pdps/groups/{name}")
69 @ApiOperation(value = "Delete PDP Group",
70 notes = "Deletes a PDP Group, returning optional error details",
71 response = PdpGroupDeleteResponse.class,
72 tags = {"PdpGroup Delete"},
73 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
75 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
76 response = String.class),
77 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
78 response = String.class),
79 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
80 response = String.class),
81 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
82 response = UUID.class)},
84 @Extension(name = EXTENSION_NAME,
86 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
87 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
90 @ApiResponses(value = {
91 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
92 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
93 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
96 public ResponseEntity<PdpGroupDeleteResponse> deleteGroup(
97 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
99 value = REQUEST_ID_NAME) final UUID requestId,
100 @ApiParam(value = "PDP Group Name") @PathVariable("name") String groupName) {
101 return doOperation(requestId, "delete group failed", () -> provider.deleteGroup(groupName));
105 * Undeploys the latest version of a policy from the PDPs.
107 * @param requestId request ID used in ONAP logging
108 * @param policyName name of the PDP Policy to be deleted
112 @DeleteMapping("pdps/policies/{name}")
113 @ApiOperation(value = "Undeploy a PDP Policy from PDPs",
114 notes = "Undeploys the latest version of a policy from the PDPs, returning optional error details",
115 response = PdpGroupDeployResponse.class,
116 tags = {"PdpGroup Delete"},
117 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
119 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
120 response = String.class),
121 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
122 response = String.class),
123 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
124 response = String.class),
125 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
126 response = UUID.class)},
128 @Extension(name = EXTENSION_NAME,
130 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
131 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
134 @ApiResponses(value = {
135 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
136 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
137 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
140 public ResponseEntity<PdpGroupDeployResponse> deletePolicy(
141 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
143 value = REQUEST_ID_NAME) final UUID requestId,
144 @ApiParam(value = "PDP Policy Name") @PathVariable("name") String policyName) {
146 return doUndeployOperation(requestId, "undeploy policy failed",
147 () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, null), getPrincipal()));
151 * Undeploys a specific version of a policy from the PDPs.
153 * @param requestId request ID used in ONAP logging
154 * @param policyName name of the PDP Policy to be deleted
155 * @param version version to be deleted
159 @DeleteMapping("pdps/policies/{name}/versions/{version}")
160 @ApiOperation(value = "Undeploy version of a PDP Policy from PDPs",
161 notes = "Undeploys a specific version of a policy from the PDPs, returning optional error details",
162 response = PdpGroupDeployResponse.class,
163 tags = {"PdpGroup Delete"},
164 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
166 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
167 response = String.class),
168 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
169 response = String.class),
170 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
171 response = String.class),
172 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
173 response = UUID.class)},
175 @Extension(name = EXTENSION_NAME,
177 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
178 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
181 @ApiResponses(value = {
182 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
183 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
184 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
187 public ResponseEntity<PdpGroupDeployResponse> deletePolicyVersion(
188 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
190 value = REQUEST_ID_NAME) final UUID requestId,
191 @ApiParam(value = "PDP Policy Name") @PathVariable("name") String policyName,
192 @ApiParam(value = "PDP Policy Version") @PathVariable("version") String version) {
194 return doUndeployOperation(requestId, "undeploy policy failed",
195 () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, version), getPrincipal()));
199 * Invokes an 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 PdpGroupDeleteResponse} response entity
206 private ResponseEntity<PdpGroupDeleteResponse> doOperation(UUID requestId, String errmsg,
207 RunnableWithPfEx runnable) {
210 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
211 .body(new PdpGroupDeleteResponse());
213 } catch (PfModelException | PfModelRuntimeException e) {
214 logger.warn(errmsg, e);
215 var resp = new PdpGroupDeleteResponse();
216 resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
217 return addLoggingHeaders(
218 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
219 requestId).body(resp);
224 * Invokes the undeployment operation.
226 * @param requestId request ID
227 * @param errmsg error message to log if the operation throws an exception
228 * @param runnable operation to invoke
229 * @return a {@link PdpGroupDeployResponse} response entity
231 private ResponseEntity<PdpGroupDeployResponse> doUndeployOperation(UUID requestId, String errmsg,
232 RunnableWithPfEx runnable) {
235 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.accepted()), requestId)
236 .body(new PdpGroupDeployResponse(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG,
237 PdpGroupDeployControllerV1.POLICY_STATUS_URI));
239 } catch (PfModelException | PfModelRuntimeException e) {
240 logger.warn(errmsg, e);
241 var resp = new PdpGroupDeployResponse();
242 resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
243 return addLoggingHeaders(
244 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
245 requestId).body(resp);