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