2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.search.filters;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.json.JsonObject;
30 import org.apache.camel.Exchange;
31 import org.apache.camel.component.restlet.RestletConstants;
32 import org.onap.aai.cl.api.Logger;
33 import org.onap.aai.cl.eelf.LoggerFactory;
34 import org.onap.aai.sparky.logging.AaiUiMsgs;
35 import org.onap.aai.sparky.search.filters.entity.UiFilterEntity;
36 import org.onap.aai.sparky.search.filters.entity.UiFiltersEntity;
37 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
38 import org.restlet.Request;
39 import org.restlet.Response;
40 import org.restlet.data.MediaType;
41 import org.restlet.data.Status;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.ObjectMapper;
46 public class FilterProcessor {
48 private static final Logger LOG = LoggerFactory.getInstance().getLogger(FilterProcessor.class);
50 private ObjectMapper mapper;
51 private FilteredSearchHelper filteredSearchHelper;
53 public FilterProcessor() {
54 this.mapper = new ObjectMapper();
57 public ObjectMapper getMapper() {
61 public FilteredSearchHelper getFilteredSearchHelper() {
62 return filteredSearchHelper;
65 public void setFilteredSearchHelper(FilteredSearchHelper filteredSearchHelper) {
66 this.filteredSearchHelper = filteredSearchHelper;
69 public void getFiltersWithValues(Exchange exchange) {
70 Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
72 Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
74 /* Disables automatic Apache Camel Restlet component logging which prints out an undesirable log entry
75 which includes client (e.g. browser) information */
76 request.setLoggable(false);
78 UiFiltersEntity viewFiltersList = null;
79 boolean wasErrorDuringFilterDiscovery = false;
82 String payload = exchange.getIn().getBody(String.class);
84 if (payload == null || payload.isEmpty()) {
85 /* Don't throw back an error, just return an empty set */
86 LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, "Request Payload is empty");
87 wasErrorDuringFilterDiscovery = true;
89 String viewName = mapper.readValue(payload, JsonNode.class).get(SparkyConstants.UI_FILTER_VIEW_NAME_PARAMETER).asText();
91 if (viewName == null || viewName.isEmpty()) {
92 wasErrorDuringFilterDiscovery = true;
94 viewFiltersList = filteredSearchHelper.doFilterDiscovery(viewName);
97 } catch(Exception exc) {
98 LOG.error(AaiUiMsgs.ERROR_GENERIC, "FilterProcessor failed to get filter list due to error = " + exc.getMessage());
99 wasErrorDuringFilterDiscovery = true;
102 boolean wasErrorDuringValueSearch = false;
103 if(!wasErrorDuringFilterDiscovery) {
105 if(!viewFiltersList.getFilters().isEmpty()) {
106 List<String> filterIds = new ArrayList<String>();
108 for(UiFilterEntity filterEntity : viewFiltersList.getFilters()) {
109 filterIds.add(filterEntity.getFilterId());
112 UiFiltersEntity responseFiltersList = filteredSearchHelper.doFilterEnumeration(filterIds);
114 JsonObject finalResponse = UiFiltersEntityConverter.convertUiFiltersEntityToUnifiedFilterResponse(responseFiltersList);
116 response.setStatus(Status.SUCCESS_OK);
117 response.setEntity(finalResponse.toString(), MediaType.APPLICATION_JSON);
118 exchange.getOut().setBody(response);
120 wasErrorDuringValueSearch = true;
122 } catch(Exception exc) {
123 LOG.error(AaiUiMsgs.ERROR_GENERIC, "FilterProcessor failed to generate valid unifiedFilterRequest response due to error, " + exc.getMessage());
124 response.setStatus(Status.SERVER_ERROR_INTERNAL);
128 // In the case of an error we want to respond with a valid empty response
129 if(wasErrorDuringFilterDiscovery || wasErrorDuringValueSearch) {
130 response.setStatus(Status.SUCCESS_OK);
131 response.setEntity(UiFiltersEntityConverter.generateEmptyResponse().toString(), MediaType.APPLICATION_JSON);
132 exchange.getOut().setBody(response);