Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / filters / ComponentsAvailabilityFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.filters;
22
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.exception.ResponseFormat;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.web.context.WebApplicationContext;
36
37 import javax.annotation.Priority;
38 import javax.servlet.ServletContext;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.container.ContainerRequestContext;
41 import javax.ws.rs.container.ContainerRequestFilter;
42 import javax.ws.rs.core.Context;
43 import javax.ws.rs.core.Response;
44 import javax.ws.rs.core.Response.ResponseBuilder;
45 import javax.ws.rs.core.Response.Status;
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.List;
49
50 @Priority(11)
51 public class ComponentsAvailabilityFilter implements ContainerRequestFilter {
52
53     @Context
54     protected HttpServletRequest sr;
55     protected Gson gson = new GsonBuilder().setPrettyPrinting().create();
56     private static final Logger log = LoggerFactory.getLogger(ComponentsAvailabilityFilter.class);
57
58     @Override
59     public void filter(ContainerRequestContext requestContext) throws IOException {
60
61         String requestUrl = requestContext.getUriInfo().getPath();
62         if (!requestUrl.equals("healthCheck")) {
63             List<HealthCheckInfo> beHealthCheckInfos = getBeHealthCheckInfos(this.sr.getSession().getServletContext());
64             ActionStatus status = getAggregateBeStatus(beHealthCheckInfos);
65
66             if (!status.equals(ActionStatus.OK)) {
67                 log.error("Components Availability Filter Failed - ES/Cassandra is DOWN");
68                 availabilityError(requestContext);
69             }
70         }
71
72     }
73
74     protected ActionStatus getAggregateBeStatus(List<HealthCheckInfo> beHealthCheckInfos) {
75         ActionStatus status = ActionStatus.OK;
76         for (HealthCheckInfo healthCheckInfo : beHealthCheckInfos) {
77             if (healthCheckInfo.getHealthCheckStatus().equals(HealthCheckStatus.DOWN)) {
78                 status = ActionStatus.GENERAL_ERROR;
79                 break;
80             }
81         }
82         return status;
83     }
84
85     protected List<HealthCheckInfo> getBeHealthCheckInfos(ServletContext servletContext) {
86
87         List<HealthCheckInfo> healthCheckInfos = new ArrayList<HealthCheckInfo>();
88         HealthCheckBusinessLogic healthCheckBusinessLogic = getHealthCheckBL(servletContext);
89         healthCheckBusinessLogic.getTitanHealthCheck(healthCheckInfos); // Titan
90         return healthCheckInfos;
91     }
92
93     protected ComponentsUtils getComponentsUtils() {
94         ServletContext context = sr.getSession().getServletContext();
95         WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
96         WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
97         ComponentsUtils componentsUtils = webApplicationContext.getBean(ComponentsUtils.class);
98         return componentsUtils;
99     }
100
101     protected void availabilityError(ContainerRequestContext requestContext) {
102         ComponentsUtils componentUtils = getComponentsUtils();
103         if (componentUtils == null) {
104             log.error("Components Availability Filter Failed to get component utils.");
105             requestContext.abortWith(Response.status(Status.INTERNAL_SERVER_ERROR).build());
106         }
107         ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
108         ResponseBuilder responseBuilder = Response.status(responseFormat.getStatus());
109         Response response = responseBuilder.entity(gson.toJson(responseFormat.getRequestError())).build();
110         requestContext.abortWith(response);
111     }
112
113     private HealthCheckBusinessLogic getHealthCheckBL(ServletContext context) {
114         WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
115         WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
116         HealthCheckBusinessLogic healthCheckBl = webApplicationContext.getBean(HealthCheckBusinessLogic.class);
117         return healthCheckBl;
118     }
119
120 }