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.RequestMapping;
51 import org.springframework.web.bind.annotation.GetMapping;
52 import org.springframework.web.bind.annotation.RequestMethod;
53 import org.springframework.web.bind.annotation.RestController;
54 import org.onap.music.main.MusicUtil;
55 import org.onap.portalapp.controller.EPUnRestrictedBaseController;
56 import org.onap.portalapp.portal.listener.HealthMonitor;
57 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
58 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
59 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
60 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
61 import org.onap.portalapp.portal.utils.EcompPortalUtils;
62 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
63
64 import com.google.gson.Gson;
65
66 /**
67  * This controller processes requests for the health-check feature implemented
68  * in the HealthMonitor, which runs in its own thread. These requests do not
69  * require any authentication nor an active user session.
70  */
71 @RestController
72 @org.springframework.context.annotation.Configuration
73 @EnableAspectJAutoProxy
74 @EPAuditLog
75 public class HealthCheckController extends EPUnRestrictedBaseController {
76
77         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
78
79         private class HealthStatus {
80                 public int statusCode;
81                 @SuppressWarnings("unused")
82                 public String body;
83
84                 public HealthStatus(int code, String body) {
85                         this.statusCode = code;
86                         this.body = body;
87                 }
88         }
89
90         private class HealthStatusInfo {
91                 HealthStatusInfo(String healthCheckComponent) {
92                         this.healthCheckComponent = healthCheckComponent;
93                         this.healthCheckStatus = statusUp; // Default value
94                         this.version = "";
95                         this.description = statusOk; // Default value
96                         this.hostName = "";
97                         this.ipAddress = "";
98                         this.dbClusterStatus = "";
99                         this.dbPermissions = "";
100                 }
101
102                 @SuppressWarnings("unused")
103                 public String healthCheckComponent;
104                 @SuppressWarnings("unused")
105                 public String healthCheckStatus;
106                 @SuppressWarnings("unused")
107                 public String version;
108                 @SuppressWarnings("unused")
109                 public String description;
110                 @SuppressWarnings("unused")
111                 public String hostName;
112                 @SuppressWarnings("unused")
113                 public String ipAddress;
114                 @SuppressWarnings("unused")
115                 public String dbClusterStatus;
116                 @SuppressWarnings("unused")
117                 public String dbPermissions;
118         }
119
120         private final String statusUp = "UP";
121         private final String statusDown = "DOWN";
122         private final String statusOk = "OK";
123
124         @GetMapping(value = { "/portalApi/healthCheck" }, produces = "application/json")
125         public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
126                 HealthStatus healthStatus = new HealthStatus(500, "");
127
128                 // Return the status as 500 if it suspended due to manual fail over
129                 if (HealthMonitor.isSuspended()) {
130                         healthStatus.body = "Suspended";
131                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
132                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE,
133                                         Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
134                         return healthStatus;
135                 }
136
137                 try {
138                         boolean overallStatus = true;
139
140                         List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
141
142                         HealthStatusInfo beInfo = new HealthStatusInfo("BE");
143                         beInfo.hostName = EcompPortalUtils.getMyHostName();
144                         beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
145                         if (!HealthMonitor.isBackEndUp()) {
146                                 overallStatus = false;
147                                 beInfo.healthCheckStatus = statusDown;
148                                 beInfo.description = "Check the logs for more details";
149                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
150                         }
151                         statusCollection.add(beInfo);
152
153                         HealthStatusInfo feInfo = new HealthStatusInfo("FE");
154                         if (!HealthMonitor.isFrontEndUp()) {
155                                 overallStatus = false;
156                                 feInfo.healthCheckStatus = statusDown;
157                                 feInfo.description = "Check the logs for more details";
158                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
159                         }
160                         statusCollection.add(feInfo);
161
162                         HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
163                         if (!HealthMonitor.isDatabaseUp()) {
164                                 overallStatus = false;
165                                 dbInfo.healthCheckStatus = statusDown;
166                                 dbInfo.description = "Check the logs for more details";
167                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
168                         }
169
170 //                      if (!HealthMonitor.isClusterStatusOk()) {
171 //                              dbInfo.dbClusterStatus = "Problem, check the logs for more details";
172 //                              EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
173 //                      } else {
174 //                              dbInfo.dbClusterStatus = statusOk;
175 //                      }
176
177                         if (!HealthMonitor.isDbPermissionsOk()) {
178                                 dbInfo.dbPermissions = "Problem, check the logs for more details";
179                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
180                         } else {
181                                 dbInfo.dbPermissions = statusOk;
182                         }
183                         statusCollection.add(dbInfo);
184
185                         if(org.onap.portalapp.music.util.MusicUtil.isMusicEnable()){
186                                 HealthStatusInfo CassandraStatusInfo = new HealthStatusInfo("Music-Cassandra");
187                                 //CassandraStatusInfo.hostName = EcompPortalUtils.getMyHostName();
188                                 CassandraStatusInfo.ipAddress = MusicUtil.getMyCassaHost();
189                                 
190                                 if (!HealthMonitor.isCassandraStatusOk()) {
191                                         overallStatus = false;
192                                         CassandraStatusInfo.healthCheckStatus = statusDown;
193                                         CassandraStatusInfo.description = "Check the logs for more details";
194                                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckCassandraError);
195                                 }
196                                 statusCollection.add(CassandraStatusInfo);
197
198                                 /*
199                                  * HealthStatusInfo zookeeperStatusInfo = new
200                                  * HealthStatusInfo("Music-zookeeper"); --zookeeperStatusInfo.hostName =
201                                  * EcompPortalUtils.getMyHostName(); zookeeperStatusInfo.ipAddress =
202                                  * MusicUtil.getMyZkHost(); if (!HealthMonitor.isZookeeperStatusOk()) {
203                                  * overallStatus = false; zookeeperStatusInfo.healthCheckStatus = statusDown;
204                                  * zookeeperStatusInfo.description = "Check the logs for more details";
205                                  * EPLogUtil.logEcompError(logger,
206                                  * EPAppMessagesEnum.MusicHealthCheckZookeeperError); }
207                                  * statusCollection.add(zookeeperStatusInfo);
208                                  */
209                         }
210
211                         String json = "";
212                         try {
213                                 json = new Gson().toJson(statusCollection);
214                         } catch (Exception e) {
215                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
216                         }
217                         logger.info(EELFLoggerDelegate.debugLogger, json);
218
219                         if (overallStatus) {
220                                 healthStatus = new HealthStatus(200, json);
221                         } else {
222                                 healthStatus = new HealthStatus(500, json);
223                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
224                         }
225                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
226                 } catch (Exception e) {
227                         logger.error(EELFLoggerDelegate.errorLogger,    "healthCheck failed", e);
228                 }
229
230                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
231
232                 return healthStatus;
233         }
234
235         @GetMapping(value = {
236                         "/portalApi/healthCheckSuspend" }, produces = "application/json")
237         public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
238                 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
239
240                 HealthMonitor.setSuspended(true);
241                 healthStatus.statusCode = 200;
242
243                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
244                                 response.getStatus());
245
246                 return healthStatus;
247         }
248
249         @GetMapping(value = {
250                         "/portalApi/healthCheckResume" }, produces = "application/json")
251         public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
252                 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
253
254                 HealthMonitor.setSuspended(false);
255                 healthStatus.statusCode = 200;
256                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
257                                 response.getStatus());
258                 return healthStatus;
259         }
260
261         @GetMapping(value = { "/portalApi/ping" }, produces = "application/json")
262         public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
263                 HealthStatus healthStatus = new HealthStatus(200, "OK");
264                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());
265
266                 return healthStatus;
267         }
268 }