Change the header to SO
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / healthcheck / HealthCheckHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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 javax.ws.rs.GET;
25 import javax.ws.rs.HEAD;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.Produces;
28 import javax.ws.rs.QueryParam;
29 import javax.ws.rs.core.Response;
30
31 import org.apache.http.HttpStatus;
32 import org.openecomp.mso.HealthCheckUtils;
33 import org.openecomp.mso.logger.MessageEnum;
34 import org.openecomp.mso.logger.MsoLogger;
35 import org.openecomp.mso.properties.MsoJsonProperties;
36 import org.openecomp.mso.properties.MsoPropertiesFactory;
37 import org.openecomp.mso.utils.UUIDChecker;
38
39
40 @Path("/")
41         public class HealthCheckHandler {
42                 
43                 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.ASDC);
44                 private static final String MSO_PROP_ASDC = "MSO_PROP_ASDC";
45                 private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
46
47                 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>";
48                 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>";
49
50                 private static final Response OK_RESPONSE = Response.status (HttpStatus.SC_OK).entity (SUC_HTML).build ();
51                 private static final Response NOK_RESPONSE = Response.status (HttpStatus.SC_SERVICE_UNAVAILABLE).entity (NOT_FOUND).build ();
52
53                 @HEAD
54             @GET
55             @Path("/healthcheck")
56             @Produces("text/html")
57             public Response healthcheck (@QueryParam("requestId") String requestId) {
58                         long startTime = System.currentTimeMillis ();
59                         MsoLogger.setServiceName ("Healthcheck");
60                         UUIDChecker.verifyOldUUID(requestId, msoLogger);
61                         HealthCheckUtils healthCheck = new HealthCheckUtils ();
62                         if (!healthCheck.siteStatusCheck(msoLogger, startTime)) {
63                                 return HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
64                         }
65
66                         MsoJsonProperties props = loadMsoProperties ();
67                         if (props == null) {
68                                 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.ServiceNotAvailable, "Application Not Ready");
69                                 return HealthCheckUtils.NOT_STARTED_RESPONSE;
70                         }
71
72                         if (!healthCheck.catalogDBCheck (msoLogger, startTime)) {
73                                 return HealthCheckUtils.NOT_STARTED_RESPONSE;
74                         }
75                         msoLogger.debug("healthcheck - Successful");
76                         return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
77             }
78
79                 private MsoJsonProperties loadMsoProperties () {
80                         MsoJsonProperties msoProperties;
81                         try {
82                                 msoProperties = msoPropertiesFactory.getMsoJsonProperties(MSO_PROP_ASDC);
83                         } catch (Exception e) {
84                                 msoLogger.error (MessageEnum.ASDC_PROPERTIES_NOT_FOUND, MSO_PROP_ASDC, "", "", MsoLogger.ErrorCode.DataError, "Exception - getMsoJsonProperties", e);
85                                 return null;
86                         }
87
88                         if (msoProperties !=null && msoProperties.getJsonRootNode().elements().hasNext()) {
89                                 return msoProperties;
90                         } else {
91                                 msoLogger.error (MessageEnum.ASDC_PROPERTIES_NOT_FOUND , MSO_PROP_ASDC, "", "", MsoLogger.ErrorCode.DataError, "ASDC properties not found");
92                                 return  null;
93                         }
94                 }
95 }