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.onap.logging.ref.slf4j.ONAPLogConstants;
26 import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
30 import org.openecomp.sdc.common.api.Constants;
31 import org.openecomp.sdc.common.api.HealthCheckInfo;
32 import org.openecomp.sdc.common.api.HealthCheckInfo.HealthCheckStatus;
33 import org.openecomp.sdc.common.log.enums.LogLevel;
34 import org.openecomp.sdc.common.log.enums.Severity;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36 import org.openecomp.sdc.common.log.wrappers.LoggerSdcAudit;
37 import org.openecomp.sdc.exception.ResponseFormat;
38 import org.slf4j.MarkerFactory;
39 import org.springframework.web.context.WebApplicationContext;
41 import javax.annotation.Priority;
42 import javax.servlet.ServletContext;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.ws.rs.container.ContainerRequestContext;
45 import javax.ws.rs.container.ContainerRequestFilter;
46 import javax.ws.rs.core.Context;
47 import javax.ws.rs.core.Response;
48 import javax.ws.rs.core.Response.ResponseBuilder;
49 import javax.ws.rs.core.Response.Status;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.List;
55 public class ComponentsAvailabilityFilter implements ContainerRequestFilter {
57 private static LoggerSdcAudit audit = new LoggerSdcAudit(ComponentsAvailabilityFilter.class);
60 protected HttpServletRequest sr;
61 protected Gson gson = new GsonBuilder().setPrettyPrinting().create();
62 private static final Logger log = Logger.getLogger(ComponentsAvailabilityFilter.class);
65 public void filter(ContainerRequestContext requestContext) throws IOException {
67 audit.startLog(requestContext);
69 String requestUrl = requestContext.getUriInfo().getPath();
70 if (!"healthCheck".equals(requestUrl)) {
71 List<HealthCheckInfo> beHealthCheckInfos = getBeHealthCheckInfos(this.sr.getSession().getServletContext());
72 ActionStatus status = getAggregateBeStatus(beHealthCheckInfos);
74 if (!status.equals(ActionStatus.OK)) {
75 log.error("Components Availability Filter Failed - ES/Cassandra is DOWN");
76 availabilityError(requestContext);
82 protected ActionStatus getAggregateBeStatus(List<HealthCheckInfo> beHealthCheckInfos) {
83 ActionStatus status = ActionStatus.OK;
84 for (HealthCheckInfo healthCheckInfo : beHealthCheckInfos) {
85 if (healthCheckInfo.getHealthCheckStatus().equals(HealthCheckStatus.DOWN)) {
86 status = ActionStatus.GENERAL_ERROR;
93 protected List<HealthCheckInfo> getBeHealthCheckInfos(ServletContext servletContext) {
95 List<HealthCheckInfo> healthCheckInfos = new ArrayList<>();
96 HealthCheckBusinessLogic healthCheckBusinessLogic = getHealthCheckBL(servletContext);
97 healthCheckInfos.add(healthCheckBusinessLogic.getJanusGraphHealthCheck());
98 return healthCheckInfos;
101 protected ComponentsUtils getComponentsUtils() {
102 ServletContext context = sr.getSession().getServletContext();
103 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
104 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
105 return webApplicationContext.getBean(ComponentsUtils.class);
108 protected void availabilityError(ContainerRequestContext requestContext) {
109 ComponentsUtils componentUtils = getComponentsUtils();
110 if (componentUtils == null) {
111 String message = "Components Availability Filter Failed to get component utils.";
112 abortWith(requestContext, message, Response.status(Status.INTERNAL_SERVER_ERROR).build());
114 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
115 ResponseBuilder responseBuilder = Response.status(responseFormat.getStatus());
116 Response response = responseBuilder.entity(gson.toJson(responseFormat.getRequestError())).build();
117 abortWith(requestContext, responseFormat.getRequestError().toString(), response);
120 private HealthCheckBusinessLogic getHealthCheckBL(ServletContext context) {
121 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
122 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
123 return webApplicationContext.getBean(HealthCheckBusinessLogic.class);
127 private void abortWith(ContainerRequestContext requestContext, String message, Response response) {
129 audit.logExit(sr.getRemoteAddr(),
131 response.getStatusInfo(),
135 MarkerFactory.getMarker(ONAPLogConstants.Markers.EXIT.getName()));
139 requestContext.abortWith(response);