Updating PAP deployment API to reflect actual status
[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  * 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.Collection;
34 import java.util.UUID;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.HeaderParam;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.core.Response;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.base.PfModelRuntimeException;
42 import org.onap.policy.models.pap.concepts.PolicyStatus;
43 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Class to provide REST end points for PAP component to retrieve the status of deployed
50  * policies.
51  */
52 public class PolicyStatusControllerV1 extends PapRestControllerV1 {
53     private static final String GET_DEPLOYMENTS_FAILED = "get deployments failed";
54
55     private static final Logger logger = LoggerFactory.getLogger(PolicyStatusControllerV1.class);
56
57     private final PolicyStatusProvider provider = new PolicyStatusProvider();
58
59     /**
60      * Queries status of all deployed policies.
61      *
62      * @param requestId request ID used in ONAP logging
63      * @return a response
64      */
65     // @formatter:off
66     @GET
67     @Path("policies/deployed")
68     @ApiOperation(value = "Queries status of all deployed policies",
69         notes = "Queries status of all deployed policies, returning success and failure counts of the PDPs",
70         responseContainer = "List", response = PolicyStatus.class,
71         tags = {"Policy Administration (PAP) API"},
72         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
73         responseHeaders = {
74             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
75                             response = String.class),
76             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
77                             response = String.class),
78             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
79                             response = String.class),
80             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
81                             response = UUID.class)},
82         extensions = {@Extension(name = EXTENSION_NAME,
83             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
84                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
85     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
86                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
87                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
88     // @formatter:on
89
90     public Response queryAllDeployedPolicies(
91                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
92
93         try {
94             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
95                             .entity(provider.getStatus()).build();
96
97         } catch (PfModelException | PfModelRuntimeException e) {
98             logger.warn(GET_DEPLOYMENTS_FAILED, e);
99             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
100                 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
101         }
102     }
103
104
105     /**
106      * Queries status of specific deployed policies.
107      *
108      * @param requestId request ID used in ONAP logging
109      * @return a response
110      */
111     // @formatter:off
112     @GET
113     @Path("policies/deployed/{name}")
114     @ApiOperation(value = "Queries status of specific deployed policies",
115         notes = "Queries status of specific deployed policies, returning success and failure counts of the PDPs",
116         responseContainer = "List", response = PolicyStatus.class,
117         tags = {"Policy Administration (PAP) API"},
118         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
119         responseHeaders = {
120             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
121                             response = String.class),
122             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
123                             response = String.class),
124             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
125                             response = String.class),
126             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
127                             response = UUID.class)},
128         extensions = {@Extension(name = EXTENSION_NAME,
129             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
130                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
131     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
132                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
133                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
134     // @formatter:on
135
136     public Response queryDeployedPolicies(
137                     @ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
138                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
139
140         try {
141             Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, null));
142             if (result.isEmpty()) {
143                 return makeNotFoundResponse(requestId);
144
145             } else {
146                 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
147                                 .entity(result).build();
148             }
149
150         } catch (PfModelException | PfModelRuntimeException e) {
151             logger.warn(GET_DEPLOYMENTS_FAILED, e);
152             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
153                 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
154         }
155     }
156
157
158     /**
159      * Queries status of a specific deployed policy.
160      *
161      * @param requestId request ID used in ONAP logging
162      * @return a response
163      */
164     // @formatter:off
165     @GET
166     @Path("policies/deployed/{name}/{version}")
167     @ApiOperation(value = "Queries status of a specific deployed policy",
168         notes = "Queries status of a specific deployed policy, returning success and failure counts of the PDPs",
169         response = PolicyStatus.class,
170         tags = {"Policy Administration (PAP) API"},
171         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
172         responseHeaders = {
173             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
174                             response = String.class),
175             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
176                             response = String.class),
177             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
178                             response = String.class),
179             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
180                             response = UUID.class)},
181         extensions = {@Extension(name = EXTENSION_NAME,
182             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
183                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
184     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
185                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
186                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
187     // @formatter:on
188
189     public Response queryDeployedPolicy(@ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
190                     @ApiParam(value = "Policy Version", required = true) @PathParam("version") String version,
191                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
192
193         try {
194             Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, version));
195             if (result.isEmpty()) {
196                 return makeNotFoundResponse(requestId);
197
198             } else {
199                 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
200                                 .entity(result.iterator().next()).build();
201             }
202
203         } catch (PfModelException | PfModelRuntimeException e) {
204             logger.warn(GET_DEPLOYMENTS_FAILED, e);
205             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
206                 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
207         }
208     }
209
210
211     /**
212      * Queries status of all policies.
213      *
214      * @param requestId request ID used in ONAP logging
215      * @return a response
216      */
217     // @formatter:off
218     @GET
219     @Path("policies/status")
220     @ApiOperation(value = "Queries status of policies in all PdpGroups",
221         notes = "Queries status of policies in all PdpGroups, "
222             + "returning status of policies in all the PDPs belonging to all PdpGroups",
223         responseContainer = "List", response = PdpPolicyStatus.class,
224         tags = {"Policy Administration (PAP) API"},
225         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
226         responseHeaders = {
227             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
228                             response = String.class),
229             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
230                             response = String.class),
231             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
232                             response = String.class),
233             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
234                             response = UUID.class)},
235         extensions = {@Extension(name = EXTENSION_NAME,
236             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
237                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
238     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
239                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
240                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
241     // @formatter:on
242
243     public Response getStatusOfAllPolicies(
244                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
245
246         try {
247             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
248                             .entity(provider.getPolicyStatus()).build();
249
250         } catch (PfModelException | PfModelRuntimeException e) {
251             logger.warn(GET_DEPLOYMENTS_FAILED, e);
252             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
253                 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
254         }
255     }
256
257     /**
258      * Queries status of policies in a specific PdpGroup.
259      *
260      * @param pdpGroupName name of the PdpGroup
261      * @param requestId request ID used in ONAP logging
262      * @return a response
263      */
264     // @formatter:off
265     @GET
266     @Path("policies/status/{pdpGroupName}")
267     @ApiOperation(value = "Queries status of policies in a specific PdpGroup",
268         notes = "Queries status of policies in a specific PdpGroup, "
269             + "returning status of policies in all the PDPs belonging to the PdpGroup",
270         responseContainer = "List", response = PdpPolicyStatus.class,
271         tags = {"Policy Administration (PAP) API"},
272         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
273         responseHeaders = {
274             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
275                             response = String.class),
276             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
277                             response = String.class),
278             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
279                             response = String.class),
280             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
281                             response = UUID.class)},
282         extensions = {@Extension(name = EXTENSION_NAME,
283             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
284                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
285     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
286                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
287                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
288     // @formatter:on
289
290     public Response getStatusOfPoliciesByGroup(
291                     @ApiParam(value = "PDP Group Name", required = true) @PathParam("pdpGroupName") String pdpGroupName,
292                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
293
294         try {
295             Collection<PdpPolicyStatus> result = provider.getPolicyStatus(pdpGroupName);
296             if (result.isEmpty()) {
297                 return makeNotFoundResponse(requestId);
298
299             } else {
300                 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
301                                 .entity(result).build();
302             }
303
304         } catch (PfModelException | PfModelRuntimeException e) {
305             logger.warn(GET_DEPLOYMENTS_FAILED, e);
306             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
307                 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
308         }
309     }
310
311     /**
312      * Queries status of all versions of a specific policy in a specific PdpGroup.
313      *
314      * @param pdpGroupName name of the PdpGroup
315      * @param policyName name of the Policy
316      * @param requestId request ID used in ONAP logging
317      * @return a response
318      */
319     // @formatter:off
320     @GET
321     @Path("policies/status/{pdpGroupName}/{policyName}")
322     @ApiOperation(value = "Queries status of all versions of a specific policy in a specific PdpGroup",
323         notes = "Queries status of all versions of a specific policy in a specific PdpGroup,"
324             + " returning status of all versions of the policy in the PDPs belonging to the PdpGroup",
325         responseContainer = "List", response = PdpPolicyStatus.class,
326         tags = {"Policy Administration (PAP) API"},
327         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
328         responseHeaders = {
329             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
330                             response = String.class),
331             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
332                             response = String.class),
333             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
334                             response = String.class),
335             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
336                             response = UUID.class)},
337         extensions = {@Extension(name = EXTENSION_NAME,
338             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
339                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
340     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
341                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
342                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
343     // @formatter:on
344
345     public Response getStatusOfPolicies(
346         @ApiParam(value = "PDP Group Name", required = true) @PathParam("pdpGroupName") String pdpGroupName,
347         @ApiParam(value = "Policy Id", required = true) @PathParam("policyName") String policyName,
348         @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
349
350         try {
351             Collection<PdpPolicyStatus> result =
352                 provider.getPolicyStatus(pdpGroupName, new ToscaConceptIdentifierOptVersion(policyName, null));
353             if (result.isEmpty()) {
354                 return makeNotFoundResponse(requestId);
355
356             } else {
357                 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
358                                 .entity(result).build();
359             }
360
361         } catch (PfModelException | PfModelRuntimeException e) {
362             logger.warn(GET_DEPLOYMENTS_FAILED, e);
363             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
364                 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
365         }
366     }
367
368
369     /**
370      * Queries status of a specific version of a specific policy in a specific PdpGroup.
371      *
372      * @param pdpGroupName name of the PdpGroup
373      * @param policyName name of the Policy
374      * @param policyVersion version of the Policy
375      * @param requestId request ID used in ONAP logging
376      * @return a response
377      */
378     // @formatter:off
379     @GET
380     @Path("policies/status/{pdpGroupName}/{policyName}/{policyVersion}")
381     @ApiOperation(value = "Queries status of a specific version of a specific policy in a specific PdpGroup",
382         notes = "Queries status of a specific version of a specific policy in a specific PdpGroup,"
383             + " returning status of the policy in the PDPs belonging to the PdpGroup",
384         response = PdpPolicyStatus.class,
385         tags = {"Policy Administration (PAP) API"},
386         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
387         responseHeaders = {
388             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
389                             response = String.class),
390             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
391                             response = String.class),
392             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
393                             response = String.class),
394             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
395                             response = UUID.class)},
396         extensions = {@Extension(name = EXTENSION_NAME,
397             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
398                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
399     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
400                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
401                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
402     // @formatter:on
403
404     public Response getStatusOfPolicy(
405         @ApiParam(value = "PDP Group Name", required = true) @PathParam("pdpGroupName") String pdpGroupName,
406         @ApiParam(value = "Policy Id", required = true) @PathParam("policyName") String policyName,
407         @ApiParam(value = "Policy Version", required = true) @PathParam("policyVersion") String policyVersion,
408         @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
409
410         try {
411             Collection<PdpPolicyStatus> result = provider.getPolicyStatus(pdpGroupName,
412                 new ToscaConceptIdentifierOptVersion(policyName, policyVersion));
413             if (result.isEmpty()) {
414                 return makeNotFoundResponse(requestId);
415
416             } else {
417                 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
418                                 .entity(result.iterator().next()).build();
419             }
420
421         } catch (PfModelException | PfModelRuntimeException e) {
422             logger.warn(GET_DEPLOYMENTS_FAILED, e);
423             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
424                 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
425         }
426     }
427
428     /**
429      * Makes a "not found" response.
430      *
431      * @param requestId request ID
432      * @return a "not found" response
433      */
434     private Response makeNotFoundResponse(final UUID requestId) {
435         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.NOT_FOUND)), requestId)
436                         .build();
437     }
438 }