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