[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / HealthCheckController.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.controller;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.slf4j.MDC;
29 import org.springframework.context.annotation.EnableAspectJAutoProxy;
30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RequestMethod;
32 import org.springframework.web.bind.annotation.RestController;
33
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
35 import org.openecomp.portalapp.controller.EPUnRestrictedBaseController;
36 import org.openecomp.portalapp.portal.listener.HealthMonitor;
37 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
38 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
39 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
40 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
41 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
42 import com.google.gson.Gson;
43
44 /*
45  * This controller checks the health status and returns JSON response.
46  * It does not require authentication or session.
47  */
48 @RestController
49 @org.springframework.context.annotation.Configuration
50 @EnableAspectJAutoProxy
51 @EPAuditLog
52 public class HealthCheckController extends EPUnRestrictedBaseController {
53         
54         private class HealthStatus {
55                 public int statusCode;
56                 @SuppressWarnings("unused")
57                 public String body;
58
59                 public HealthStatus(int code, String body) {
60                         this.statusCode = code;
61                         this.body = body;
62                 }
63         }
64
65         private class HealthStatusInfo {
66                 HealthStatusInfo(String healthCheckComponent) {
67                         this.healthCheckComponent = healthCheckComponent;
68                         this.healthCheckStatus = statusUp; // Default value
69                         this.version = "";
70                         this.description = statusOk; // Default value
71                         this.hostName = "";
72                         this.ipAddress = "";
73                         this.dbClusterStatus = "";
74                         this.dbPermissions = "";
75                 }
76
77                 @SuppressWarnings("unused")
78                 public String healthCheckComponent;
79                 @SuppressWarnings("unused")
80                 public String healthCheckStatus;
81                 @SuppressWarnings("unused")
82                 public String version;
83                 @SuppressWarnings("unused")
84                 public String description;
85                 @SuppressWarnings("unused")
86                 public String hostName;
87                 @SuppressWarnings("unused")
88                 public String ipAddress;
89                 @SuppressWarnings("unused")
90                 public String dbClusterStatus;
91                 @SuppressWarnings("unused")
92                 public String dbPermissions;
93         }
94
95         private final String statusUp = "UP";
96         private final String statusDown = "DOWN";
97         private final String statusOk = "OK";
98
99         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
100         
101         @RequestMapping(value = { "/portalApi/healthCheck" }, method = RequestMethod.GET, produces = "application/json")
102         public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
103                 HealthStatus healthStatus = new HealthStatus(500, "");
104
105                 // Return the status as 500 if it suspended due to manual fail over
106                 if (HealthMonitor.isSuspended) {
107                         healthStatus.body = "Suspended";
108                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
109                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
110                         return healthStatus;
111                 }
112
113                 try {
114                         boolean overallStatus = true;
115
116                         List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
117
118                         HealthStatusInfo beInfo = new HealthStatusInfo("BE");
119                         beInfo.hostName = EcompPortalUtils.getMyHostName();
120                         beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
121                         if (!HealthMonitor.isBackEndUp()) {
122                                 overallStatus = false;
123                                 beInfo.healthCheckStatus = statusDown;
124                                 beInfo.description = "Check the logs for more details";
125                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
126                         }
127                         statusCollection.add(beInfo);
128
129                         HealthStatusInfo feInfo = new HealthStatusInfo("FE");
130                         if (!HealthMonitor.isFrontEndUp()) {
131                                 overallStatus = false;
132                                 feInfo.healthCheckStatus = statusDown;
133                                 feInfo.description = "Check the logs for more details";
134                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
135                         }
136                         statusCollection.add(feInfo);
137
138                         HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
139                         if (!HealthMonitor.isDatabaseUp()) {
140                                 overallStatus = false;
141                                 dbInfo.healthCheckStatus = statusDown;
142                                 dbInfo.description = "Check the logs for more details";
143                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
144                         }
145                                         
146                         if (!HealthMonitor.isClusterStatusOk()) {
147                                 dbInfo.dbClusterStatus = "Problem, check the logs for more details";
148                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
149                         }
150                         else {
151                                 dbInfo.dbClusterStatus = statusOk;
152                         }
153                         
154                         if (!HealthMonitor.isDatabasePermissionsOk()) {
155                                 dbInfo.dbPermissions = "Problem, check the logs for more details";
156                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
157                         }
158                         else {
159                                 dbInfo.dbPermissions = statusOk;
160                         }
161                         statusCollection.add(dbInfo);
162
163                         HealthStatusInfo uebInfo = new HealthStatusInfo("UEB");
164                         if (!HealthMonitor.isUebUp()) {
165                                 //As per test case review meeting, UEB is considered as critical as DB. Hence commenting
166                                 //overallStatus = false;
167                                 uebInfo.healthCheckStatus = statusDown;
168                                 uebInfo.description = "Check the logs for more details";
169                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError);
170                         }
171                         statusCollection.add(uebInfo);
172
173                         String json = "";
174                         try {
175                                 json = new Gson().toJson(statusCollection);
176                         } catch (Exception e) {
177                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
178                         }
179                         logger.info(EELFLoggerDelegate.debugLogger, json);
180
181                         if (overallStatus) {
182                                 healthStatus = new HealthStatus(200, json);
183                         } else {
184                                 healthStatus = new HealthStatus(500, json);
185                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
186                         }
187                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
188                 } catch (Exception e) {
189                         logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the healthcheck. Details: "
190                                         + EcompPortalUtils.getStackTrace(e));
191                 }
192                 
193                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
194                 
195                 return healthStatus;
196         }
197
198         @RequestMapping(value = {
199                         "/portalApi/healthCheckSuspend" }, method = RequestMethod.GET, produces = "application/json")
200         public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
201                 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
202
203                 HealthMonitor.isSuspended = true;
204                 healthStatus.statusCode = 200;
205                 
206                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
207                                                                                                                                                 response.getStatus());
208                 
209                 return healthStatus;
210         }
211
212         @RequestMapping(value = {
213                         "/portalApi/healthCheckResume" }, method = RequestMethod.GET, produces = "application/json")
214         public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
215                 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
216
217                 HealthMonitor.isSuspended = false;
218                 healthStatus.statusCode = 200;
219                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
220                                                                                                                                                         response.getStatus());
221                 return healthStatus;
222         }
223         
224         
225
226         @RequestMapping(value = {
227                         "/portalApi/ping" }, method = RequestMethod.GET, produces = "application/json")
228         public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
229                 HealthStatus healthStatus = new HealthStatus(200, "OK");        
230                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =",
231                                 response.getStatus());
232                 
233                 return healthStatus;
234         }
235 }