Fix swagger tags in controllers
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupDeployControllerV1.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 Bell Canada. All rights reserved.
7  * Modifications Copyright (C) 2021 Nordix Foundation.
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 javax.ws.rs.HeaderParam;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.Response.Status;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.pap.concepts.PdpDeployPolicies;
42 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
43 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Class to provide REST end points for PAP component to deploy a PDP group.
49  */
50 public class PdpGroupDeployControllerV1 extends PapRestControllerV1 {
51     public static final String POLICY_STATUS_URI = "/policy/pap/v1/policies/status";
52
53     public static final String DEPLOYMENT_RESPONSE_MSG = "Use the policy status url to fetch the latest status. "
54             + "Kindly note that when a policy is successfully undeployed,"
55             + " it will no longer appear in policy status response.";
56
57     private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeployControllerV1.class);
58
59     private final PdpGroupDeployProvider provider = new PdpGroupDeployProvider();
60
61     /**
62      * Updates policy deployments within specific PDP groups.
63      *
64      * @param requestId request ID used in ONAP logging
65      * @param groups PDP group configuration
66      * @return a response
67      */
68     // @formatter:off
69     @POST
70     @Path("pdps/deployments/batch")
71     @ApiOperation(value = "Updates policy deployments within specific PDP groups",
72         notes = "Updates policy deployments within specific PDP groups, returning optional error details",
73         response = PdpGroupDeployResponse.class,
74         tags = {"Deployments Update"},
75         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
76         responseHeaders = {
77             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
78                             response = String.class),
79             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
80                             response = String.class),
81             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
82                             response = String.class),
83             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
84                             response = UUID.class)},
85         extensions = {
86             @Extension(name = EXTENSION_NAME,
87                 properties = {
88                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
89                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
90                 })
91             })
92     @ApiResponses(value = {
93         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
94         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
95         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
96     })
97     // @formatter:on
98
99     public Response updateGroupPolicies(
100             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
101             @ApiParam(value = "List of PDP Group Deployments", required = true) DeploymentGroups groups) {
102         return doOperation(requestId, "update policy deployments failed",
103             () -> provider.updateGroupPolicies(groups, getPrincipal()));
104     }
105
106     /**
107      * Deploys or updates PDP policies.
108      *
109      * @param requestId request ID used in ONAP logging
110      * @param policies PDP policies
111      * @return a response
112      */
113     // @formatter:off
114     @POST
115     @Path("pdps/policies")
116     @ApiOperation(value = "Deploy or update PDP Policies",
117         notes = "Deploys or updates PDP Policies, returning optional error details",
118         response = PdpGroupDeployResponse.class,
119         tags = {"Deployments Update"},
120         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
121         responseHeaders = {
122             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
123                             response = String.class),
124             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
125                             response = String.class),
126             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
127                             response = String.class),
128             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
129                             response = UUID.class)},
130         extensions = {
131             @Extension(name = EXTENSION_NAME,
132                 properties = {
133                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
134                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
135                 })
136             })
137     @ApiResponses(value = {
138         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
139         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
140         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
141     })
142     // @formatter:on
143
144     public Response deployPolicies(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
145             @ApiParam(value = "PDP Policies; only the name is required", required = true) PdpDeployPolicies policies) {
146         return doOperation(requestId, "deploy policies failed",
147             () -> provider.deployPolicies(policies, getPrincipal()));
148     }
149
150     /**
151      * Invokes an operation.
152      *
153      * @param requestId request ID
154      * @param errmsg error message to log if the operation throws an exception
155      * @param runnable operation to invoke
156      * @return a {@link PdpGroupDeployResponse} response entity
157      */
158     private Response doOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
159         try {
160             runnable.run();
161             return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.ACCEPTED)), requestId)
162                     .entity(new PdpGroupDeployResponse(DEPLOYMENT_RESPONSE_MSG, POLICY_STATUS_URI)).build();
163
164         } catch (PfModelException | PfModelRuntimeException e) {
165             logger.warn(errmsg, e);
166             var resp = new PdpGroupDeployResponse();
167             resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
168             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
169                     requestId).entity(resp).build();
170         }
171     }
172 }