Add check to verify api to db connectivity
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / HealthCheckProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6  * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
7  * Modifications Copyright (C) 2019 Nordix Foundation.
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  *
21  * SPDX-License-Identifier: Apache-2.0
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.policy.api.main.rest.provider;
26
27 import org.onap.policy.api.main.rest.PolicyFetchMode;
28 import org.onap.policy.api.main.startstop.ApiActivator;
29 import org.onap.policy.common.endpoints.report.HealthCheckReport;
30 import org.onap.policy.common.utils.network.NetworkUtil;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Class to fetch health check of api service.
38  *
39  */
40 public class HealthCheckProvider {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheckProvider.class);
43
44     private static final String NOT_ALIVE = "not alive";
45     private static final String ALIVE = "alive";
46     private static final String URL = NetworkUtil.getHostname();
47     private static final String NAME = "Policy API";
48     private static final String DB_CONN_FAILURE = "unable to connect with database";
49
50     /**
51      * Performs the health check of api service.
52      *
53      * @return Report containing health check status
54      */
55     public HealthCheckReport performHealthCheck() {
56         final var report = new HealthCheckReport();
57         report.setName(NAME);
58         report.setUrl(URL);
59         boolean heathStatus = ApiActivator.isAlive();
60         if (heathStatus) {
61             boolean dbConnectionStatus = verifyApiDatabase();
62             report.setHealthy(dbConnectionStatus);
63             report.setCode(dbConnectionStatus ? 200 : 503);
64             report.setMessage(dbConnectionStatus ? ALIVE : DB_CONN_FAILURE);
65         } else {
66             report.setHealthy(heathStatus);
67             report.setCode(500);
68             report.setMessage(NOT_ALIVE);
69         }
70         return report;
71     }
72
73     /**
74      * Verifies the connectivity between api component & policy database.
75      *
76      * @return boolean signaling the verification result
77      */
78     private boolean verifyApiDatabase() {
79         try (var policyProvider = new PolicyProvider()) {
80             policyProvider.fetchPolicies(null, null, null, null, PolicyFetchMode.BARE);
81             return true;
82         } catch (PfModelException | PfModelRuntimeException pfme) {
83             LOGGER.warn("Api to database connection check failed. Details - ", pfme);
84             return false;
85         }
86     }
87 }