Initial OpenECOMP MSO commit
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / healthcheck / HealthCheckHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.asdc.healthcheck;
22
23
24 import org.openecomp.mso.MsoStatusUtil;
25 import org.openecomp.mso.db.catalog.CatalogDatabase;
26 import org.openecomp.mso.logger.MessageEnum;
27 import org.openecomp.mso.logger.MsoLogger;
28 import org.openecomp.mso.properties.MsoJsonProperties;
29 import org.openecomp.mso.properties.MsoPropertiesFactory;
30 import org.openecomp.mso.HealthCheckUtils;
31 import org.openecomp.mso.utils.UUIDChecker;
32 import org.apache.http.HttpStatus;
33
34 import javax.ws.rs.GET;
35 import javax.ws.rs.HEAD;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.QueryParam;
39 import javax.ws.rs.core.Response;
40
41
42 @Path("/")
43         public class HealthCheckHandler {
44                 
45                 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.ASDC);
46                 private static final String MSO_PROP_ASDC = "MSO_PROP_ASDC";
47                 private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
48
49                 private static final String SUC_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title>Health Check</title></head><body>Application ready</body></html>";
50                 private static final String NOT_FOUND = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title>Application Not Ready</title></head><body>Application Not Ready. Properties file missing or invalid or database Connection failed</body></html>";
51
52                 private static final Response OK_RESPONSE = Response.status (HttpStatus.SC_OK).entity (SUC_HTML).build ();
53                 private static final Response NOK_RESPONSE = Response.status (HttpStatus.SC_SERVICE_UNAVAILABLE).entity (NOT_FOUND).build ();
54
55                 @HEAD
56             @GET
57             @Path("/healthcheck")
58             @Produces("text/html")
59             public Response healthcheck (@QueryParam("requestId") String requestId) {
60                         long startTime = System.currentTimeMillis ();
61                         MsoLogger.setServiceName ("Healthcheck");
62                         UUIDChecker.verifyOldUUID(requestId, msoLogger);
63                         HealthCheckUtils healthCheck = new HealthCheckUtils ();
64                         if (!healthCheck.siteStatusCheck(msoLogger, startTime)) {
65                                 return HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
66                         }
67
68                         MsoJsonProperties props = loadMsoProperties ();
69                         if (props == null) {
70                                 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.ServiceNotAvailable, "Application Not Ready");
71                                 return HealthCheckUtils.NOT_STARTED_RESPONSE;
72                         }
73
74                         if (!healthCheck.catalogDBCheck (msoLogger, startTime)) {
75                                 return HealthCheckUtils.NOT_STARTED_RESPONSE;
76                         }
77                         msoLogger.debug("healthcheck - Successful");
78                         return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
79             }
80
81                 private MsoJsonProperties loadMsoProperties () {
82                         MsoJsonProperties msoProperties;
83                         try {
84                                 msoProperties = msoPropertiesFactory.getMsoJsonProperties(MSO_PROP_ASDC);
85                         } catch (Exception e) {
86                                 msoLogger.error (MessageEnum.ASDC_PROPERTIES_NOT_FOUND, MSO_PROP_ASDC, "", "", MsoLogger.ErrorCode.DataError, "Exception - getMsoJsonProperties", e);
87                                 return null;
88                         }
89
90                         if (msoProperties !=null && msoProperties.getJsonRootNode().getElements().hasNext()) {
91                                 return msoProperties;
92                         } else {
93                                 msoLogger.error (MessageEnum.ASDC_PROPERTIES_NOT_FOUND , MSO_PROP_ASDC, "", "", MsoLogger.ErrorCode.DataError, "ASDC properties not found");
94                                 return  null;
95                         }
96                 }
97 }