Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / HealthCheckController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.controller;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.slf4j.MDC;
47 import org.springframework.context.annotation.EnableAspectJAutoProxy;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestMethod;
50 import org.springframework.web.bind.annotation.RestController;
51
52 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.openecomp.portalapp.controller.EPUnRestrictedBaseController;
54 import org.openecomp.portalapp.portal.listener.HealthMonitor;
55 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
56 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
57 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
58 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
59 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
60 import com.google.gson.Gson;
61
62 /**
63  * This controller processes requests for the health-check feature implemented
64  * in the HealthMonitor, which runs in its own thread. These requests do not
65  * require any authentication nor an active user session.
66  */
67 @RestController
68 @org.springframework.context.annotation.Configuration
69 @EnableAspectJAutoProxy
70 @EPAuditLog
71 public class HealthCheckController extends EPUnRestrictedBaseController {
72
73         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
74
75         private class HealthStatus {
76                 public int statusCode;
77                 @SuppressWarnings("unused")
78                 public String body;
79
80                 public HealthStatus(int code, String body) {
81                         this.statusCode = code;
82                         this.body = body;
83                 }
84         }
85
86         private class HealthStatusInfo {
87                 HealthStatusInfo(String healthCheckComponent) {
88                         this.healthCheckComponent = healthCheckComponent;
89                         this.healthCheckStatus = statusUp; // Default value
90                         this.version = "";
91                         this.description = statusOk; // Default value
92                         this.hostName = "";
93                         this.ipAddress = "";
94                         this.dbClusterStatus = "";
95                         this.dbPermissions = "";
96                 }
97
98                 @SuppressWarnings("unused")
99                 public String healthCheckComponent;
100                 @SuppressWarnings("unused")
101                 public String healthCheckStatus;
102                 @SuppressWarnings("unused")
103                 public String version;
104                 @SuppressWarnings("unused")
105                 public String description;
106                 @SuppressWarnings("unused")
107                 public String hostName;
108                 @SuppressWarnings("unused")
109                 public String ipAddress;
110                 @SuppressWarnings("unused")
111                 public String dbClusterStatus;
112                 @SuppressWarnings("unused")
113                 public String dbPermissions;
114         }
115
116         private final String statusUp = "UP";
117         private final String statusDown = "DOWN";
118         private final String statusOk = "OK";
119
120         @RequestMapping(value = { "/portalApi/healthCheck" }, method = RequestMethod.GET, produces = "application/json")
121         public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
122                 HealthStatus healthStatus = new HealthStatus(500, "");
123
124                 // Return the status as 500 if it suspended due to manual fail over
125                 if (HealthMonitor.isSuspended) {
126                         healthStatus.body = "Suspended";
127                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
128                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE,
129                                         Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
130                         return healthStatus;
131                 }
132
133                 try {
134                         boolean overallStatus = true;
135
136                         List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
137
138                         HealthStatusInfo beInfo = new HealthStatusInfo("BE");
139                         beInfo.hostName = EcompPortalUtils.getMyHostName();
140                         beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
141                         if (!HealthMonitor.isBackEndUp()) {
142                                 overallStatus = false;
143                                 beInfo.healthCheckStatus = statusDown;
144                                 beInfo.description = "Check the logs for more details";
145                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
146                         }
147                         statusCollection.add(beInfo);
148
149                         HealthStatusInfo feInfo = new HealthStatusInfo("FE");
150                         if (!HealthMonitor.isFrontEndUp()) {
151                                 overallStatus = false;
152                                 feInfo.healthCheckStatus = statusDown;
153                                 feInfo.description = "Check the logs for more details";
154                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
155                         }
156                         statusCollection.add(feInfo);
157
158                         HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
159                         if (!HealthMonitor.isDatabaseUp()) {
160                                 overallStatus = false;
161                                 dbInfo.healthCheckStatus = statusDown;
162                                 dbInfo.description = "Check the logs for more details";
163                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
164                         }
165
166                         if (!HealthMonitor.isClusterStatusOk()) {
167                                 dbInfo.dbClusterStatus = "Problem, check the logs for more details";
168                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
169                         } else {
170                                 dbInfo.dbClusterStatus = statusOk;
171                         }
172
173                         if (!HealthMonitor.isDatabasePermissionsOk()) {
174                                 dbInfo.dbPermissions = "Problem, check the logs for more details";
175                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
176                         } else {
177                                 dbInfo.dbPermissions = statusOk;
178                         }
179                         statusCollection.add(dbInfo);
180
181                         HealthStatusInfo uebInfo = new HealthStatusInfo("UEB");
182                         if (!HealthMonitor.isUebUp()) {
183                                 // As per test case review meeting, UEB is considered as
184                                 // critical as DB. Hence commenting
185                                 // overallStatus = false;
186                                 uebInfo.healthCheckStatus = statusDown;
187                                 uebInfo.description = "Check the logs for more details";
188                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError);
189                         }
190                         statusCollection.add(uebInfo);
191
192                         String json = "";
193                         try {
194                                 json = new Gson().toJson(statusCollection);
195                         } catch (Exception e) {
196                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
197                         }
198                         logger.info(EELFLoggerDelegate.debugLogger, json);
199
200                         if (overallStatus) {
201                                 healthStatus = new HealthStatus(200, json);
202                         } else {
203                                 healthStatus = new HealthStatus(500, json);
204                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
205                         }
206                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
207                 } catch (Exception e) {
208                         logger.error(EELFLoggerDelegate.errorLogger,    "healthCheck failed", e);
209                 }
210
211                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
212
213                 return healthStatus;
214         }
215
216         @RequestMapping(value = {
217                         "/portalApi/healthCheckSuspend" }, method = RequestMethod.GET, produces = "application/json")
218         public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
219                 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
220
221                 HealthMonitor.isSuspended = true;
222                 healthStatus.statusCode = 200;
223
224                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
225                                 response.getStatus());
226
227                 return healthStatus;
228         }
229
230         @RequestMapping(value = {
231                         "/portalApi/healthCheckResume" }, method = RequestMethod.GET, produces = "application/json")
232         public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
233                 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
234
235                 HealthMonitor.isSuspended = false;
236                 healthStatus.statusCode = 200;
237                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
238                                 response.getStatus());
239                 return healthStatus;
240         }
241
242         @RequestMapping(value = { "/portalApi/ping" }, method = RequestMethod.GET, produces = "application/json")
243         public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
244                 HealthStatus healthStatus = new HealthStatus(200, "OK");
245                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());
246
247                 return healthStatus;
248         }
249 }