summaryrefslogtreecommitdiffstats
path: root/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/HealthCheckService.java
blob: 701003421492b4855399cb9748587812bdd7e5a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.fe.servlets;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContext;
import javax.ws.rs.core.Response;

import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.api.HealthCheckInfo;
import org.openecomp.sdc.common.api.HealthCheckInfo.HealthCheckStatus;
import org.openecomp.sdc.common.api.HealthCheckWrapper;
import org.openecomp.sdc.common.config.EcompErrorName;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.util.HealthCheckUtil;
import org.openecomp.sdc.fe.config.Configuration;
import org.openecomp.sdc.fe.config.ConfigurationManager;
import org.openecomp.sdc.fe.config.FeEcompErrorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

public class HealthCheckService {

	private class HealthStatus {
		public String body;
		public int statusCode;

		public HealthStatus(int code, String body) {
			this.body = body;
			this.statusCode = code;
		}
	}

	private static final String URL = "%s://%s:%s/sdc2/rest/healthCheck";
	private static Logger healthLogger = LoggerFactory.getLogger("asdc.fe.healthcheck");
	private static Logger log = LoggerFactory.getLogger(HealthCheckService.class.getName());

	private HealthStatus lastHealthStatus = new HealthStatus(500, "{}");
	private final List<String> healthCheckFeComponents = Arrays.asList(Constants.HC_COMPONENT_ON_BOARDING, Constants.HC_COMPONENT_DCAE);

	private class HealthCheckScheduledTask implements Runnable {
		@Override
		public void run() {
			healthLogger.trace("Executing FE Health Check Task - Start");
			HealthStatus currentHealth = checkHealth();
			int currentHealthStatus = currentHealth.statusCode;
			healthLogger.trace("Executing FE Health Check Task - Status = {}", currentHealthStatus);

			// In case health status was changed, issue alarm/recovery
			if (currentHealthStatus != lastHealthStatus.statusCode) {
				log.trace("FE Health State Changed to {}. Issuing alarm / recovery alarm...", currentHealthStatus);
				logFeAlarm(currentHealthStatus);
			}

			// Anyway, update latest response
			lastHealthStatus = currentHealth;
		}
	}

