Release version 1.13.7
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / healthcheck-rest / healthcheck-rest-services / src / main / java / org / openecomp / sdcrests / health / rest / services / HealthCheckImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdcrests.health.rest.services;
21
22 import java.util.Arrays;
23 import java.util.Collection;
24 import javax.inject.Named;
25 import javax.ws.rs.core.Response;
26 import org.apache.cxf.jaxrs.impl.ResponseBuilderImpl;
27 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
28 import org.openecomp.sdc.health.HealthCheckManager;
29 import org.openecomp.sdc.health.HealthCheckManagerFactory;
30 import org.openecomp.sdc.health.data.HealthCheckResult;
31 import org.openecomp.sdc.health.data.HealthCheckStatus;
32 import org.openecomp.sdc.health.data.HealthInfo;
33 import org.openecomp.sdc.logging.api.Logger;
34 import org.openecomp.sdc.logging.api.LoggerFactory;
35 import org.springframework.context.annotation.Scope;
36 import org.springframework.stereotype.Service;
37
38 @Named
39 @Service("healthCheck")
40 @Scope(value = "prototype")
41 public class HealthCheckImpl implements org.openecomp.sdcrests.health.rest.HealthCheck {
42
43     private static final Logger logger = LoggerFactory.getLogger(HealthCheckImpl.class);
44     private HealthCheckManager healthCheckManager;
45
46     public HealthCheckImpl() {
47         try {
48             healthCheckManager = HealthCheckManagerFactory.getInstance().createInterface();
49         } catch (Exception e) {
50             logger.error(e.getMessage(), e);
51         }
52     }
53
54     @Override
55     public Response checkHealth() {
56         HealthCheckResult healthCheckResult = new HealthCheckResult();
57         SessionContextProviderFactory.getInstance().createInterface().create("public", "dox");
58         try {
59             Collection<HealthInfo> healthInfos = healthCheckManager.checkHealth();
60             healthCheckResult.setComponentsInfo(healthInfos);
61             boolean someIsDown = healthInfos.stream().anyMatch(healthInfo -> healthInfo.getHealthCheckStatus().equals(HealthCheckStatus.DOWN));
62             healthInfos.stream().filter(healthInfo -> healthInfo.getHealthCheckComponent().equals(org.openecomp.sdc.health.data.MonitoredModules.BE))
63                 .findFirst().ifPresent(healthInfo -> healthCheckResult.setSdcVersion(healthInfo.getVersion()));
64             if (someIsDown) {
65                 Response.ResponseBuilder responseBuilder = new ResponseBuilderImpl();
66                 return responseBuilder.entity(healthCheckResult).status(500).build();
67             }
68             return Response.ok(healthCheckResult).build();
69         } catch (Exception ex) {
70             logger.error("Health check failed", ex);
71             Response.ResponseBuilder responseBuilder = new ResponseBuilderImpl();
72             HealthInfo healthInfo = new HealthInfo(org.openecomp.sdc.health.data.MonitoredModules.BE, HealthCheckStatus.DOWN, "",
73                 "Failed to perform Health Check");
74             Collection<HealthInfo> healthInfos = Arrays.asList(healthInfo);
75             healthCheckResult.setComponentsInfo(healthInfos);
76             return responseBuilder.entity(healthCheckResult).status(500).build();
77         }
78     }
79 }