2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.mso.bpmn.core;
24 import java.io.BufferedReader;
25 import java.io.InputStreamReader;
26 import java.util.Base64;
28 import java.util.UUID;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.HEAD;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.QueryParam;
35 import javax.ws.rs.core.Response;
37 import org.apache.http.HttpEntity;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.HttpStatus;
40 import org.apache.http.client.config.RequestConfig;
41 import org.apache.http.client.methods.HttpPost;
42 import org.apache.http.entity.StringEntity;
43 import org.apache.http.impl.client.CloseableHttpClient;
44 import org.apache.http.impl.client.HttpClientBuilder;
45 import org.camunda.bpm.engine.ProcessEngines;
46 import org.openecomp.mso.HealthCheckUtils;
47 import org.openecomp.mso.logger.MessageEnum;
48 import org.openecomp.mso.logger.MsoLogger;
49 import org.openecomp.mso.utils.CryptoUtils;
50 import org.openecomp.mso.utils.UUIDChecker;
53 public class HealthCheckHandler {
55 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
56 private static final String SITENAME = "mso.sitename";
57 private static final String ADAPTER_ENDPOINT = "mso.adapters.db.endpoint";
58 private static final String ADAPTER_NAMESPACE = "mso.adapters.namespace";
59 private static final String CONFIG = "mso.bpmn.urn.properties";
60 private static final String CREDENTIAL = "mso.adapters.db.auth";
61 private static final String MSOKEY = "mso.msoKey";
62 private String healthcheckDebugEnabled = "mso.healthcheck.log.debug";
64 private static final String CHECK_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title>Health Check</title></head><body>Application ready</body></html>";
65 private static final String NOT_FOUND = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title>Application Not Started</title></head><body>Application not started. Properties file missing or invalid or database Connection failed</body></html>";
66 private static final String NOT_HEALTHY = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title>Application Not Started</title></head><body>Application not available or at least one of the sub-modules is not available.</body></html>";
67 public static final Response HEALTH_CHECK_RESPONSE = Response.status (HttpStatus.SC_OK)
70 public static final Response HEALTH_CHECK_NOK_RESPONSE = Response.status (HttpStatus.SC_SERVICE_UNAVAILABLE)
73 public static final Response NOT_STARTED_RESPONSE = Response.status (HttpStatus.SC_SERVICE_UNAVAILABLE)
79 @Path("/nodehealthcheck")
80 @Produces("text/html")
81 public Response nodeHealthcheck () {
82 MsoLogger.setServiceName ("NodeHealthcheck");
83 // Generate a Request Id
84 String requestId = UUIDChecker.generateUUID(msoLogger);
86 PropertyConfiguration propertyConfiguration = PropertyConfiguration.getInstance();
87 Map<String,String> props = propertyConfiguration.getProperties(CONFIG);
91 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.AvailabilityError, "Unable to load " + CONFIG);
93 return NOT_STARTED_RESPONSE;
96 String siteName = props.get(SITENAME);
97 String endpoint = props.get(ADAPTER_ENDPOINT);
99 if (null == siteName || siteName.length () == 0 || null == endpoint || endpoint.length () == 0) {
101 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.DataError, "Unable to load key attributes (" + SITENAME + " or " + ADAPTER_ENDPOINT + ") from the config file:" + CONFIG);
103 return NOT_STARTED_RESPONSE;
107 if (!this.getSiteStatus (endpoint, siteName, props.get(CREDENTIAL), props.get(MSOKEY), props.get(ADAPTER_NAMESPACE))) {
108 msoLogger.debug("This site is currently disabled for maintenance.");
109 return HEALTH_CHECK_NOK_RESPONSE;
111 } catch (Exception e) {
113 msoLogger.error(MessageEnum.GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception while getting SiteStatus", e);
115 msoLogger.debug("Exception while getting SiteStatus");
116 return NOT_STARTED_RESPONSE;
120 HealthCheckUtils healthCheck = new HealthCheckUtils ();
121 if (healthCheck.verifyNodeHealthCheck(HealthCheckUtils.NodeType.BPMN, requestId)) {
122 msoLogger.debug("nodeHealthcheck - Successful");
123 return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
125 msoLogger.debug("nodeHealthcheck - At leaset one of the sub-modules is not available.");
126 return HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
132 @Path("/healthcheck")
133 @Produces("text/html")
134 public Response healthcheck (@QueryParam("requestId") String requestId) {
135 MsoLogger.setServiceName ("Healthcheck");
136 verifyOldUUID(requestId);
138 PropertyConfiguration propertyConfiguration = PropertyConfiguration.getInstance();
139 Map<String,String> props = propertyConfiguration.getProperties(CONFIG);
143 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.AvailabilityError, "Unable to load " + CONFIG);
145 return NOT_STARTED_RESPONSE;
148 String siteName = props.get(SITENAME);
149 String endpoint = props.get(ADAPTER_ENDPOINT);
151 if (null == siteName || siteName.length () == 0 || null == endpoint || endpoint.length () == 0) {
153 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.DataError, "Unable to load key attributes (" + SITENAME + " or " + ADAPTER_ENDPOINT + ") from the config file:" + CONFIG);
155 return NOT_STARTED_RESPONSE;
159 if (!this.getSiteStatus (endpoint, siteName, props.get(CREDENTIAL), props.get(MSOKEY), props.get(ADAPTER_NAMESPACE))) {
160 msoLogger.debug("This site is currently disabled for maintenance.");
161 return HEALTH_CHECK_NOK_RESPONSE;
163 } catch (Exception e) {
165 msoLogger.error(MessageEnum.GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception while getting SiteStatus", e);
167 msoLogger.debug("Exception while getting SiteStatus");
168 return NOT_STARTED_RESPONSE;
172 // TODO: check the appropriate process engine
173 // ProcessEngines.getDefaultProcessEngine().getIdentityService().createGroupQuery().list();
174 } catch (final Exception e) {
176 msoLogger.error(MessageEnum.GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception while verifying Camunda engine", e);
178 msoLogger.debug("Exception while verifying Camunda engine");
179 return NOT_STARTED_RESPONSE;
182 return HEALTH_CHECK_RESPONSE;
186 private String verifyOldUUID (String oldId) {
187 if (!isValidUUID(oldId)) {
188 String newId = UUID.randomUUID().toString();
189 MsoLogger.setLogContext(newId, null);
192 MsoLogger.setLogContext(oldId, null);
197 private boolean isValidUUID (String id) {
202 UUID uuid = UUID.fromString(id);
203 return uuid.toString().equalsIgnoreCase(id);
204 } catch (IllegalArgumentException iae) {
205 msoLogger.debug("IllegalArgumentException :",iae);
210 private String decrypt(String encryptedString, String key){
212 if (encryptedString != null && !encryptedString.isEmpty() && key != null && !key.isEmpty()) {
213 return CryptoUtils.decrypt(encryptedString, key);
215 } catch (Exception e) {
216 msoLogger.error(MessageEnum.GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Failed to decrypt credentials", e);
221 private boolean getSiteStatus (String url, String site, String credential, String key, String adapterNamespace) throws Exception {
222 // set the connection timeout value to 30 seconds (30000 milliseconds)
223 RequestConfig.Builder requestBuilder = RequestConfig.custom();
224 requestBuilder = requestBuilder.setConnectTimeout(30000);
225 requestBuilder = requestBuilder.setConnectionRequestTimeout(30000);
226 HttpClientBuilder builder = HttpClientBuilder.create ();
227 builder.setDefaultRequestConfig (requestBuilder.build ());
229 HttpPost post = new HttpPost(url);
231 String cred = decrypt(credential, key);
232 if (cred != null && !cred.isEmpty()) {
233 post.setHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString(cred.getBytes()));
235 if(healthcheckDebugEnabled == null){
236 healthcheckDebugEnabled = "false";
238 BPMNLogger.debug(healthcheckDebugEnabled, "Post url is: " + url);
240 //now create a soap request message as follows:
241 final StringBuffer payload = new StringBuffer();
242 payload.append("\n");
243 payload.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:req=\"" + adapterNamespace + "/requestsdb\">\n");
244 payload.append("<soapenv:Header/>\n");
245 payload.append("<soapenv:Body>\n");
246 payload.append("<req:getSiteStatus>\n");
247 payload.append("<siteName>" + site + "</siteName>\n");
248 payload.append("</req:getSiteStatus>\n");
249 payload.append("</soapenv:Body>\n");
250 payload.append("</soapenv:Envelope>\n");
252 BPMNLogger.debug(healthcheckDebugEnabled, "Initialize SOAP request to url:" + url);
253 BPMNLogger.debug(healthcheckDebugEnabled, "The payload of the request is:" + payload);
254 HttpEntity entity = new StringEntity(payload.toString(),"UTF-8");
255 post.setEntity(entity);
257 CloseableHttpClient client = builder.build ();
258 HttpResponse response = client.execute(post);
259 BPMNLogger.debug(healthcheckDebugEnabled, "Response received is:" + response);
261 int statusCode = response.getStatusLine().getStatusCode();
262 if (statusCode != 200) {
264 msoLogger.error(MessageEnum.GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.DataError,
265 "Communication with DB Adapter failed, The response received from DB Adapter is with failed status code:" + statusCode);
267 Exception e = new Exception("Communication with DB Adapter failed");
270 BufferedReader rd = new BufferedReader(
271 new InputStreamReader(response.getEntity().getContent()));
273 StringBuffer result = new StringBuffer();
275 while ((line = rd.readLine()) != null) {
278 BPMNLogger.debug(healthcheckDebugEnabled, "Content of the response is:" + result);
279 String status = result.substring(result.indexOf("<return>") + 8, result.indexOf("</return>"));
281 client.close (); //shut down the connection
282 return Boolean.valueOf(status);