	/**
	 * This executor will execute the health check task.
	 */
	ScheduledExecutorService healthCheckExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
		@Override
		public Thread newThread(Runnable r) {
			return new Thread(r, "FE-Health-Check-Thread");
		}
	});
	private ServletContext context;

	public HealthCheckService(ServletContext context) {
		this.context = context;
	}

	public void start(int interval) {
		this.healthCheckExecutor.scheduleAtFixedRate(new HealthCheckScheduledTask(), 0, interval, TimeUnit.SECONDS);
	}

	/**
	 * To be used by the HealthCheckServlet
	 * 
	 * @return
	 */
	public Response getFeHealth() {
		return this.buildResponse(lastHealthStatus.statusCode, lastHealthStatus.body);
	}

	private HealthStatus checkHealth() {
		CloseableHttpClient httpClient = null;
		try {
			Gson gson = new GsonBuilder().setPrettyPrinting().create();
			Configuration config = ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
					.getConfiguration();
			String redirectedUrl = String.format(URL, config.getBeProtocol(), config.getBeHost(),
					config.getBeHttpPort());
			httpClient = getHttpClient(config);
			HttpGet httpGet = new HttpGet(redirectedUrl);
			CloseableHttpResponse beResponse;
			HealthCheckWrapper feAggHealthCheck;
			try {
				beResponse = httpClient.execute(httpGet);
				log.debug("HC call to BE - status code is {}", beResponse.getStatusLine().getStatusCode());
				String beJsonResponse = EntityUtils.toString(beResponse.getEntity());
				feAggHealthCheck = getFeHealthCheckInfos(gson, beJsonResponse);
			} catch (Exception e) {
				log.debug("Health Check error when trying to connect to BE or external FE. Error: {}", e.getMessage());
				log.error("Health Check error when trying to connect to BE or external FE.", e);
				String beDowneResponse = gson.toJson(getBeDownCheckInfos());
				return new HealthStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR, beDowneResponse);
			}
			
			//Getting aggregate FE status
			boolean aggregateFeStatus = HealthCheckUtil.getAggregateStatus(feAggHealthCheck.getComponentsInfo());
			return new HealthStatus(aggregateFeStatus ? HttpStatus.SC_OK : HttpStatus.SC_INTERNAL_SERVER_ERROR, gson.toJson(feAggHealthCheck));
		} catch (Exception e) {
			FeEcompErrorManager.getInstance().processEcompError(EcompErrorName.FeHealthCheckGeneralError, "Unexpected FE Health check error");
			FeEcompErrorManager.getInstance().logFeHealthCheckGeneralError("Unexpected FE Health check error");
			log.error("Unexpected FE health check error {}", e.getMessage());
			return new HealthStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		} finally {
			if (httpClient != null) {
				try {
					httpClient.close();
				} catch (IOException e) {
					log.error("Couldn't close HC HTTP Client: ", e);
				}
			}
		}
	}





	private Response buildResponse(int status, String jsonResponse) {
		healthLogger.trace("FE and BE health check status: {}", jsonResponse);
		return Response.status(status).entity(jsonResponse).build();
	}

	private void logFeAlarm(int lastFeStatus) {

		switch (lastFeStatus) {
		case 200:
			FeEcompErrorManager.getInstance().processEcompError(EcompErrorName.FeHealthCheckRecovery,
					"FE Health Recovered");
			FeEcompErrorManager.getInstance().logFeHealthCheckRecovery("FE Health Recovered");
			break;
		case 500:
			FeEcompErrorManager.getInstance().processEcompError(EcompErrorName.FeHealthCheckConnectionError,
					"Connection with ASDC-BE is probably down");
			FeEcompErrorManager.getInstance().logFeHealthCheckError("Connection with ASDC-BE is probably down");
			break;
		default:
			break;
		}

	}

	private HealthCheckWrapper getFeHealthCheckInfos(Gson gson, String responseString) {
		Type wrapperType = new TypeToken<HealthCheckWrapper>() {
		}.getType();
		HealthCheckWrapper healthCheckWrapper = gson.fromJson(responseString, wrapperType);
		String appVersion = ExternalConfiguration.getAppVersion();
		String description = "OK";
		healthCheckWrapper.getComponentsInfo()
				.add(new HealthCheckInfo(Constants.HC_COMPONENT_FE, HealthCheckStatus.UP, appVersion, description));

		//add hosted components fe component
		for (String component: healthCheckFeComponents) {
			List<HealthCheckInfo> feComponentsInfo = addHostedComponentsFeHealthCheck(component);
			HealthCheckInfo baseComponentHCInfo = healthCheckWrapper.getComponentsInfo().stream().filter(c -> c.getHealthCheckComponent().equals(component)).findFirst().orElse(null);
			if (baseComponentHCInfo != null) {
				if (baseComponentHCInfo.getComponentsInfo() == null) {
					baseComponentHCInfo.setComponentsInfo(new ArrayList<>());
				}
				baseComponentHCInfo.getComponentsInfo().addAll(feComponentsInfo);
				boolean status = HealthCheckUtil.getAggregateStatus(baseComponentHCInfo.getComponentsInfo());
				baseComponentHCInfo.setHealthCheckStatus(status ? HealthCheckStatus.UP : HealthCheckStatus.DOWN);

				String componentsDesc = HealthCheckUtil.getAggregateDescription(baseComponentHCInfo.getComponentsInfo(), baseComponentHCInfo.getDescription());
				if (componentsDesc.length() > 0) { //aggregated description contains all the internal components desc
					baseComponentHCInfo.setDescription(componentsDesc);
				}
			} else {
				log.error("{} not exists in HealthCheck info", component);
			}
		}
		return healthCheckWrapper;
	}

	private List<HealthCheckInfo> addHostedComponentsFeHealthCheck(String baseComponent) {
		Configuration config = ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
				.getConfiguration();

		String healthCheckUrl = null;
		switch(baseComponent) {
			case Constants.HC_COMPONENT_ON_BOARDING:
				healthCheckUrl = buildOnboardingHealthCheckUrl(config);
				break;
			case Constants.HC_COMPONENT_DCAE:
				healthCheckUrl = buildDcaeHealthCheckUrl(config);
				break;
		}

		String description;

		if (healthCheckUrl != null) {
			ObjectMapper mapper = new ObjectMapper();
			CloseableHttpClient httpClient = getHttpClient(config);
			HttpGet httpGet = new HttpGet(healthCheckUrl);
			CloseableHttpResponse beResponse;

			try {
				beResponse = httpClient.execute(httpGet);
				int beStatus = beResponse.getStatusLine().getStatusCode();
				if (beStatus == HttpStatus.SC_OK || beStatus == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
					try {
						String beJsonResponse = EntityUtils.toString(beResponse.getEntity());

						Map<String, Object> healthCheckMap = mapper.readValue(beJsonResponse, new TypeReference<Map<String, Object>>(){});
						if (healthCheckMap.containsKey("componentsInfo")) {
							List<HealthCheckInfo> componentsInfo = mapper.convertValue(healthCheckMap.get("componentsInfo"), new TypeReference<List<HealthCheckInfo>>() {});
							return componentsInfo;
						} else {
							description = "Internal components are missing";
						}
					} catch (JsonSyntaxException e) {
						log.error("{} Unexpected response body ", baseComponent, e);
						description = baseComponent + " Unexpected response body. Response code: " + beStatus;
					}
				} else {
					description = "Response code: " + beStatus;
					log.trace("{} Health Check Response code: {}", baseComponent, beStatus);
				}
			} catch (Exception e) {
				log.error("{} Unexpected response ", baseComponent, e);
				description = baseComponent + " Unexpected response: " + e.getMessage();
			}
		} else {
			description = baseComponent + " health check Configuration is missing";
		}

		return Arrays.asList(new HealthCheckInfo(Constants.HC_COMPONENT_FE, HealthCheckStatus.DOWN, null, description));
	}

	private HealthCheckWrapper getBeDownCheckInfos() {
		List<HealthCheckInfo> healthCheckInfos = new ArrayList<HealthCheckInfo>();
		healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_FE, HealthCheckStatus.UP,
				ExternalConfiguration.getAppVersion(), "OK"));
		healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_BE, HealthCheckStatus.DOWN, null, null));
		healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_TITAN, HealthCheckStatus.UNKNOWN, null, null));
		healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_CASSANDRA, HealthCheckStatus.UNKNOWN, null, null));
		healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.UNKNOWN, null, null));
		healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_ON_BOARDING, HealthCheckStatus.UNKNOWN, null, null));
		healthCheckInfos.add(new HealthCheckInfo(Constants.HC_COMPONENT_DCAE, HealthCheckStatus.UNKNOWN, null, null));
		HealthCheckWrapper hcWrapper = new HealthCheckWrapper(healthCheckInfos, "UNKNOWN", "UNKNOWN");
		return hcWrapper;
	}

	private CloseableHttpClient getHttpClient(Configuration config) {
		int timeout = 3000;
		int socketTimeout = config.getHealthCheckSocketTimeoutInMs(5000);
		RequestConfig.Builder requestBuilder = RequestConfig.custom();
		requestBuilder.setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(socketTimeout);

		HttpClientBuilder builder = HttpClientBuilder.create();
		builder.setDefaultRequestConfig(requestBuilder.build());
		return builder.build();
	}

	private String buildOnboardingHealthCheckUrl(Configuration config) {

		Configuration.OnboardingConfig onboardingConfig = config.getOnboarding();

		if (onboardingConfig != null) {
			String protocol = onboardingConfig.getProtocol();
			String host = onboardingConfig.getHost();
			Integer port = onboardingConfig.getPort();
			String uri = onboardingConfig.getHealthCheckUri();

			return protocol + "://" + host + ":" + port + uri;
		}

		log.error("onboarding health check configuration is missing.");
		return null;
	}

	private String buildDcaeHealthCheckUrl(Configuration config) {

		Configuration.DcaeConfig dcaeConfig = config.getDcae();

		if (dcaeConfig != null) {
			String protocol = dcaeConfig.getProtocol();
			String host = dcaeConfig.getHost();
			Integer port = dcaeConfig.getPort();
			String uri = dcaeConfig.getHealthCheckUri();

			return protocol + "://" + host + ":" + port + uri;
		}

		log.error("dcae health check configuration is missing.");
		return null;
	}
}