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.dal.ElasticSearchAdapter;
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;
38 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
42 * Performs all Elasticsearch related queries for filters related to
47 public class FilterElasticSearchAdapter {
49 private static final Logger LOG = LoggerFactory.getInstance().getLogger(FilterElasticSearchAdapter.class);
50 private static final String AGGS = "aggregations";
51 private static final String CONTAINER = "default";
52 private static final String BUCKETS = "buckets";
53 private static final String FILTER_VALUE_KEY = "key";
54 private ElasticSearchAdapter elasticSearchAdapter;
56 public FilterElasticSearchAdapter(ElasticSearchAdapter elasticSearchAdapter) {
57 this.elasticSearchAdapter = elasticSearchAdapter;
61 * For a given UiFilterEntity, will attempt to contact an Elasticsearch instance
62 * and fetch all possible values for filter's field name.
64 * @param filter - Filter object against which the search will take place.
65 * @param sourceData - If present, contains the index name and field value to search against.
66 * @return - A List of strings if results were found, else empty list.
68 public List<String> fetchValuesForFilter(UiFilterEntity filter, UiFilterDataSourceConfig dataSourceConfig) {
69 ArrayList<String> filterValues = new ArrayList<String>();
71 if(dataSourceConfig != null) {
72 JsonObject filterValueQuery = null;
73 if(dataSourceConfig.getPathToField() != null) {
74 filterValueQuery = FilterQueryBuilder.createNestedFilterValueQueryObject(dataSourceConfig.getFieldName(), dataSourceConfig.getPathToField());
76 filterValueQuery = FilterQueryBuilder.createFilterValueQueryObject(dataSourceConfig.getFieldName());
79 OperationResult opResult = elasticSearchAdapter.doPost(
80 elasticSearchAdapter.buildElasticSearchUrlForApi(dataSourceConfig.getIndexName(),
81 SparkyConstants.ES_SEARCH_API),
82 filterValueQuery.toString(), MediaType.APPLICATION_JSON_TYPE);
84 String result = opResult.getResult();
85 if(opResult.wasSuccessful() && result != null) {
86 JSONObject responseJson = new JSONObject(result);
87 JSONObject aggJson = responseJson.getJSONObject(AGGS);
89 JSONObject containerJson = null;
90 if(dataSourceConfig.getPathToField() != null) {
91 JSONObject nestedContainer = aggJson.getJSONObject(dataSourceConfig.getPathToField());
92 containerJson = nestedContainer.getJSONObject(dataSourceConfig.getFieldName());
94 containerJson = aggJson.getJSONObject(CONTAINER);
97 JSONArray buckets = containerJson.getJSONArray(BUCKETS);
99 int bucketLength = buckets.length();
100 for(int i = 0; i < bucketLength; i++) {
101 JSONObject filterBucket = buckets.getJSONObject(i);
103 String filterValue = filterBucket.getString(FILTER_VALUE_KEY);
104 if(filterValue != null && !filterValue.isEmpty()) {
105 filterValues.add(filterValue);
109 LOG.error(AaiUiMsgs.ERROR_FETCHING_FILTER_VALUES, String.valueOf(opResult.getResultCode()), filter.getFilterName());
112 filterValues.sort(String::compareToIgnoreCase);