nexus site path corrected
[portal.git] / ecomp-portal-BE / 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.openecomp.portalapp.controller.EPRestrictedBaseController;
29 import org.openecomp.portalapp.portal.listener.HealthMonitor;
30 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
31 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
32 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
33 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
34 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
36 import org.slf4j.MDC;
37 import org.springframework.context.annotation.EnableAspectJAutoProxy;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RequestMethod;
40 import org.springframework.web.bind.annotation.RestController;
41
42 import com.google.gson.Gson;
43
44 /*
45  * This controller checks the health status and returns JSON response.
46  * It does not require 
47  */
48 @RestController
49 @org.springframework.context.annotation.Configuration
50 @EnableAspectJAutoProxy
51 @EPAuditLog
52 public class HealthCheckController extends EPRestrictedBaseController {
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(EPSystemProperties.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(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(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(EPAppMessagesEnum.BeDaoSystemError);
144                         }
145                                         
146                         if (!HealthMonitor.isClusterStatusOk()) {
147                                 dbInfo.dbClusterStatus = "Problem, check the logs for more details";
148                                 EPLogUtil.logEcompError(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(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                                 uebInfo.healthCheckStatus = statusDown;
166                                 uebInfo.description = "Check the logs for more details";
167                                 EPLogUtil.logEcompError(EPAppMessagesEnum.BeUebConnectionError);
168                         }
169                         statusCollection.add(uebInfo);
170
171                         String json = "";
172                         try {
173                                 json = new Gson().toJson(statusCollection);
174                         } catch (Exception e) {
175                                 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
176                                 EPLogUtil.logEcompError(EPAppMessagesEnum.BeInvalidJsonInput);
177                         }
178                         logger.info(EELFLoggerDelegate.debugLogger, json);
179
180                         if (overallStatus) {
181                                 healthStatus = new HealthStatus(200, json);
182                         } else {
183                                 healthStatus = new HealthStatus(500, json);
184                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
185                         }
186                         MDC.put(EPSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
187                 } catch (Exception e) {
188                         logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the healthcheck. Details: "
189                                         + EcompPortalUtils.getStackTrace(e));
190                 }
191                 
192                 EcompPortalUtils.logAndSerializeObject("/portalApi/healthCheck", "GET result =", response.getStatus());
193                 
194                 return healthStatus;
195         }
196
197         @RequestMapping(value = {
198                         "/portalApi/healthCheckSuspend" }, method = RequestMethod.GET, produces = "application/json")
199         public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
200                 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
201
202                 HealthMonitor.isSuspended = true;
203                 healthStatus.statusCode = 200;
204                 
205                 EcompPortalUtils.logAndSerializeObject("/portalApi/healthCheckSuspend", "GET result =",
206                                                                                                                                                 response.getStatus());
207                 
208                 return healthStatus;
209         }
210
211         @RequestMapping(value = {
212                         "/portalApi/healthCheckResume" }, method = RequestMethod.GET, produces = "application/json")
213         public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
214                 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
215
216                 HealthMonitor.isSuspended = false;
217                 healthStatus.statusCode = 200;
218                 EcompPortalUtils.logAndSerializeObject("/portalApi/healthCheckResume", "GET result =",
219                                                                                                                                                         response.getStatus());
220                 return healthStatus;
221         }
222         
223         
224
225         @RequestMapping(value = {
226                         "/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("/portalApi/ping", "GET result =",
230                                 response.getStatus());
231                 
232                 return healthStatus;
233         }
234 }