Migrate pap startup & controllers to spring boot
[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 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;
48
49 /**
50  * Class to provide REST end points for PAP component to delete a PDP group.
51  */
52 @RestController
53 @RequestMapping(path = "/policy/pap/v1")
54 @RequiredArgsConstructor
55 public class PdpGroupDeleteControllerV1 extends PapRestControllerV1 {
56     private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeleteControllerV1.class);
57
58     private final PdpGroupDeleteProvider provider;
59
60     /**
61      * Deletes a PDP group.
62      *
63      * @param requestId request ID used in ONAP logging
64      * @param groupName name of the PDP group to be deleted
65      * @return a response
66      */
67     // @formatter:off
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),
74         responseHeaders = {
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)},
83         extensions = {
84             @Extension(name = EXTENSION_NAME,
85                 properties = {
86                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
87                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
88                 })
89             })
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)
94     })
95     // @formatter:on
96     public ResponseEntity<PdpGroupDeleteResponse> deleteGroup(
97         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
98             required = false,
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));
102     }
103
104     /**
105      * Undeploys the latest version of a policy from the PDPs.
106      *
107      * @param requestId request ID used in ONAP logging
108      * @param policyName name of the PDP Policy to be deleted
109      * @return a response
110      */
111     // @formatter:off
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),
118         responseHeaders = {
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)},
127         extensions = {
128             @Extension(name = EXTENSION_NAME,
129                 properties = {
130                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
131                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
132                 })
133             })
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)
138     })
139     // @formatter:on
140     public ResponseEntity<PdpGroupDeployResponse> deletePolicy(
141         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
142             required = false,
143             value = REQUEST_ID_NAME) final UUID requestId,
144         @ApiParam(value = "PDP Policy Name") @PathVariable("name") String policyName) {
145
146         return doUndeployOperation(requestId, "undeploy policy failed",
147             () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, null), getPrincipal()));
148     }
149
150     /**
151      * Undeploys a specific version of a policy from the PDPs.
152      *
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
156      * @return a response
157      */
158     // @formatter:off
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),
165         responseHeaders = {
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)},
174         extensions = {
175             @Extension(name = EXTENSION_NAME,
176                 properties = {
177                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
178                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
179                 })
180             })
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)
185     })
186     // @formatter:on
187     public ResponseEntity<PdpGroupDeployResponse> deletePolicyVersion(
188         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
189             required = false,
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) {
193
194         return doUndeployOperation(requestId, "undeploy policy failed",
195             () -> provider.undeploy(new ToscaConceptIdentifierOptVersion(policyName, version), getPrincipal()));
196     }
197
198     /**
199      * Invokes an operation.
200      *
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
205      */
206     private ResponseEntity<PdpGroupDeleteResponse> doOperation(UUID requestId, String errmsg,
207         RunnableWithPfEx runnable) {
208         try {
209             runnable.run();
210             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
211                 .body(new PdpGroupDeleteResponse());
212
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);
220         }
221     }
222
223     /**
224      * Invokes the undeployment operation.
225      *
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
230      */
231     private ResponseEntity<PdpGroupDeployResponse> doUndeployOperation(UUID requestId, String errmsg,
232         RunnableWithPfEx runnable) {
233         try {
234             runnable.run();
235             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.accepted()), requestId)
236                 .body(new PdpGroupDeployResponse(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG,
237                     PdpGroupDeployControllerV1.POLICY_STATUS_URI));
238
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);
246         }
247     }
248 }