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