consolidate Policy Health Check into PAP
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PolicyComponentsHealthCheckControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.Map;
32 import java.util.UUID;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.HeaderParam;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.Response.Status;
38 import org.apache.commons.lang3.tuple.Pair;
39
40 /**
41  * Class to provide REST end point for PAP component to fetch all policy components, including PAP,
42  * API, Distribution, and PDPs
43  *
44  * @author Yehui Wang (yehui.wang@est.tech)
45  */
46 public class PolicyComponentsHealthCheckControllerV1 extends PapRestControllerV1 {
47
48     /**
49      * Returns health status of all Policy components, including PAP, API, Distribution, and PDPs.
50      * Note: a new provider {@link PolicyComponentsHealthCheckProvider} must be created for each
51      * request.
52      *
53      * @param requestId request ID used in ONAP logging
54      * @return a response
55      */
56     // @formatter:off
57     @GET
58     @Path("components/healthcheck")
59     @ApiOperation(value = "Returns health status of all policy components, including PAP, API, Distribution, and PDPs",
60         notes = "Queries health status of all policy components, returning all policy components health status",
61         response = Map.class,
62         tags = {"Policy Administration (PAP) API"},
63         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
64         responseHeaders = {
65             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
66                             response = String.class),
67             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
68                             response = String.class),
69             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
70                             response = String.class),
71             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
72                             response = UUID.class)},
73         extensions = {@Extension(name = EXTENSION_NAME,
74             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
75                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
76     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
77                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
78                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
79     // @formatter:on
80
81     public Response policyComponentsHealthCheck(
82             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
83         final Pair<Status, Map<String, Object>> pair =
84                 new PolicyComponentsHealthCheckProvider().fetchPolicyComponentsHealthStatus();
85         return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
86                 .entity(pair.getRight()).build();
87     }
88 }