Upgrade SDC from Titan to Janus Graph
[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.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;
38
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;
51
52 @Priority(11)
53 public class ComponentsAvailabilityFilter implements ContainerRequestFilter {
54
55         private static LoggerSdcAudit audit = new LoggerSdcAudit(ComponentsAvailabilityFilter.class);
56
57     @Context
58     protected HttpServletRequest sr;
59     protected Gson gson = new GsonBuilder().setPrettyPrinting().create();
60     private static final Logger log = Logger.getLogger(ComponentsAvailabilityFilter.class);
61
62     @Override
63     public void filter(ContainerRequestContext requestContext) throws IOException {
64
65                 audit.startLog(requestContext);
66
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);
71
72             if (!status.equals(ActionStatus.OK)) {
73                 log.error("Components Availability Filter Failed - ES/Cassandra is DOWN");
74                 availabilityError(requestContext);
75             }
76         }
77
78     }
79
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;
85                 break;
86             }
87         }
88         return status;
89     }
90
91     protected List<HealthCheckInfo> getBeHealthCheckInfos(ServletContext servletContext) {
92
93         List<HealthCheckInfo> healthCheckInfos = new ArrayList<>();
94         HealthCheckBusinessLogic healthCheckBusinessLogic = getHealthCheckBL(servletContext);
95         healthCheckBusinessLogic.getJanusGraphHealthCheck(healthCheckInfos); // JanusGraph
96         return healthCheckInfos;
97     }
98
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);
104     }
105
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());
111         }
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);
116     }
117
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);
122     }
123
124
125         private void abortWith(ContainerRequestContext requestContext, String message, Response response) {
126
127                 audit.log(sr.getRemoteAddr(),
128                                 requestContext,
129                                 response.getStatusInfo(),
130                                 LogLevel.ERROR,
131                                 Severity.OK,
132                                 message);
133
134                 log.error(message);
135                 audit.clearMyData();
136                 requestContext.abortWith(response);
137         }
138 }