Make PAP stateless
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PolicyStatusControllerV1.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 Nordix Foundation.
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.Collection;
33 import java.util.UUID;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.core.Response;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.pap.concepts.PolicyStatus;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Class to provide REST end points for PAP component to retrieve the status of deployed
48  * policies.
49  */
50 public class PolicyStatusControllerV1 extends PapRestControllerV1 {
51     private static final String GET_DEPLOYMENTS_FAILED = "get deployments failed";
52
53     private static final Logger logger = LoggerFactory.getLogger(PolicyStatusControllerV1.class);
54
55     private final PolicyStatusProvider provider = new PolicyStatusProvider();
56
57     /**
58      * Queries status of all deployed policies.
59      *
60      * @param requestId request ID used in ONAP logging
61      * @return a response
62      */
63     // @formatter:off
64     @GET
65     @Path("policies/deployed")
66     @ApiOperation(value = "Queries status of all deployed policies",
67         notes = "Queries status of all deployed policies, returning success and failure counts of the PDPs",
68         responseContainer = "List", response = PolicyStatus.class,
69         tags = {"Policy Administration (PAP) API"},
70         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
71         responseHeaders = {
72             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
73                             response = String.class),
74             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
75                             response = String.class),
76             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
77                             response = String.class),
78             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
79                             response = UUID.class)},
80         extensions = {@Extension(name = EXTENSION_NAME,
81             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
82                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
83     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
84                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
85                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
86     // @formatter:on
87
88     public Response queryAllDeployedPolicies(
89                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
90
91         try {
92             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
93                             .entity(provider.getStatus()).build();
94
95         } catch (PfModelException | PfModelRuntimeException e) {
96             logger.warn(GET_DEPLOYMENTS_FAILED, e);
97             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
98                 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
99         }
100     }
101
102
103     /**
104      * Queries status of specific deployed policies.
105      *
106      * @param requestId request ID used in ONAP logging
107      * @return a response
108      */
109     // @formatter:off
110     @GET
111     @Path("policies/deployed/{name}")
112     @ApiOperation(value = "Queries status of specific deployed policies",
113         notes = "Queries status of specific deployed policies, returning success and failure counts of the PDPs",
114         responseContainer = "List", response = PolicyStatus.class,
115         tags = {"Policy Administration (PAP) API"},
116         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
117         responseHeaders = {
118             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
119                             response = String.class),
120             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
121                             response = String.class),
122             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
123                             response = String.class),
124             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
125                             response = UUID.class)},
126         extensions = {@Extension(name = EXTENSION_NAME,
127             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
128                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
129     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
130                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
131                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
132     // @formatter:on
133
134     public Response queryDeployedPolicies(
135                     @ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
136                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
137
138         try {
139             Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, null));
140             if (result.isEmpty()) {
141                 return makeNotFoundResponse(requestId);
142
143             } else {
144                 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
145                                 .entity(result).build();
146             }
147
148         } catch (PfModelException | PfModelRuntimeException e) {
149             logger.warn(GET_DEPLOYMENTS_FAILED, e);
150             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
151                 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
152         }
153     }
154
155
156     /**
157      * Queries status of a specific deployed policy.
158      *
159      * @param requestId request ID used in ONAP logging
160      * @return a response
161      */
162     // @formatter:off
163     @GET
164     @Path("policies/deployed/{name}/{version}")
165     @ApiOperation(value = "Queries status of a specific deployed policy",
166         notes = "Queries status of a specific deployed policy, returning success and failure counts of the PDPs",
167         response = PolicyStatus.class,
168         tags = {"Policy Administration (PAP) API"},
169         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
170         responseHeaders = {
171             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
172                             response = String.class),
173             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
174                             response = String.class),
175             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
176                             response = String.class),
177             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
178                             response = UUID.class)},
179         extensions = {@Extension(name = EXTENSION_NAME,
180             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
181                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
182     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
183                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
184                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
185     // @formatter:on
186
187     public Response queryDeployedPolicy(@ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
188                     @ApiParam(value = "Policy Version", required = true) @PathParam("version") String version,
189                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
190
191         try {
192             Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, version));
193             if (result.isEmpty()) {
194                 return makeNotFoundResponse(requestId);
195
196             } else {
197                 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
198                                 .entity(result.iterator().next()).build();
199             }
200
201         } catch (PfModelException | PfModelRuntimeException e) {
202             logger.warn(GET_DEPLOYMENTS_FAILED, e);
203             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
204                 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
205         }
206     }
207
208
209     /**
210      * Makes a "not found" response.
211      *
212      * @param requestId request ID
213      * @return a "not found" response
214      */
215     private Response makeNotFoundResponse(final UUID requestId) {
216         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.NOT_FOUND)), requestId)
217                         .build();
218     }
219 }