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.sdc.be.components.health;
23 import com.fasterxml.jackson.core.type.TypeReference;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.common.annotations.VisibleForTesting;
26 import org.apache.commons.lang3.tuple.ImmutablePair;
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.openecomp.sdc.be.catalog.impl.DmaapProducerHealth;
29 import org.openecomp.sdc.be.components.distribution.engine.DistributionEngineClusterHealth;
30 import org.openecomp.sdc.be.components.distribution.engine.DmaapHealth;
31 import org.openecomp.sdc.be.components.impl.CADIHealthCheck;
32 import org.openecomp.sdc.be.components.impl.CassandraHealthCheck;
33 import org.openecomp.sdc.be.config.BeEcompErrorManager;
34 import org.openecomp.sdc.be.config.Configuration;
35 import org.openecomp.sdc.be.config.ConfigurationManager;
36 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
37 import org.openecomp.sdc.be.switchover.detector.SwitchoverDetector;
38 import org.openecomp.sdc.common.api.HealthCheckInfo;
39 import org.openecomp.sdc.common.api.HealthCheckInfo.HealthCheckStatus;
40 import org.openecomp.sdc.common.http.client.api.HttpRequest;
41 import org.openecomp.sdc.common.http.client.api.HttpResponse;
42 import org.openecomp.sdc.common.http.config.HttpClientConfig;
43 import org.openecomp.sdc.common.http.config.Timeouts;
44 import org.openecomp.sdc.common.log.elements.LogFieldsMdcHandler;
45 import org.openecomp.sdc.common.log.wrappers.Logger;
46 import org.openecomp.sdc.common.util.HealthCheckUtil;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.stereotype.Component;
50 import javax.annotation.PostConstruct;
51 import javax.annotation.PreDestroy;
52 import javax.annotation.Resource;
53 import java.util.ArrayList;
54 import java.util.HashMap;
55 import java.util.List;
57 import java.util.Map.Entry;
58 import java.util.concurrent.ScheduledExecutorService;
59 import java.util.concurrent.ScheduledFuture;
60 import java.util.concurrent.TimeUnit;
61 import java.util.stream.Collectors;
63 import static java.lang.String.format;
64 import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
65 import static org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR;
66 import static org.apache.http.HttpStatus.SC_OK;
67 import static org.openecomp.sdc.common.api.Constants.HC_COMPONENT_BE;
68 import static org.openecomp.sdc.common.api.Constants.HC_COMPONENT_CASSANDRA;
69 import static org.openecomp.sdc.common.api.Constants.HC_COMPONENT_DCAE;
70 import static org.openecomp.sdc.common.api.Constants.HC_COMPONENT_DMAAP_PRODUCER;
71 import static org.openecomp.sdc.common.api.Constants.HC_COMPONENT_ECOMP_PORTAL;
72 import static org.openecomp.sdc.common.api.Constants.HC_COMPONENT_JANUSGRAPH;
73 import static org.openecomp.sdc.common.api.Constants.HC_COMPONENT_ON_BOARDING;
74 import static org.openecomp.sdc.common.api.HealthCheckInfo.HealthCheckStatus.DOWN;
75 import static org.openecomp.sdc.common.api.HealthCheckInfo.HealthCheckStatus.UP;
76 import static org.openecomp.sdc.common.impl.ExternalConfiguration.getAppVersion;
79 @Component("healthCheckBusinessLogic")
80 public class HealthCheckBusinessLogic {
82 private static String hcUrl = "%s://%s:%s%s";
83 private static final String BE_HEALTH_CHECK_STR = "beHealthCheck";
84 private static final String LOG_PARTNER_NAME = "SDC.BE";
85 private static final String COMPONENT_CHANGED_MESSAGE = "BE Component %s state changed from %s to %s";
86 private static final Logger log = Logger.getLogger(HealthCheckBusinessLogic.class.getName());
87 private static final HealthCheckUtil healthCheckUtil = new HealthCheckUtil();
88 private final ScheduledExecutorService healthCheckScheduler = newSingleThreadScheduledExecutor((Runnable r) -> new Thread(r, "BE-Health-Check-Task"));
89 private HealthCheckScheduledTask healthCheckScheduledTask = null;
90 private static LogFieldsMdcHandler mdcFieldsHandler = new LogFieldsMdcHandler();
93 private JanusGraphGenericDao janusGraphGenericDao;
95 private DistributionEngineClusterHealth distributionEngineClusterHealth;
97 private DmaapHealth dmaapHealth;
99 private DmaapProducerHealth dmaapProducerHealth;
101 private CassandraHealthCheck cassandraHealthCheck;
103 private PortalHealthCheckBuilder portalHealthCheck;
106 private SwitchoverDetector switchoverDetector;
107 private volatile List<HealthCheckInfo> prevBeHealthCheckInfos = null;
108 private ScheduledFuture<?> scheduledFuture = null;
113 prevBeHealthCheckInfos = getBeHealthCheckInfos();
115 log.debug("After initializing prevBeHealthCheckInfos: {}", prevBeHealthCheckInfos);
117 healthCheckScheduledTask = new HealthCheckScheduledTask();
119 if (this.scheduledFuture == null) {
120 this.scheduledFuture = this.healthCheckScheduler.scheduleAtFixedRate(healthCheckScheduledTask, 0, 3, TimeUnit.SECONDS);
125 public boolean isDistributionEngineUp() {
127 HealthCheckInfo healthCheckInfo = distributionEngineClusterHealth.getHealthCheckInfo();
128 return healthCheckInfo.getHealthCheckStatus() != DOWN;
131 public Pair<Boolean, List<HealthCheckInfo>> getBeHealthCheckInfosStatus() {
132 Configuration config = ConfigurationManager.getConfigurationManager().getConfiguration();
133 return new ImmutablePair<>(healthCheckUtil.getAggregateStatus(prevBeHealthCheckInfos, config.getHealthStatusExclude()), prevBeHealthCheckInfos);
136 private List<HealthCheckInfo> getBeHealthCheckInfos() {
138 log.trace("In getBeHealthCheckInfos");
140 List<HealthCheckInfo> healthCheckInfos = new ArrayList<>();
143 HealthCheckInfo info;
144 if ((info = getDmaapHealthCheck()) != null) {
145 healthCheckInfos.add(info);
149 healthCheckInfos.add(getDmaapProducerHealthCheck());
152 healthCheckInfos.add(new HealthCheckInfo(HC_COMPONENT_BE, UP, getAppVersion(), "OK"));
155 healthCheckInfos.add(getJanusGraphHealthCheck());
157 // Distribution Engine
158 healthCheckInfos.add(distributionEngineClusterHealth.getHealthCheckInfo());
161 healthCheckInfos.add(getCassandraHealthCheck());
164 healthCheckInfos.add(getHostedComponentsBeHealthCheck(HC_COMPONENT_ON_BOARDING, buildOnBoardingHealthCheckUrl()));
167 healthCheckInfos.add(getHostedComponentsBeHealthCheck(HC_COMPONENT_DCAE, buildDcaeHealthCheckUrl()));
170 healthCheckInfos.add(portalHealthCheck.getHealthCheckInfo());
173 healthCheckInfos.add(CADIHealthCheck.getCADIHealthCheckInstance().getCADIStatus());
175 return healthCheckInfos;
178 private HealthCheckInfo getDmaapHealthCheck() {
179 HealthCheckInfo healthCheckInfo = null;
180 if(ConfigurationManager.getConfigurationManager().getConfiguration().getDmaapConsumerConfiguration().isActive()){
181 String appVersion = getAppVersion();
182 dmaapHealth.getHealthCheckInfo().setVersion(appVersion);
183 healthCheckInfo = dmaapHealth.getHealthCheckInfo();
185 log.debug("Dmaap health check disabled");
187 return healthCheckInfo;
190 private HealthCheckInfo getDmaapProducerHealthCheck() {
191 if (ConfigurationManager.getConfigurationManager().getConfiguration().getDmaapConsumerConfiguration().isActive()) {
192 String appVersion = getAppVersion();
193 dmaapProducerHealth.getHealthCheckInfo().setVersion(appVersion);
194 return dmaapProducerHealth.getHealthCheckInfo();
196 log.debug("Dmaap health check disabled");
197 String description = ("Dmaap health check disabled");
198 return new HealthCheckInfo(HC_COMPONENT_DMAAP_PRODUCER, DOWN, null, description);
202 public HealthCheckInfo getJanusGraphHealthCheck() {
203 // JanusGraph health check and version
205 boolean isJanusGraphUp;
206 HealthCheckInfo healthCheckInfo = new HealthCheckInfo(HC_COMPONENT_JANUSGRAPH, DOWN, null, null);
209 isJanusGraphUp = janusGraphGenericDao.isGraphOpen();
210 } catch (Exception e) {
211 description = "JanusGraph error: " + e.getMessage();
212 healthCheckInfo.setDescription(description);
213 log.error(description);
214 return healthCheckInfo;
216 if (isJanusGraphUp) {
218 healthCheckInfo.setDescription(description);
219 healthCheckInfo.setHealthCheckStatus(HealthCheckInfo.HealthCheckStatus.UP);
222 description = "JanusGraph graph is down";
223 healthCheckInfo.setDescription(description);
225 return healthCheckInfo;
228 private HealthCheckInfo getCassandraHealthCheck() {
231 boolean isCassandraUp = false;
232 HealthCheckInfo healthCheckInfo = new HealthCheckInfo(HC_COMPONENT_CASSANDRA, DOWN, null, null);
235 isCassandraUp = cassandraHealthCheck.getCassandraStatus();
236 } catch (Exception e) {
237 description = "Cassandra error: " + e.getMessage();
238 log.error(description, e);
242 // healthCheckInfos.add(new HealthCheckInfo(HC_COMPONENT_CASSANDRA, UP, null, description));
243 healthCheckInfo.setHealthCheckStatus(HealthCheckStatus.UP);
244 healthCheckInfo.setDescription(description);
246 description = "Cassandra is down";
247 // healthCheckInfos.add(new HealthCheckInfo(HC_COMPONENT_CASSANDRA, DOWN, null, description));
248 healthCheckInfo.setDescription(description);
250 return healthCheckInfo;
253 private HealthCheckInfo getHostedComponentsBeHealthCheck(String componentName, String healthCheckUrl) {
254 HealthCheckStatus healthCheckStatus;
256 String version = null;
257 List<HealthCheckInfo> componentsInfo = new ArrayList<>();
258 final int timeout = 3000;
260 if (healthCheckUrl != null) {
262 HttpResponse<String> httpResponse = HttpRequest.get(healthCheckUrl, new HttpClientConfig(new Timeouts(timeout, timeout)));
263 int statusCode = httpResponse.getStatusCode();
264 String aggDescription = "";
266 if ((statusCode == SC_OK || statusCode == SC_INTERNAL_SERVER_ERROR) && !componentName.equals(HC_COMPONENT_ECOMP_PORTAL)) {
267 String response = httpResponse.getResponse();
268 log.trace("{} Health Check response: {}", componentName, response);
269 ObjectMapper mapper = new ObjectMapper();
270 Map<String, Object> healthCheckMap = mapper.readValue(response, new TypeReference<Map<String, Object>>() {
272 version = getVersion(healthCheckMap);
273 if (healthCheckMap.containsKey("componentsInfo")) {
274 componentsInfo = mapper.convertValue(healthCheckMap.get("componentsInfo"), new TypeReference<List<HealthCheckInfo>>() {
277 aggDescription = getAggDescription(componentsInfo, aggDescription);
279 log.trace("{} Health Check Response code: {}", componentName, statusCode);
282 if (statusCode != SC_OK) {
283 healthCheckStatus = DOWN;
284 description = getDescription(componentName, aggDescription);
285 setDescriptionToObject(description, componentsInfo);
287 healthCheckStatus = UP;
291 } catch (Exception e) {
292 log.error("{} unexpected response: ", componentName, e);
293 healthCheckStatus = DOWN;
294 description = componentName + " unexpected response: " + e.getMessage();
295 addToHealthCheckInfoObject(description, componentsInfo);
298 healthCheckStatus = DOWN;
299 description = componentName + " health check Configuration is missing";
300 componentsInfo.add(new HealthCheckInfo(HC_COMPONENT_BE, DOWN, null, description));
302 return new HealthCheckInfo(componentName, healthCheckStatus, version, description, componentsInfo);
305 private void addToHealthCheckInfoObject(String description, List<HealthCheckInfo> componentsInfo) {
306 if (componentsInfo != null && componentsInfo.isEmpty()) {
307 componentsInfo.add(new HealthCheckInfo(HC_COMPONENT_BE, DOWN, null, description));
311 private void setDescriptionToObject(String description, List<HealthCheckInfo> componentsInfo) {
312 if (componentsInfo.isEmpty()) {
313 componentsInfo.add(new HealthCheckInfo(HC_COMPONENT_BE, DOWN, null, description));
317 private String getDescription(String componentName, String aggDescription) {
319 description = aggDescription.length() > 0
321 : componentName + " is Down, specific reason unknown";//No inner component returned DOWN, but the status of HC is still DOWN.
325 private String getVersion(Map<String, Object> healthCheckMap) {
326 return healthCheckMap.get("sdcVersion") != null ? healthCheckMap.get("sdcVersion").toString() : null;
329 private String getAggDescription(List<HealthCheckInfo> componentsInfo, String aggDescription) {
330 if (!componentsInfo.isEmpty()) {
331 aggDescription = healthCheckUtil.getAggregateDescription(componentsInfo);
333 componentsInfo.add(new HealthCheckInfo(HC_COMPONENT_BE, DOWN, null, null));
335 return aggDescription;
339 protected void destroy() {
341 if (scheduledFuture != null) {
342 scheduledFuture.cancel(true);
343 scheduledFuture = null;
346 if (healthCheckScheduler != null) {
347 healthCheckScheduler.shutdown();
352 private void logAlarm(String componentChangedMsg) {
353 BeEcompErrorManager.getInstance().logBeHealthCheckRecovery(componentChangedMsg);
356 public String getSiteMode() {
357 return switchoverDetector.getSiteMode();
360 public boolean anyStatusChanged(List<HealthCheckInfo> beHealthCheckInfos, List<HealthCheckInfo> prevBeHealthCheckInfos) {
362 boolean result = false;
364 if (beHealthCheckInfos != null && prevBeHealthCheckInfos != null) {
366 Map<String, HealthCheckStatus> currentValues = beHealthCheckInfos.stream().collect(Collectors.toMap(HealthCheckInfo::getHealthCheckComponent, HealthCheckInfo::getHealthCheckStatus));
367 Map<String, HealthCheckStatus> prevValues = prevBeHealthCheckInfos.stream().collect(Collectors.toMap(HealthCheckInfo::getHealthCheckComponent, HealthCheckInfo::getHealthCheckStatus));
369 if (currentValues != null && prevValues != null) {
370 int currentSize = currentValues.size();
371 int prevSize = prevValues.size();
373 if (currentSize != prevSize) {
374 result = true; //extra/missing component
375 updateHealthCheckStatusMap(currentValues, prevValues);
377 result = isHealthStatusChanged(result, currentValues, prevValues);
381 } else if (beHealthCheckInfos == null && prevBeHealthCheckInfos == null) {
384 writeLogAlarm(prevBeHealthCheckInfos);
391 private void writeLogAlarm(List<HealthCheckInfo> prevBeHealthCheckInfos) {
392 logAlarm(format(COMPONENT_CHANGED_MESSAGE, "", prevBeHealthCheckInfos == null ? "null" : "true", prevBeHealthCheckInfos == null ? "true" : "null"));
395 private boolean isHealthStatusChanged(boolean result, Map<String, HealthCheckStatus> currentValues, Map<String, HealthCheckStatus> prevValues) {
396 for (Entry<String, HealthCheckStatus> entry : currentValues.entrySet()) {
397 String key = entry.getKey();
398 HealthCheckStatus value = entry.getValue();
400 if (!prevValues.containsKey(key)) {
401 result = true; //component missing
402 logAlarm(format(COMPONENT_CHANGED_MESSAGE, key, prevValues.get(key), currentValues.get(key)));
406 HealthCheckStatus prevHealthCheckStatus = prevValues.get(key);
408 if (value != prevHealthCheckStatus) {
409 result = true; //component status changed
410 logAlarm(format(COMPONENT_CHANGED_MESSAGE, key, prevValues.get(key), currentValues.get(key)));
417 private void updateHealthCheckStatusMap(Map<String, HealthCheckStatus> currentValues, Map<String, HealthCheckStatus> prevValues) {
418 Map<String, HealthCheckStatus> notPresent;
419 if (currentValues.keySet().containsAll(prevValues.keySet())) {
420 notPresent = new HashMap<>(currentValues);
421 notPresent.keySet().removeAll(prevValues.keySet());
423 notPresent = new HashMap<>(prevValues);
424 notPresent.keySet().removeAll(currentValues.keySet());
427 for (String component : notPresent.keySet()) {
428 logAlarm(format(COMPONENT_CHANGED_MESSAGE, component, prevValues.get(component), currentValues.get(component)));
432 String buildOnBoardingHealthCheckUrl() {
434 Configuration.OnboardingConfig onboardingConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getOnboarding();
435 if (onboardingConfig != null) {
436 return String.format(hcUrl, onboardingConfig.getProtocol(), onboardingConfig.getHost(),
437 onboardingConfig.getPort(),onboardingConfig.getHealthCheckUri());
439 log.error("Onboarding health check configuration is missing.");
444 String buildDcaeHealthCheckUrl() {
446 Configuration.DcaeConfig dcaeConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getDcae();
448 if (dcaeConfig != null) {
449 return String.format(hcUrl, dcaeConfig.getProtocol(), dcaeConfig.getHost(),
450 dcaeConfig.getPort(),dcaeConfig.getHealthCheckUri());
453 log.error("DCAE health check configuration is missing.");
457 public class HealthCheckScheduledTask implements Runnable {
460 mdcFieldsHandler.addInfoForErrorAndDebugLogging(LOG_PARTNER_NAME);
461 Configuration config = ConfigurationManager.getConfigurationManager().getConfiguration();
462 log.trace("Executing BE Health Check Task");
464 List<HealthCheckInfo> currentBeHealthCheckInfos = getBeHealthCheckInfos();
465 boolean healthStatus = healthCheckUtil.getAggregateStatus(currentBeHealthCheckInfos, config.getHealthStatusExclude());
467 boolean prevHealthStatus = healthCheckUtil.getAggregateStatus(prevBeHealthCheckInfos, config.getHealthStatusExclude());
469 boolean anyStatusChanged = anyStatusChanged(currentBeHealthCheckInfos, prevBeHealthCheckInfos);
471 if (prevHealthStatus != healthStatus || anyStatusChanged) {
472 log.trace("BE Health State Changed to {}. Issuing alarm / recovery alarm...", healthStatus);
474 prevBeHealthCheckInfos = currentBeHealthCheckInfos;
475 logAlarm(healthStatus);
479 private void logAlarm(boolean prevHealthState) {
480 if (prevHealthState) {
481 BeEcompErrorManager.getInstance().logBeHealthCheckRecovery(BE_HEALTH_CHECK_STR);
483 BeEcompErrorManager.getInstance().logBeHealthCheckError(BE_HEALTH_CHECK_STR);