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