8dcc07ecb059c467d829a248ef86f64a1011e306
[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  *  Modifications Copyright (C) 2020 AT&T Intellectual Property.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
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.Map;
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.core.Response;
38 import javax.ws.rs.core.Response.Status;
39 import org.apache.commons.lang3.tuple.Pair;
40 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
41
42 /**
43  * Class to provide REST end point for PAP component to fetch all policy components, including PAP,
44  * API, Distribution, and PDPs.
45  *
46  * @author Yehui Wang (yehui.wang@est.tech)
47  */
48 public class PolicyComponentsHealthCheckControllerV1 extends PapRestControllerV1 {
49
50     private PolicyComponentsHealthCheckProvider provider;
51
52     /**
53      * Constructs the object.
54      *
55      * @throws HttpClientConfigException if creating http client failed
56      */
57     public PolicyComponentsHealthCheckControllerV1() throws HttpClientConfigException {
58         provider = new PolicyComponentsHealthCheckProvider();
59     }
60
61     /**
62      * Returns health status of all Policy components, including PAP, API, Distribution, and PDPs.
63      *
64      * @param requestId request ID used in ONAP logging
65      * @return a response
66      */
67     // @formatter:off
68     @GET
69     @Path("components/healthcheck")
70     @ApiOperation(value = "Returns health status of all policy components, including PAP, API, Distribution, and PDPs",
71         notes = "Queries health status of all policy components, returning all policy components health status",
72         response = Map.class,
73         tags = {"Policy Administration (PAP) API"},
74         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
75         responseHeaders = {
76             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
77                             response = String.class),
78             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
79                             response = String.class),
80             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
81                             response = String.class),
82             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
83                             response = UUID.class)},
84         extensions = {@Extension(name = EXTENSION_NAME,
85             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
86                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
87     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
88                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
89                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
90     // @formatter:on
91
92     public Response policyComponentsHealthCheck(
93             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
94         final Pair<Status, Map<String, Object>> pair =
95                 provider.fetchPolicyComponentsHealthStatus();
96         return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
97                 .entity(pair.getRight()).build();
98     }
99 }