dbbb49e8f2f14fcf5f3fcb9432d02ad82f12cb6b
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / HealthCheckProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import org.onap.policy.common.endpoints.report.HealthCheckReport;
26 import org.onap.policy.common.utils.network.NetworkUtil;
27 import org.onap.policy.common.utils.services.Registry;
28 import org.onap.policy.pap.main.PapConstants;
29 import org.onap.policy.pap.main.startstop.PapActivator;
30 import org.springframework.stereotype.Service;
31
32 /**
33  * Class to fetch health check of PAP service.
34  *
35  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
36  */
37 @Service
38 public class HealthCheckProvider {
39
40     private static final String NOT_ALIVE = "not alive";
41     private static final String ALIVE = "alive";
42     private static final String URL = NetworkUtil.getHostname();
43     private static final String NAME = "Policy PAP";
44
45     /**
46      * Performs the health check of PAP service.
47      *
48      * @return Report containing health check status
49      */
50     public HealthCheckReport performHealthCheck() {
51         final var report = new HealthCheckReport();
52         report.setName(NAME);
53         report.setUrl(URL);
54
55         boolean alive = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive();
56
57         report.setHealthy(alive);
58         report.setCode(alive ? 200 : 500);
59         report.setMessage(alive ? ALIVE : NOT_ALIVE);
60         return report;
61     }
62 }