Cleanup sparky-be pom
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / search / filters / FilterProcessor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.search.filters;
22
23 import javax.servlet.http.HttpServletRequest;
24
25 import org.apache.camel.Exchange;
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.sparky.logging.AaiUiMsgs;
29 import org.onap.aai.sparky.logging.util.ServletUtils;
30 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
31
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.google.gson.Gson;
34 import com.google.gson.JsonArray;
35 import com.google.gson.JsonElement;
36 import com.google.gson.JsonObject;
37
38 public class FilterProcessor {
39   
40   private static final Logger LOG = LoggerFactory.getInstance().getLogger(FilterProcessor.class);
41   
42   private ObjectMapper mapper;
43   private FilteredSearchHelper filteredSearchHelper;
44   private Gson converter;
45   
46   public FilterProcessor() {
47     this.mapper = new ObjectMapper();
48     this.converter = new Gson();
49   }
50   
51   public ObjectMapper getMapper() {
52     return mapper;
53   }
54
55   public FilteredSearchHelper getFilteredSearchHelper() {
56     return filteredSearchHelper;
57   }
58   
59   public void setFilteredSearchHelper(FilteredSearchHelper filteredSearchHelper) {
60     this.filteredSearchHelper = filteredSearchHelper;
61   }
62
63   public void getFiltersWithValues(Exchange exchange) {
64     
65     HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
66     ServletUtils.setUpMdcContext(exchange, request);
67     
68     JsonArray viewFiltersQueries = null;
69     boolean wasErrorDuringFilterDiscovery = false;
70     
71     try {
72       String payload = exchange.getIn().getBody(String.class);
73
74       if (payload == null || payload.isEmpty()) {
75         /* Don't throw back an error, just return an empty set */
76         LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, "Request Payload is empty");
77         wasErrorDuringFilterDiscovery = true;
78       } else {
79         JsonObject payloadObj = converter.fromJson(payload, JsonObject.class);
80         String viewName = null;
81         
82         if(payloadObj.has(SparkyConstants.UI_FILTER_VIEW_NAME_PARAMETER)) {
83           JsonElement viewNameElement = payloadObj.get(SparkyConstants.UI_FILTER_VIEW_NAME_PARAMETER);
84           if(!viewNameElement.isJsonNull()) {
85             viewName = viewNameElement.getAsString();
86           }
87         }
88         
89         if (viewName == null || viewName.isEmpty()) {
90           wasErrorDuringFilterDiscovery = true;
91         } else {
92           viewFiltersQueries = filteredSearchHelper.createFilterValueQueries(payload);
93         }
94       }
95     } catch(Exception exc) {
96       LOG.error(AaiUiMsgs.ERROR_GENERIC, "FilterProcessor failed to get filter list due to error = " + exc.getMessage());
97       
98       exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 500);
99       
100       exchange.getOut().setBody(
101           ServletUtils.generateJsonErrorResponse("FilterProcessor failed to get filter list due to error = " + exc.getMessage()),
102           String.class);
103       
104       return;
105     }
106     
107     boolean wasErrorDuringValueSearch = false;
108     if(!wasErrorDuringFilterDiscovery) {
109       try {
110         if(viewFiltersQueries != null && viewFiltersQueries.size() > 0) {
111           String populatedFiltersList = filteredSearchHelper.doFilterEnumeration(viewFiltersQueries);
112
113           exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
114           exchange.getOut().setBody(populatedFiltersList);
115         } else {
116           wasErrorDuringValueSearch = true;
117         }
118       } catch(Exception exc) {
119         LOG.error(AaiUiMsgs.ERROR_GENERIC, "FilterProcessor failed to generate valid unifiedFilterRequest response due to error, " + exc.getMessage());
120
121         exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 500);
122
123         exchange.getOut().setBody(
124             ServletUtils.generateJsonErrorResponse("FilterProcessor failed to generate valid unifiedFilterRequest response due to error = " + exc.getMessage()),
125             String.class);
126         
127         return;
128       }
129     } 
130     
131     // In the case of an error we want to respond with a valid empty response
132     if(wasErrorDuringFilterDiscovery || wasErrorDuringValueSearch) {
133       exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
134       exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
135       exchange.getOut().setBody(UiFiltersEntityConverter.generateEmptyResponse().toString());
136     }
137   }
138   
139 }