Updating PAP deployment API to reflect actual status
[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 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiParam;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import io.swagger.annotations.Authorization;
29 import io.swagger.annotations.Extension;
30 import io.swagger.annotations.ExtensionProperty;
31 import io.swagger.annotations.ResponseHeader;
32 import java.util.UUID;
33 import javax.ws.rs.HeaderParam;
34 import javax.ws.rs.POST;
35 import javax.ws.rs.Path;
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.PdpDeployPolicies;
41 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
42 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Class to provide REST end points for PAP component to deploy a PDP group.
48  */
49 public class PdpGroupDeployControllerV1 extends PapRestControllerV1 {
50     public static final String POLICY_STATUS_URI = "/policy/pap/v1/policies/status";
51
52     public static final String DEPLOYMENT_RESPONSE_MSG = "Use the policy status url to fetch the latest status. "
53         + "Kindly note that when a policy is successfully undeployed,"
54         + " it will no longer appear in policy status response.";
55
56     private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeployControllerV1.class);
57
58     private final PdpGroupDeployProvider provider = new PdpGroupDeployProvider();
59
60     /**
61      * Updates policy deployments within specific PDP groups.
62      *
63      * @param requestId request ID used in ONAP logging
64      * @param groups PDP group configuration
65      * @return a response
66      */
67     // @formatter:off
68     @POST
69     @Path("pdps/deployments/batch")
70     @ApiOperation(value = "Updates policy deployments within specific PDP groups",
71         notes = "Updates policy deployments within specific PDP groups, returning optional error details",
72         response = PdpGroupDeployResponse.class,
73         tags = {"Policy Administration (PAP) API"},
74         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
75         responseHeaders = {
76             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
77                             response = String.class),
78             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
79                             response = String.class),
80             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
81                             response = String.class),
82             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
83                             response = UUID.class)},
84         extensions = {@Extension(name = EXTENSION_NAME,
85             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
86                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
87     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
88                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
89                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
90     // @formatter:on
91
92     public Response updateGroupPolicies(
93                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
94                     @ApiParam(value = "List of PDP Group Deployments", required = true) DeploymentGroups groups) {
95
96         return doOperation(requestId, "update policy deployments failed", () -> provider.updateGroupPolicies(groups));
97     }
98
99     /**
100      * Deploys or updates PDP policies.
101      *
102      * @param requestId request ID used in ONAP logging
103      * @param policies PDP policies
104      * @return a response
105      */
106     // @formatter:off
107     @POST
108     @Path("pdps/policies")
109     @ApiOperation(value = "Deploy or update PDP Policies",
110         notes = "Deploys or updates PDP Policies, returning optional error details",
111         response = PdpGroupDeployResponse.class,
112         tags = {"Policy Administration (PAP) API"},
113         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
114         responseHeaders = {
115             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
116                             response = String.class),
117             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
118                             response = String.class),
119             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
120                             response = String.class),
121             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
122                             response = UUID.class)},
123         extensions = {@Extension(name = EXTENSION_NAME,
124             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
125                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
126     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
127                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
128                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
129     // @formatter:on
130
131     public Response deployPolicies(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
132                     @ApiParam(value = "PDP Policies; only the name is required",
133                                     required = true) PdpDeployPolicies policies) {
134
135         return doOperation(requestId, "deploy policies failed", () -> provider.deployPolicies(policies));
136     }
137
138     /**
139      * Invokes an operation.
140      *
141      * @param requestId request ID
142      * @param errmsg error message to log if the operation throws an exception
143      * @param runnable operation to invoke
144      * @return a {@link PdpGroupDeployResponse} response entity
145      */
146     private Response doOperation(UUID requestId, String errmsg, RunnableWithPfEx runnable) {
147         try {
148             runnable.run();
149             return addLoggingHeaders(addVersionControlHeaders(Response.status(Status.ACCEPTED)), requestId)
150                 .entity(new PdpGroupDeployResponse(DEPLOYMENT_RESPONSE_MSG, POLICY_STATUS_URI))
151                 .build();
152
153         } catch (PfModelException | PfModelRuntimeException e) {
154             logger.warn(errmsg, e);
155             PdpGroupDeployResponse resp = new PdpGroupDeployResponse();
156             resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
157             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
158                 requestId).entity(resp).build();
159         }
160     }
161 }