Migrate policy api startup & config, controller to springboot
[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  * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  * SPDX-License-Identifier: Apache-2.0
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.policy.api.main.rest.provider;
27
28 import org.onap.policy.api.main.rest.PolicyFetchMode;
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 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37
38 /**
39  * Class to fetch health check of api service.
40  *
41  */
42 @Service
43 public class HealthCheckProvider {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheckProvider.class);
46
47     private static final String ALIVE = "alive";
48     private static final String URL = NetworkUtil.getHostname();
49     private static final String NAME = "Policy API";
50     private static final String DB_CONN_FAILURE = "unable to connect with database";
51
52     @Autowired
53     private PolicyProvider policyProvider;
54
55     /**
56      * Performs the health check of api service.
57      *
58      * @return Report containing health check status
59      */
60     public HealthCheckReport performHealthCheck() {
61         final var report = new HealthCheckReport();
62         final var dbConnectionStatus = verifyApiDatabase();
63         report.setName(NAME);
64         report.setUrl(URL);
65         report.setHealthy(dbConnectionStatus);
66         report.setCode(dbConnectionStatus ? 200 : 503);
67         report.setMessage(dbConnectionStatus ? ALIVE : DB_CONN_FAILURE);
68         return report;
69     }
70
71     /**
72      * Verifies the connectivity between api component & policy database.
73      *
74      * @return boolean signaling the verification result
75      */
76     private boolean verifyApiDatabase() {
77         try {
78             policyProvider.fetchPolicies(null, null, null, null, PolicyFetchMode.BARE);
79             return true;
80         } catch (PfModelException | PfModelRuntimeException pfme) {
81             LOGGER.warn("Api to database connection check failed. Details - ", pfme);
82             return false;
83         }
84     }
85 }