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 java.util.ArrayList;
24 import java.util.List;
26 import javax.json.JsonObject;
27 import javax.ws.rs.core.MediaType;
29 import org.json.JSONArray;
30 import org.json.JSONObject;
31 import org.onap.aai.cl.api.Logger;
32 import org.onap.aai.cl.eelf.LoggerFactory;
33 import org.onap.aai.restclient.client.OperationResult;
34 import org.onap.aai.sparky.search.SearchServiceAdapter;
35 import org.onap.aai.sparky.logging.AaiUiMsgs;
36 import org.onap.aai.sparky.search.filters.config.UiFilterDataSourceConfig;
37 import org.onap.aai.sparky.search.filters.entity.UiFilterEntity;
41 * Performs all Elasticsearch related queries for filters related to
46 public class FilterElasticSearchAdapter {
48 private static final Logger LOG = LoggerFactory.getInstance().getLogger(FilterElasticSearchAdapter.class);
49 private static final String AGGS = "aggregations";
50 private static final String CONTAINER = "default";
51 private static final String BUCKETS = "buckets";
52 private static final String FILTER_VALUE_KEY = "key";
53 private SearchServiceAdapter searchServiceAdapter;
55 public FilterElasticSearchAdapter(SearchServiceAdapter searchServiceAdapter) {
56 this.searchServiceAdapter = searchServiceAdapter;
60 * For a given UiFilterEntity, will attempt to contact an Elasticsearch instance
61 * and fetch all possible values for filter's field name.
63 * @param filter - Filter object against which the search will take place.
64 * @param sourceData - If present, contains the index name and field value to search against.
65 * @return - A List of strings if results were found, else empty list.
67 public List<String> fetchValuesForFilter(UiFilterEntity filter, UiFilterDataSourceConfig dataSourceConfig) {
68 ArrayList<String> filterValues = new ArrayList<String>();
70 if(dataSourceConfig != null) {
71 JsonObject filterValueQuery = null;
72 if(dataSourceConfig.getPathToField() != null) {
73 filterValueQuery = FilterQueryBuilder.createNestedFilterValueQueryObject(dataSourceConfig.getFieldName(), dataSourceConfig.getPathToField());
75 filterValueQuery = FilterQueryBuilder.createFilterValueQueryObject(dataSourceConfig.getFieldName());
78 OperationResult opResult = searchServiceAdapter.doPost(
79 searchServiceAdapter.buildSearchServiceQueryUrl(dataSourceConfig.getIndexName()),
80 filterValueQuery.toString(), "application/json");
82 String result = opResult.getResult();
83 if(opResult.wasSuccessful() && result != null) {
84 JSONObject responseJson = new JSONObject(result);
85 JSONObject aggJson = responseJson.getJSONObject(AGGS);
87 JSONObject containerJson = null;
88 if(dataSourceConfig.getPathToField() != null) {
89 JSONObject nestedContainer = aggJson.getJSONObject(dataSourceConfig.getPathToField());
90 containerJson = nestedContainer.getJSONObject(dataSourceConfig.getFieldName());
92 containerJson = aggJson.getJSONObject(CONTAINER);
95 JSONArray buckets = containerJson.getJSONArray(BUCKETS);
97 int bucketLength = buckets.length();
98 for(int i = 0; i < bucketLength; i++) {
99 JSONObject filterBucket = buckets.getJSONObject(i);
101 String filterValue = filterBucket.getString(FILTER_VALUE_KEY);
102 if(filterValue != null && !filterValue.isEmpty()) {
103 filterValues.add(filterValue);
107 LOG.error(AaiUiMsgs.ERROR_FETCHING_FILTER_VALUES, String.valueOf(opResult.getResultCode()), filter.getFilterName());
110 filterValues.sort(String::compareToIgnoreCase);