2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.health.rest.services;
23 import org.apache.cxf.jaxrs.impl.ResponseBuilderImpl;
24 import org.openecomp.sdc.health.HealthCheckManager;
25 import org.openecomp.sdc.health.HealthCheckManagerFactory;
26 import org.openecomp.sdc.health.data.HealthCheckResult;
27 import org.openecomp.sdc.health.data.HealthCheckStatus;
28 import org.openecomp.sdc.health.data.HealthInfo;
29 import org.openecomp.sdc.health.data.SiteMode;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.logging.context.MdcUtil;
33 import org.openecomp.sdc.logging.types.LoggerServiceName;
34 import org.openecomp.sdcrests.health.types.HealthInfoDtos;
35 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
36 import org.springframework.context.annotation.Scope;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Named;
40 import javax.ws.rs.core.Response;
41 import java.util.Arrays;
42 import java.util.Collection;
45 @Service("healthCheck")
46 @Scope(value = "prototype")
47 public class HealthCheckImpl implements org.openecomp.sdcrests.health.rest.HealthCheck {
49 private HealthCheckManager healthCheckManager;
50 private static final Logger logger = LoggerFactory.getLogger(HealthCheckImpl.class);
52 public HealthCheckImpl() {
54 healthCheckManager = HealthCheckManagerFactory.getInstance().createInterface();
55 } catch (Exception e){
56 logger.error(e.getMessage(),e);
61 public Response checkHealth() {
62 HealthCheckResult healthCheckResult = new HealthCheckResult();
65 MdcUtil.initMdc(LoggerServiceName.Health_check.toString());
66 Collection<HealthInfo> healthInfos = healthCheckManager.checkHealth();
67 healthCheckResult.setComponentsInfo(healthInfos);
68 boolean someIsDown = healthInfos.stream()
69 .anyMatch(healthInfo -> healthInfo.getHealthCheckStatus().equals(HealthCheckStatus.DOWN));
71 filter(healthInfo -> healthInfo.getHealthCheckComponent()
72 .equals(org.openecomp.sdc.health.data.MonitoredModules.BE)).
73 findFirst().ifPresent(healthInfo -> healthCheckResult.setSdcVersion(healthInfo.getVersion()));
75 Response.ResponseBuilder responseBuilder = new ResponseBuilderImpl();
76 return responseBuilder.entity(healthCheckResult).status(500).build();
78 return Response.ok(healthCheckResult).build();
79 } catch (Exception ex) {
80 logger.error("Health check failed", ex);
81 Response.ResponseBuilder responseBuilder = new ResponseBuilderImpl();
82 GenericCollectionWrapper<HealthInfoDtos> results = new GenericCollectionWrapper<>();
83 HealthInfo healthInfo = new HealthInfo(org.openecomp.sdc.health.data.MonitoredModules.BE ,
84 HealthCheckStatus.DOWN,
85 "", "Failed to perform Health Check");
86 Collection<HealthInfo> healthInfos = Arrays.asList(healthInfo);
87 healthCheckResult.setComponentsInfo(healthInfos);
88 return responseBuilder.entity(healthCheckResult).status(500).build();