2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.filters;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.impl.ComponentsUtils;
28 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
29 import org.openecomp.sdc.common.api.Constants;
30 import org.openecomp.sdc.common.api.HealthCheckInfo;
31 import org.openecomp.sdc.common.api.HealthCheckInfo.HealthCheckStatus;
32 import org.openecomp.sdc.common.log.enums.LogLevel;
33 import org.openecomp.sdc.common.log.enums.Severity;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
35 import org.openecomp.sdc.common.log.wrappers.LoggerSdcAudit;
36 import org.openecomp.sdc.exception.ResponseFormat;
37 import org.springframework.web.context.WebApplicationContext;
39 import javax.annotation.Priority;
40 import javax.servlet.ServletContext;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.ws.rs.container.ContainerRequestContext;
43 import javax.ws.rs.container.ContainerRequestFilter;
44 import javax.ws.rs.core.Context;
45 import javax.ws.rs.core.Response;
46 import javax.ws.rs.core.Response.ResponseBuilder;
47 import javax.ws.rs.core.Response.Status;
48 import java.io.IOException;
49 import java.util.ArrayList;
50 import java.util.List;
53 public class ComponentsAvailabilityFilter implements ContainerRequestFilter {
55 private static LoggerSdcAudit audit = new LoggerSdcAudit(ComponentsAvailabilityFilter.class);
58 protected HttpServletRequest sr;
59 protected Gson gson = new GsonBuilder().setPrettyPrinting().create();
60 private static final Logger log = Logger.getLogger(ComponentsAvailabilityFilter.class);
63 public void filter(ContainerRequestContext requestContext) throws IOException {
65 audit.startLog(requestContext);
67 String requestUrl = requestContext.getUriInfo().getPath();
68 if (!"healthCheck".equals(requestUrl)) {
69 List<HealthCheckInfo> beHealthCheckInfos = getBeHealthCheckInfos(this.sr.getSession().getServletContext());
70 ActionStatus status = getAggregateBeStatus(beHealthCheckInfos);
72 if (!status.equals(ActionStatus.OK)) {
73 log.error("Components Availability Filter Failed - ES/Cassandra is DOWN");
74 availabilityError(requestContext);
80 protected ActionStatus getAggregateBeStatus(List<HealthCheckInfo> beHealthCheckInfos) {
81 ActionStatus status = ActionStatus.OK;
82 for (HealthCheckInfo healthCheckInfo : beHealthCheckInfos) {
83 if (healthCheckInfo.getHealthCheckStatus().equals(HealthCheckStatus.DOWN)) {
84 status = ActionStatus.GENERAL_ERROR;
91 protected List<HealthCheckInfo> getBeHealthCheckInfos(ServletContext servletContext) {
93 List<HealthCheckInfo> healthCheckInfos = new ArrayList<>();
94 HealthCheckBusinessLogic healthCheckBusinessLogic = getHealthCheckBL(servletContext);
95 healthCheckBusinessLogic.getJanusGraphHealthCheck(healthCheckInfos); // JanusGraph
96 return healthCheckInfos;
99 protected ComponentsUtils getComponentsUtils() {
100 ServletContext context = sr.getSession().getServletContext();
101 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
102 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
103 return webApplicationContext.getBean(ComponentsUtils.class);
106 protected void availabilityError(ContainerRequestContext requestContext) {
107 ComponentsUtils componentUtils = getComponentsUtils();
108 if (componentUtils == null) {
109 String message = "Components Availability Filter Failed to get component utils.";
110 abortWith(requestContext, message, Response.status(Status.INTERNAL_SERVER_ERROR).build());
112 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
113 ResponseBuilder responseBuilder = Response.status(responseFormat.getStatus());
114 Response response = responseBuilder.entity(gson.toJson(responseFormat.getRequestError())).build();
115 abortWith(requestContext, responseFormat.getRequestError().toString(), response);
118 private HealthCheckBusinessLogic getHealthCheckBL(ServletContext context) {
119 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
120 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
121 return webApplicationContext.getBean(HealthCheckBusinessLogic.class);
125 private void abortWith(ContainerRequestContext requestContext, String message, Response response) {
127 audit.log(sr.getRemoteAddr(),
129 response.getStatusInfo(),
136 requestContext.abortWith(response);