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