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