[PORTAL-16 PORTAL-18] Widget ms; staging
[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 processes requests for the health-check feature implemented
46  * in the HealthMonitor, which runs in its own thread. These requests do not
47  * require any authentication nor an active user session.
48  */
49 @RestController
50 @org.springframework.context.annotation.Configuration
51 @EnableAspectJAutoProxy
52 @EPAuditLog
53 public class HealthCheckController extends EPUnRestrictedBaseController {
54
55         private class HealthStatus {
56                 public int statusCode;
57                 @SuppressWarnings("unused")
58                 public String body;
59
60                 public HealthStatus(int code, String body) {
61                         this.statusCode = code;
62                         this.body = body;
63                 }
64         }
65
66         private class HealthStatusInfo {
67                 HealthStatusInfo(String healthCheckComponent) {
68                         this.healthCheckComponent = healthCheckComponent;
69                         this.healthCheckStatus = statusUp; // Default value
70                         this.version = "";
71                         this.description = statusOk; // Default value
72                         this.hostName = "";
73                         this.ipAddress = "";
74                         this.dbClusterStatus = "";
75                         this.dbPermissions = "";
76                 }
77
78                 @SuppressWarnings("unused")
79                 public String healthCheckComponent;
80                 @SuppressWarnings("unused")
81                 public String healthCheckStatus;
82                 @SuppressWarnings("unused")
83                 public String version;
84                 @SuppressWarnings("unused")
85                 public String description;
86                 @SuppressWarnings("unused")
87                 public String hostName;
88                 @SuppressWarnings("unused")
89                 public String ipAddress;
90                 @SuppressWarnings("unused")
91                 public String dbClusterStatus;
92                 @SuppressWarnings("unused")
93                 public String dbPermissions;
94         }
95
96         private final String statusUp = "UP";
97         private final String statusDown = "DOWN";
98         private final String statusOk = "OK";
99
100         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
101
102         @RequestMapping(value = { "/portalApi/healthCheck" }, method = RequestMethod.GET, produces = "application/json")
103         public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
104                 HealthStatus healthStatus = new HealthStatus(500, "");
105
106                 // Return the status as 500 if it suspended due to manual fail over
107                 if (HealthMonitor.isSuspended) {
108                         healthStatus.body = "Suspended";
109                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
110                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE,
111                                         Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
112                         return healthStatus;
113                 }
114
115                 try {
116                         boolean overallStatus = true;
117
118                         List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
119
120                         HealthStatusInfo beInfo = new HealthStatusInfo("BE");
121                         beInfo.hostName = EcompPortalUtils.getMyHostName();
122                         beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
123                         if (!HealthMonitor.isBackEndUp()) {
124                                 overallStatus = false;
125                                 beInfo.healthCheckStatus = statusDown;
126                                 beInfo.description = "Check the logs for more details";
127                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
128                         }
129                         statusCollection.add(beInfo);
130
131                         HealthStatusInfo feInfo = new HealthStatusInfo("FE");
132                         if (!HealthMonitor.isFrontEndUp()) {
133                                 overallStatus = false;
134                                 feInfo.healthCheckStatus = statusDown;
135                                 feInfo.description = "Check the logs for more details";
136                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
137                         }
138                         statusCollection.add(feInfo);
139
140                         HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
141                         if (!HealthMonitor.isDatabaseUp()) {
142                                 overallStatus = false;
143                                 dbInfo.healthCheckStatus = statusDown;
144                                 dbInfo.description = "Check the logs for more details";
145                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
146                         }
147
148                         if (!HealthMonitor.isClusterStatusOk()) {
149                                 dbInfo.dbClusterStatus = "Problem, check the logs for more details";
150                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
151                         } else {
152                                 dbInfo.dbClusterStatus = statusOk;
153                         }
154
155                         if (!HealthMonitor.isDatabasePermissionsOk()) {
156                                 dbInfo.dbPermissions = "Problem, check the logs for more details";
157                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
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
166                                 // critical as DB. Hence commenting
167                                 // overallStatus = false;
168                                 uebInfo.healthCheckStatus = statusDown;
169                                 uebInfo.description = "Check the logs for more details";
170                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError);
171                         }
172                         statusCollection.add(uebInfo);
173
174                         String json = "";
175                         try {
176                                 json = new Gson().toJson(statusCollection);
177                         } catch (Exception e) {
178                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
179                         }
180                         logger.info(EELFLoggerDelegate.debugLogger, json);
181
182                         if (overallStatus) {
183                                 healthStatus = new HealthStatus(200, json);
184                         } else {
185                                 healthStatus = new HealthStatus(500, json);
186                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
187                         }
188                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
189                 } catch (Exception e) {
190                         logger.error(EELFLoggerDelegate.errorLogger,
191                                         "Exception occurred while performing the healthcheck. Details: "
192                                                         + EcompPortalUtils.getStackTrace(e));
193                 }
194
195                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
196
197                 return healthStatus;
198         }
199
200         @RequestMapping(value = {
201                         "/portalApi/healthCheckSuspend" }, method = RequestMethod.GET, produces = "application/json")
202         public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
203                 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
204
205                 HealthMonitor.isSuspended = true;
206                 healthStatus.statusCode = 200;
207
208                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
209                                 response.getStatus());
210
211                 return healthStatus;
212         }
213
214         @RequestMapping(value = {
215                         "/portalApi/healthCheckResume" }, method = RequestMethod.GET, produces = "application/json")
216         public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
217                 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
218
219                 HealthMonitor.isSuspended = false;
220                 healthStatus.statusCode = 200;
221                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
222                                 response.getStatus());
223                 return healthStatus;
224         }
225
226         @RequestMapping(value = { "/portalApi/ping" }, method = RequestMethod.GET, produces = "application/json")
227         public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
228                 HealthStatus healthStatus = new HealthStatus(200, "OK");
229                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());
230
231                 return healthStatus;
232         }
233 }