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