75858b14eca6ab0b778ebec0a5116843abb9a374
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / HealthCheckHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.openecomp.mso.bpmn.core;
23
24 import java.io.BufferedReader;
25 import java.io.InputStreamReader;
26 import java.util.Base64;
27 import java.util.Map;
28 import java.util.UUID;
29
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;
36
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;
51
52 @Path("/")
53 public class HealthCheckHandler  {
54
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";
63
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)
68             .entity (CHECK_HTML)
69             .build ();
70     public static final Response HEALTH_CHECK_NOK_RESPONSE = Response.status (HttpStatus.SC_SERVICE_UNAVAILABLE)
71             .entity (NOT_HEALTHY)
72             .  build ();
73     public static final Response NOT_STARTED_RESPONSE = Response.status (HttpStatus.SC_SERVICE_UNAVAILABLE)
74             .entity (NOT_FOUND)
75             .build ();
76
77     @HEAD
78     @GET
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);
85
86         PropertyConfiguration propertyConfiguration = PropertyConfiguration.getInstance();
87         Map<String,String> props = propertyConfiguration.getProperties(CONFIG);
88
89         if (props == null) {
90
91             msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(),  MsoLogger.ErrorCode.AvailabilityError, "Unable to load " + CONFIG);
92
93             return NOT_STARTED_RESPONSE;
94         }
95
96         String siteName = props.get(SITENAME);
97         String endpoint = props.get(ADAPTER_ENDPOINT);
98
99         if (null == siteName || siteName.length () == 0 || null == endpoint || endpoint.length () == 0) {
100
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);
102
103             return NOT_STARTED_RESPONSE;
104         }
105
106         try {
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;
110             }
111         } catch (Exception e) {
112
113             msoLogger.error(MessageEnum.GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception while getting SiteStatus", e);
114
115             msoLogger.debug("Exception while getting SiteStatus");
116             return NOT_STARTED_RESPONSE;
117         }
118
119
120         HealthCheckUtils healthCheck = new HealthCheckUtils ();
121         if (healthCheck.verifyNodeHealthCheck(HealthCheckUtils.NodeType.BPMN, requestId)) {
122             msoLogger.debug("nodeHealthcheck - Successful");
123             return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
124         } else {
125             msoLogger.debug("nodeHealthcheck - At leaset one of the sub-modules is not available.");
126             return  HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
127         }
128     }
129
130     @HEAD
131     @GET
132     @Path("/healthcheck")
133     @Produces("text/html")
134     public Response healthcheck (@QueryParam("requestId") String requestId) {
135         MsoLogger.setServiceName ("Healthcheck");
136         verifyOldUUID(requestId);
137
138         PropertyConfiguration propertyConfiguration = PropertyConfiguration.getInstance();
139         Map<String,String> props = propertyConfiguration.getProperties(CONFIG);
140
141         if (props == null) {
142
143             msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(),  MsoLogger.ErrorCode.AvailabilityError, "Unable to load " + CONFIG);
144
145             return NOT_STARTED_RESPONSE;
146         }
147
148         String siteName = props.get(SITENAME);
149         String endpoint = props.get(ADAPTER_ENDPOINT);
150
151         if (null == siteName || siteName.length () == 0 || null == endpoint || endpoint.length () == 0) {
152
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);
154
155             return NOT_STARTED_RESPONSE;
156         }
157
158         try {
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;
162             }
163         } catch (Exception e) {
164
165             msoLogger.error(MessageEnum.GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception while getting SiteStatus", e);
166
167             msoLogger.debug("Exception while getting SiteStatus");
168             return NOT_STARTED_RESPONSE;
169         }
170
171         try {
172                 // TODO: check the appropriate process engine
173                 // ProcessEngines.getDefaultProcessEngine().getIdentityService().createGroupQuery().list();
174         } catch (final Exception e) {
175
176             msoLogger.error(MessageEnum.GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception while verifying Camunda engine", e);
177
178             msoLogger.debug("Exception while verifying Camunda engine");
179             return NOT_STARTED_RESPONSE;
180         }
181
182         return HEALTH_CHECK_RESPONSE;
183     }
184
185
186     private String verifyOldUUID (String oldId) {
187         if (!isValidUUID(oldId)) {
188             String newId = UUID.randomUUID().toString();
189             MsoLogger.setLogContext(newId, null);
190             return newId;
191         }
192         MsoLogger.setLogContext(oldId, null);
193         return oldId;
194     }
195
196
197     private boolean isValidUUID (String id) {
198         try {
199             if (null == id) {
200                 return false;
201             }
202             UUID uuid = UUID.fromString(id);
203             return uuid.toString().equalsIgnoreCase(id);
204         } catch (IllegalArgumentException iae) {
205                 msoLogger.debug("IllegalArgumentException :",iae);
206             return false;
207         }
208     }
209
210     private String decrypt(String encryptedString, String key){
211         try {
212             if (encryptedString != null && !encryptedString.isEmpty() && key != null && !key.isEmpty()) {
213                 return CryptoUtils.decrypt(encryptedString, key);
214             }
215         } catch (Exception e) {
216             msoLogger.error(MessageEnum.GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Failed to decrypt credentials", e);
217         }
218         return null;
219     }
220
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 ());
228
229         HttpPost post = new HttpPost(url);
230
231         String cred = decrypt(credential, key);
232         if (cred != null && !cred.isEmpty()) {
233             post.setHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString(cred.getBytes()));
234         }
235         if(healthcheckDebugEnabled == null){
236                 healthcheckDebugEnabled = "false";
237         }
238         BPMNLogger.debug(healthcheckDebugEnabled, "Post url is: " + url);
239
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");
251
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);
256
257         CloseableHttpClient client = builder.build ();
258         HttpResponse response = client.execute(post);
259         BPMNLogger.debug(healthcheckDebugEnabled, "Response received is:" + response);
260
261         int statusCode = response.getStatusLine().getStatusCode();
262         if (statusCode != 200) {
263
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);
266
267             Exception e = new Exception("Communication with DB Adapter failed");
268             throw e;
269         }
270         BufferedReader rd = new BufferedReader(
271                 new InputStreamReader(response.getEntity().getContent()));
272
273         StringBuffer result = new StringBuffer();
274         String line = "";
275         while ((line = rd.readLine()) != null) {
276             result.append(line);
277         }
278         BPMNLogger.debug(healthcheckDebugEnabled, "Content of the response is:" + result);
279         String status = result.substring(result.indexOf("<return>") + 8, result.indexOf("</return>"));
280
281         client.close (); //shut down the connection
282         return Boolean.valueOf(status);
283     }
284 }