2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.fe.impl;
23 import com.fasterxml.jackson.core.type.TypeReference;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.common.annotations.VisibleForTesting;
26 import com.google.common.collect.Lists;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.JsonSyntaxException;
30 import com.google.gson.reflect.TypeToken;
31 import org.apache.commons.collections.CollectionUtils;
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.http.HttpStatus;
34 import org.openecomp.sdc.common.api.Constants;
35 import org.openecomp.sdc.common.api.HealthCheckInfo;
36 import org.openecomp.sdc.common.api.HealthCheckWrapper;
37 import org.openecomp.sdc.common.config.EcompErrorEnum;
38 import org.openecomp.sdc.common.http.client.api.HttpRequest;
39 import org.openecomp.sdc.common.http.client.api.HttpResponse;
40 import org.openecomp.sdc.common.http.config.HttpClientConfig;
41 import org.openecomp.sdc.common.http.config.Timeouts;
42 import org.openecomp.sdc.common.impl.ExternalConfiguration;
43 import org.openecomp.sdc.common.log.elements.ErrorLogOptionalData;
44 import org.openecomp.sdc.common.log.elements.LogFieldsMdcHandler;
45 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
46 import org.openecomp.sdc.common.log.wrappers.Logger;
47 import org.openecomp.sdc.common.util.HealthCheckUtil;
48 import org.openecomp.sdc.fe.config.Configuration;
49 import org.openecomp.sdc.fe.config.FeEcompErrorManager;
51 import java.io.IOException;
52 import java.lang.reflect.Type;
53 import java.util.ArrayList;
54 import java.util.Arrays;
55 import java.util.Collections;
56 import java.util.List;
59 import static org.openecomp.sdc.common.api.Constants.HC_COMPONENT_CATALOG_FACADE_MS;
60 import static org.openecomp.sdc.common.api.Constants.HC_COMPONENT_DCAE;
61 import static org.openecomp.sdc.common.api.Constants.HC_COMPONENT_ON_BOARDING;
63 public class HealthCheckScheduledTask implements Runnable {
64 private static final Logger healthLogger = Logger.getLogger("asdc.fe.healthcheck");
65 private static final Logger log = Logger.getLogger(HealthCheckScheduledTask.class.getName());
66 private static final String LOG_PARTNER_NAME = "SDC.FE";
67 private static final String LOG_TARGET_ENTITY_BE = "SDC.BE";
68 private static final String LOG_TARGET_ENTITY_CONFIG = "SDC.FE.Configuration";
69 private static final String LOG_TARGET_SERVICE_NAME_OB = "getOnboardingConfig";
70 private static final String LOG_TARGET_SERVICE_NAME_DCAE = "getDCAEConfig";
71 private static final String LOG_TARGET_SERVICE_NAME_FACADE = "getCatalogFacadeConfig";
72 private static final String LOG_SERVICE_NAME = "/rest/healthCheck";
73 private static LogFieldsMdcHandler mdcFieldsHandler = new LogFieldsMdcHandler();
75 private static final String URL = "%s://%s:%s/sdc2/rest/healthCheck";
77 private final List<String> healthCheckFeComponents =
78 Arrays.asList(HC_COMPONENT_ON_BOARDING, HC_COMPONENT_DCAE, HC_COMPONENT_CATALOG_FACADE_MS);
79 private static final HealthCheckUtil healthCheckUtil = new HealthCheckUtil();
80 private static final String DEBUG_CONTEXT = "HEALTH_FE";
81 private static final String EXTERNAL_HC_URL = "%s://%s:%s%s";
82 private static String ONBOARDING_HC_URL;
83 private static String DCAE_HC_URL;
84 private static String CATALOG_FACADE_MS_HC_URL;
86 private final HealthCheckService service;
88 HealthCheckScheduledTask(HealthCheckService service) {
89 this.service = service;
92 static String getOnboardingHcUrl() {
93 return ONBOARDING_HC_URL;
96 static String getDcaeHcUrl() {
100 static String getCatalogFacadeMsHcUrl() {
101 return CATALOG_FACADE_MS_HC_URL;
107 mdcFieldsHandler.addInfoForErrorAndDebugLogging(LOG_PARTNER_NAME);
108 healthLogger.trace("Executing FE Health Check Task - Start");
109 HealthCheckService.HealthStatus currentHealth = checkHealth();
110 int currentHealthStatus = currentHealth.getStatusCode();
111 healthLogger.trace("Executing FE Health Check Task - Status = {}", currentHealthStatus);
113 // In case health status was changed, issue alarm/recovery
114 if (currentHealthStatus != service.getLastHealthStatus().getStatusCode()) {
115 log.trace("FE Health State Changed to {}. Issuing alarm / recovery alarm...", currentHealthStatus);
116 logFeAlarm(currentHealthStatus);
118 // Anyway, update latest response
119 service.setLastHealthStatus(currentHealth);
122 private List<HealthCheckInfo> addHostedComponentsFeHealthCheck(String baseComponent, boolean requestedByBE) {
123 String healthCheckUrl = getExternalComponentHcUrl(baseComponent);
124 String serviceName = getExternalComponentHcUri(baseComponent);
125 ErrorLogOptionalData errorLogOptionalData = ErrorLogOptionalData.newBuilder().targetEntity(baseComponent)
126 .targetServiceName(serviceName).build();
128 StringBuilder description = new StringBuilder("");
129 int connectTimeoutMs = 3000;
130 int readTimeoutMs = service.getConfig().getHealthCheckSocketTimeoutInMs(5000);
132 if (healthCheckUrl != null) {
133 ObjectMapper mapper = new ObjectMapper();
135 HttpResponse<String> response = HttpRequest.get(healthCheckUrl, new HttpClientConfig(new Timeouts(connectTimeoutMs, readTimeoutMs)));
136 int beStatus = response.getStatusCode();
137 if (beStatus == HttpStatus.SC_OK || beStatus == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
138 String beJsonResponse = response.getResponse();
139 return convertResponse(beJsonResponse, mapper, baseComponent, description, beStatus);
141 description.append("Response code: " + beStatus);
142 log.trace("{} Health Check Response code: {}", baseComponent, beStatus);
144 } catch (Exception e) {
145 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, serviceName, errorLogOptionalData, baseComponent + " unexpected response ", e);
146 description.append(baseComponent + " Unexpected response: " + e.getMessage());
149 description.append(baseComponent + " health check Configuration is missing");
152 String compName = requestedByBE ? Constants.HC_COMPONENT_FE : baseComponent;
153 return Collections.singletonList(new HealthCheckInfo(
155 HealthCheckInfo.HealthCheckStatus.DOWN,
157 description.toString()));
160 private String getExternalComponentHcUri(String baseComponent) {
161 String healthCheckUri = null;
162 switch (baseComponent) {
163 case HC_COMPONENT_ON_BOARDING:
164 healthCheckUri = service.getConfig().getOnboarding().getHealthCheckUriFe();
166 case HC_COMPONENT_DCAE:
167 healthCheckUri = service.getConfig().getDcae().getHealthCheckUri();
169 case HC_COMPONENT_CATALOG_FACADE_MS:
170 healthCheckUri = service.getConfig().getCatalogFacadeMs().getHealthCheckUri();
173 log.debug("Unsupported base component {}", baseComponent);
176 return healthCheckUri;
181 String getExternalComponentHcUrl(String baseComponent) {
182 String healthCheckUrl = null;
183 switch (baseComponent) {
184 case HC_COMPONENT_ON_BOARDING:
185 healthCheckUrl = getOnboardingHealthCheckUrl();
187 case HC_COMPONENT_DCAE:
188 healthCheckUrl = getDcaeHealthCheckUrl();
190 case HC_COMPONENT_CATALOG_FACADE_MS:
191 healthCheckUrl = getCatalogFacadeHealthCheckUrl();
194 log.debug("Unsupported base component {}", baseComponent);
197 return healthCheckUrl;
200 private void logFeAlarm(int lastFeStatus) {
201 switch (lastFeStatus) {
203 FeEcompErrorManager.getInstance().processEcompError(DEBUG_CONTEXT, EcompErrorEnum.FeHealthCheckRecovery, "FE Health Recovered");
204 FeEcompErrorManager.getInstance().logFeHealthCheckRecovery("FE Health Recovered");
207 FeEcompErrorManager.getInstance().processEcompError(DEBUG_CONTEXT, EcompErrorEnum.FeHealthCheckError, "Connection with ASDC-BE is probably down");
208 FeEcompErrorManager.getInstance().logFeHealthCheckError("Connection with ASDC-BE is probably down");
215 private HealthCheckService.HealthStatus checkHealth() {
216 Gson gson = new GsonBuilder().setPrettyPrinting().create();
217 Configuration config = service.getConfig();
219 HealthCheckWrapper feAggHealthCheck;
220 boolean aggregateFeStatus = false;
221 String redirectedUrl = String.format(URL, config.getBeProtocol(), config.getBeHost(),
222 Constants.HTTPS.equals(config.getBeProtocol()) ? config.getBeSslPort() : config.getBeHttpPort());
223 int connectTimeoutMs = 3000;
224 int readTimeoutMs = config.getHealthCheckSocketTimeoutInMs(5000);
225 ErrorLogOptionalData errorLogOptionalData = ErrorLogOptionalData.newBuilder().targetEntity(LOG_TARGET_ENTITY_BE)
226 .targetServiceName(LOG_SERVICE_NAME).build();
229 HttpResponse<String> response = HttpRequest.get(redirectedUrl, new HttpClientConfig(new Timeouts(connectTimeoutMs, readTimeoutMs)));
230 log.debug("HC call to BE - status code is {}", response.getStatusCode());
231 String beJsonResponse = response.getResponse();
232 feAggHealthCheck = getFeHealthCheckInfos(gson, beJsonResponse);
233 if (response.getStatusCode() != HttpStatus.SC_INTERNAL_SERVER_ERROR) {
234 aggregateFeStatus = healthCheckUtil.getAggregateStatus(feAggHealthCheck.getComponentsInfo(), getExcludedComponentList());
236 //Getting aggregate FE status
237 return new HealthCheckService.HealthStatus(aggregateFeStatus ? HttpStatus.SC_OK : HttpStatus.SC_INTERNAL_SERVER_ERROR, gson.toJson(feAggHealthCheck));
240 catch (Exception e) {
241 log.debug("Health Check error when trying to connect to BE or external FE. Error: {}", e);
242 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, LOG_SERVICE_NAME, errorLogOptionalData,
243 "Health Check error when trying to connect to BE or external FE.", e.getMessage());
244 FeEcompErrorManager.getInstance().processEcompError(DEBUG_CONTEXT,EcompErrorEnum.FeHealthCheckGeneralError, "Unexpected FE Health check error");
245 FeEcompErrorManager.getInstance().logFeHealthCheckGeneralError("Unexpected FE Health check error");
246 return new HealthCheckService.HealthStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR, gson.toJson(getBeDownCheckInfos()));
251 List<String> getExcludedComponentList() {
252 List <String> excludedComponentList = Lists.newArrayList(service.getConfig().getHealthStatusExclude());
253 if (isCatalogFacadeMsExcluded()) {
254 if (log.isInfoEnabled()) {
255 log.info(HC_COMPONENT_CATALOG_FACADE_MS + " has been added to the Healthcheck exclude list");
257 excludedComponentList.add(HC_COMPONENT_CATALOG_FACADE_MS);
259 return excludedComponentList;
262 private boolean isCatalogFacadeMsExcluded() {
263 //CATALOG_FACADE_MS is excluded if it is not configured
264 return service.getConfig().getCatalogFacadeMs() == null || StringUtils.isEmpty(service.getConfig().getCatalogFacadeMs().getPath());
267 private HealthCheckWrapper getFeHealthCheckInfos(Gson gson, String responseString) {
268 Type wrapperType = new TypeToken<HealthCheckWrapper>() {
270 HealthCheckWrapper healthCheckWrapper = gson.fromJson(responseString, wrapperType);
271 String description = "OK";
272 healthCheckWrapper.getComponentsInfo()
273 .add(new HealthCheckInfo(Constants.HC_COMPONENT_FE, HealthCheckInfo.HealthCheckStatus.UP, ExternalConfiguration.getAppVersion(), description));
275 //add FE hosted components
276 for (String component : healthCheckFeComponents) {
277 buildHealthCheckListForComponent(component, healthCheckWrapper);
279 return healthCheckWrapper;
282 private void buildHealthCheckListForComponent(String component, HealthCheckWrapper healthCheckWrapper) {
284 HealthCheckInfo componentHCInfoFromBE = getComponentHcFromList(component, healthCheckWrapper.getComponentsInfo());
285 List<HealthCheckInfo> componentHCInfoList = addHostedComponentsFeHealthCheck(component, componentHCInfoFromBE != null);
286 HealthCheckInfo calculateStatusFor;
287 if (componentHCInfoFromBE != null) {
288 if (log.isDebugEnabled()) {
289 log.debug("{} component healthcheck info has been received from the BE and from the component itself", component);
291 //update the subcomponents's HC if exist and recalculate the component status according to the subcomponets HC
292 calculateStatusFor = updateSubComponentsInfoOfBeHc(componentHCInfoFromBE, componentHCInfoList);
296 //this component is not in the BE HC response, need to add it and calculate the aggregated status
297 if (log.isDebugEnabled()) {
298 log.debug("{} component healthcheck info has been received from the component itself, it is not monitored by the BE", component);
300 //we assume that response from components which HC is not requested by BE have only one entry in the responded list
301 calculateStatusFor = componentHCInfoList.get(0);
302 healthCheckWrapper.getComponentsInfo()
303 .add(calculateStatusFor);
306 calculateAggregatedStatus(calculateStatusFor);
311 HealthCheckInfo updateSubComponentsInfoOfBeHc(HealthCheckInfo componentHCInfoFromBE, List<HealthCheckInfo> componentHcReceivedByFE) {
312 if (!CollectionUtils.isEmpty(componentHcReceivedByFE)) {
313 //this component HC is received from BE, just need to calculate the status for that
314 if (componentHCInfoFromBE.getComponentsInfo() == null) {
315 componentHCInfoFromBE.setComponentsInfo(new ArrayList<>());
317 componentHCInfoFromBE.getComponentsInfo().addAll(componentHcReceivedByFE);
319 return componentHCInfoFromBE;
322 private HealthCheckInfo getComponentHcFromList(String component, List<HealthCheckInfo> hcList) {
323 return hcList.stream().filter(c -> c.getHealthCheckComponent().equals(component)).findFirst().orElse(null);
326 private void calculateAggregatedStatus(HealthCheckInfo baseComponentHCInfo) {
327 if (!CollectionUtils.isEmpty(baseComponentHCInfo.getComponentsInfo())) {
328 boolean status = healthCheckUtil.getAggregateStatus(baseComponentHCInfo.getComponentsInfo(), getExcludedComponentList());
329 baseComponentHCInfo.setHealthCheckStatus(status ?
330 HealthCheckInfo.HealthCheckStatus.UP : HealthCheckInfo.HealthCheckStatus.DOWN);
332 String componentsDesc = healthCheckUtil.getAggregateDescription(baseComponentHCInfo.getComponentsInfo());
333 if (!StringUtils.isEmpty(componentsDesc)) { //aggregated description contains all the internal components desc
334 baseComponentHCInfo.setDescription(componentsDesc);
339 private HealthCheckWrapper getBeDownCheckInfos() {
340 List<HealthCheckInfo> healthCheckInfos = new ArrayList<>();
341 healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_FE, HealthCheckInfo.HealthCheckStatus.UP,
342 ExternalConfiguration.getAppVersion(), "OK"));
343 healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_BE, HealthCheckInfo.HealthCheckStatus.DOWN, null, null));
344 healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_JANUSGRAPH, HealthCheckInfo.HealthCheckStatus.UNKNOWN, null, null));
345 healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_CASSANDRA, HealthCheckInfo.HealthCheckStatus.UNKNOWN, null, null));
346 healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckInfo.HealthCheckStatus.UNKNOWN, null, null));
347 healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_ON_BOARDING, HealthCheckInfo.HealthCheckStatus.UNKNOWN, null, null));
348 healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_DCAE, HealthCheckInfo.HealthCheckStatus.UNKNOWN, null, null));
349 healthCheckInfos.add(new HealthCheckInfo(HC_COMPONENT_CATALOG_FACADE_MS, HealthCheckInfo.HealthCheckStatus.UNKNOWN, null, null));
350 return new HealthCheckWrapper(healthCheckInfos, "UNKNOWN", "UNKNOWN");
353 String buildHealthCheckUrl(String protocol, String host, Integer port, String uri) {
354 return String.format(EXTERNAL_HC_URL, protocol, host, port, uri);
357 private String getOnboardingHealthCheckUrl() {
358 Configuration.OnboardingConfig onboardingConfig = service.getConfig().getOnboarding();
359 ErrorLogOptionalData errorLogOptionalData = ErrorLogOptionalData.newBuilder().targetEntity(LOG_TARGET_ENTITY_CONFIG)
360 .targetServiceName(LOG_TARGET_SERVICE_NAME_OB).build();
362 if (StringUtils.isEmpty(ONBOARDING_HC_URL)) {
363 if (onboardingConfig != null) {
364 ONBOARDING_HC_URL = buildHealthCheckUrl(
365 onboardingConfig.getProtocolFe(), onboardingConfig.getHostFe(),
366 onboardingConfig.getPortFe(), onboardingConfig.getHealthCheckUriFe());
369 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, LOG_SERVICE_NAME, errorLogOptionalData,
370 "Onboarding health check configuration is missing.");
373 return ONBOARDING_HC_URL;
376 private String getDcaeHealthCheckUrl() {
377 Configuration.DcaeConfig dcaeConfig = service.getConfig().getDcae();
378 ErrorLogOptionalData errorLogOptionalData = ErrorLogOptionalData.newBuilder().targetEntity(LOG_TARGET_ENTITY_CONFIG)
379 .targetServiceName(LOG_TARGET_SERVICE_NAME_DCAE).build();
381 if (StringUtils.isEmpty(DCAE_HC_URL)) {
382 if (dcaeConfig != null) {
383 DCAE_HC_URL = buildHealthCheckUrl(
384 dcaeConfig.getProtocol(), dcaeConfig.getHost(),
385 dcaeConfig.getPort(), dcaeConfig.getHealthCheckUri());
388 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, LOG_SERVICE_NAME, errorLogOptionalData,
389 "DCAE health check configuration is missing.");
395 private String getCatalogFacadeHealthCheckUrl() {
396 Configuration.CatalogFacadeMsConfig catalogFacadeMsConfig = service.getConfig().getCatalogFacadeMs();
397 ErrorLogOptionalData errorLogOptionalData = ErrorLogOptionalData.newBuilder().targetEntity(LOG_TARGET_ENTITY_CONFIG)
398 .targetServiceName(LOG_TARGET_SERVICE_NAME_FACADE).build();
400 if (StringUtils.isEmpty(CATALOG_FACADE_MS_HC_URL)) {
401 if (catalogFacadeMsConfig != null) {
402 CATALOG_FACADE_MS_HC_URL = buildHealthCheckUrl(
403 catalogFacadeMsConfig.getProtocol(), catalogFacadeMsConfig.getHost(),
404 catalogFacadeMsConfig.getPort(), catalogFacadeMsConfig.getHealthCheckUri());
407 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, LOG_SERVICE_NAME, errorLogOptionalData,
408 "Catalog Facade MS health check configuration is missing.");
411 return CATALOG_FACADE_MS_HC_URL;
415 private List<HealthCheckInfo> convertResponse(String beJsonResponse, ObjectMapper mapper, String baseComponent, StringBuilder description, int beStatus) {
416 ErrorLogOptionalData errorLogOptionalData = ErrorLogOptionalData.newBuilder().targetEntity(baseComponent)
417 .targetServiceName(LOG_SERVICE_NAME).build();
420 Map<String, Object> healthCheckMap = mapper.readValue(beJsonResponse, new TypeReference<Map<String, Object>>() {
422 if (healthCheckMap.containsKey("componentsInfo")) {
423 return mapper.convertValue(healthCheckMap.get("componentsInfo"), new TypeReference<List<HealthCheckInfo>>() {
426 description.append("Internal components are missing");
428 } catch (JsonSyntaxException | IOException e) {
429 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, LOG_SERVICE_NAME, errorLogOptionalData,
430 baseComponent + " Unexpected response body ", e);
431 description.append(baseComponent)
432 .append("Unexpected response body. Response code: ")
435 return new ArrayList<>();