PolicyAudit creation when deploy/undeploy triggered.
[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 = {"Policy Administration (PAP) API"},
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 = {@Extension(name = EXTENSION_NAME,
86             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
87                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
88     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
89                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
90                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
91     // @formatter:on
92
93     public Response updateGroupPolicies(
94             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
95             @ApiParam(value = "List of PDP Group Deployments", required = true) DeploymentGroups groups) {
96         return doOperation(requestId, "update policy deployments failed",
97                 () -> provider.updateGroupPolicies(groups, getPrincipal()));
98     }
99
100     /**
101      * Deploys or updates PDP policies.
102      *
103      * @param requestId request ID used in ONAP logging
104      * @param policies PDP policies
105      * @return a response
106      */
107     // @formatter:off
108     @POST
109     @Path("pdps/policies")
110     @ApiOperation(value = "Deploy or update PDP Policies",
111         notes = "Deploys or updates PDP Policies, returning optional error details",
112         response = PdpGroupDeployResponse.class,
113         tags = {"Policy Administration (PAP) API"},
114         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
115         responseHeaders = {
116             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
117                             response = String.class),
118             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
119                             response = String.class),
120             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
121                             response = String.class),
122             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
123                             response = UUID.class)},
124         extensions = {@Extension(name = EXTENSION_NAME,
125             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
126                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
127     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
128                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
129                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
130     // @formatter:on
131
132     public Response deployPolicies(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
133             @ApiParam(value = "PDP Policies; only the name is required", required = true) PdpDeployPolicies policies) {
134         return doOperation(requestId, "deploy policies failed",
135                 () -> provider.deployPolicies(policies, getPrincipal()));
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)).build();
151
152         } catch (PfModelException | PfModelRuntimeException e) {
153             logger.warn(errmsg, e);
154             var resp = new PdpGroupDeployResponse();
155             resp.setErrorDetails(e.getErrorResponse().getErrorMessage());
156             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
157                     requestId).entity(resp).build();
158         }
159     }
160 }