2 * ============LICENSE_START=======================================================
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
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 package org.onap.aai.sparky.search.filters;
23 import org.onap.aai.cl.api.Logger;
24 import org.onap.aai.cl.eelf.LoggerFactory;
25 import org.onap.aai.restclient.client.OperationResult;
26 import org.onap.aai.sparky.logging.AaiUiMsgs;
27 import org.onap.aai.sparky.search.SearchServiceAdapter;
28 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
29 import org.onap.aai.sparky.search.filters.searchservice.FilterQueryAndResponseBuilder;
30 import org.onap.aai.sparky.search.filters.searchservice.FilterQueryAndResponseBuilder.FileBasedFiltersConstants;
31 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
33 import com.google.gson.Gson;
34 import com.google.gson.GsonBuilder;
35 import com.google.gson.JsonArray;
36 import com.google.gson.JsonElement;
37 import com.google.gson.JsonObject;
39 public class FilteredSearchHelper {
40 private static final Logger LOG = LoggerFactory.getInstance().getLogger(FilteredSearchHelper.class);
42 private SearchServiceAdapter searchServiceAdapter;
43 private FilterQueryAndResponseBuilder filterQueryAndResponseBuilder;
44 private Gson responseFormatter;
46 public FilteredSearchHelper(FiltersConfig filterConfig, SearchServiceAdapter searchServiceAdapter) {
47 this.filterQueryAndResponseBuilder = new FilterQueryAndResponseBuilder(filterConfig);
48 this.searchServiceAdapter = searchServiceAdapter;
49 this.responseFormatter = new GsonBuilder().disableHtmlEscaping().create();
52 public void setFiltersConfig(FiltersConfig filtersConfig) {
53 this.filterQueryAndResponseBuilder.setFiltersConfig(filtersConfig);
56 public JsonArray createFilterValueQueries(String fePayload) {
57 return filterQueryAndResponseBuilder.createFileBasedFilterValueQueries(fePayload);
60 public String doFilterEnumeration(JsonArray filterQueries) {
61 String formattedResult = "";
62 JsonObject populatedFilters = new JsonObject();
64 for(JsonElement queryElement : filterQueries) {
65 JsonObject queryObj = queryElement.getAsJsonObject();
66 String filterId = queryObj.get(FileBasedFiltersConstants.FILTER_ID).getAsString();
68 if(queryObj.has(FileBasedFiltersConstants.FILTER_VALUE_QUERY)) {
70 JsonObject filterQuery = queryObj.get(FileBasedFiltersConstants.FILTER_VALUE_QUERY).getAsJsonObject();
71 String query = responseFormatter.toJson(filterQuery);
73 String index = queryObj.get(FileBasedFiltersConstants.INDEX).getAsString();
74 String searchUrl = searchServiceAdapter.buildSearchServiceUrlForApi(index, SparkyConstants.SS_QUERY_API);
76 OperationResult opResult = searchServiceAdapter.doPost(searchUrl, query);
77 if(opResult.wasSuccessful()) {
78 String result = opResult.getResult();
79 populatedFilters = filterQueryAndResponseBuilder.formatSingleFilterValueQueryResult(result, filterId, populatedFilters);
81 LOG.warn(AaiUiMsgs.WARN_GENERIC, "Filter values query failed with code " + opResult.getResultCode() + " for filter with ID " + filterId);
82 populatedFilters = filterQueryAndResponseBuilder.formatSingleFilterValueQueryResult(null, filterId, populatedFilters);
85 // If there is no query, then populate filter with data from file
86 populatedFilters = filterQueryAndResponseBuilder.formatSingleFilterValueQueryResult(null, filterId, populatedFilters);
90 JsonObject finalResponse = new JsonObject();
91 finalResponse.add(FileBasedFiltersConstants.FILTERS, populatedFilters);
92 formattedResult = responseFormatter.toJson(finalResponse);
95 return formattedResult;