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