Update pap for checkstyle 8.43
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupHealthCheckControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019,2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest;
22
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiParam;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27 import io.swagger.annotations.Authorization;
28 import io.swagger.annotations.Extension;
29 import io.swagger.annotations.ExtensionProperty;
30 import io.swagger.annotations.ResponseHeader;
31 import java.util.UUID;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.HeaderParam;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.Status;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.pdp.concepts.Pdps;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Class to provide REST end point for PAP component to fetch health status of all PDPs registered with PAP.
45  *
46  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
47  */
48 public class PdpGroupHealthCheckControllerV1 extends PapRestControllerV1 {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(PdpGroupHealthCheckControllerV1.class);
51
52     private final PdpGroupHealthCheckProvider provider = new PdpGroupHealthCheckProvider();
53
54     /**
55      * Returns health status of all PDPs registered with PAP.
56      *
57      * @param requestId request ID used in ONAP logging
58      * @return a response
59      */
60     // @formatter:off
61     @GET
62     @Path("pdps/healthcheck")
63     @ApiOperation(value = "Returns health status of all PDPs registered with PAP",
64         notes = "Queries health status of all PDPs, returning all pdps health status",
65         response = Pdps.class,
66         tags = {"Policy Administration (PAP) API"},
67         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
68         responseHeaders = {
69             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
70                             response = String.class),
71             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
72                             response = String.class),
73             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
74                             response = String.class),
75             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
76                             response = UUID.class)},
77         extensions = {
78             @Extension(name = EXTENSION_NAME,
79                 properties = {
80                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
81                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
82                 })
83             })
84     @ApiResponses(value = {
85         @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     })
89     // @formatter:on
90
91     public Response pdpGroupHealthCheck(
92             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
93
94         try {
95             final Pair<Status, Pdps> pair = provider.fetchPdpGroupHealthStatus();
96             return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
97                     .entity(pair.getRight()).build();
98         } catch (final PfModelException exp) {
99             LOGGER.info("pdpGroup health check failed", exp);
100             return addLoggingHeaders(
101                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
102                             .entity(exp.getErrorResponse()).build();
103         }
104     }
105 }