2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.search.filters;
27 import java.util.ArrayList;
28 import java.util.List;
30 import javax.json.JsonObject;
31 import javax.ws.rs.core.MediaType;
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.onap.aai.cl.api.Logger;
36 import org.onap.aai.cl.eelf.LoggerFactory;
37 import org.onap.aai.restclient.client.OperationResult;
38 import org.onap.aai.sparky.dal.ElasticSearchAdapter;
39 import org.onap.aai.sparky.logging.AaiUiMsgs;
40 import org.onap.aai.sparky.search.filters.config.UiFilterDataSourceConfig;
41 import org.onap.aai.sparky.search.filters.entity.UiFilterEntity;
42 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
46 * Performs all Elasticsearch related queries for filters related to
51 public class FilterElasticSearchAdapter {
53 private static final Logger LOG = LoggerFactory.getInstance().getLogger(FilterElasticSearchAdapter.class);
54 private static final String AGGS = "aggregations";
55 private static final String CONTAINER = "default";
56 private static final String BUCKETS = "buckets";
57 private static final String FILTER_VALUE_KEY = "key";
58 private ElasticSearchAdapter elasticSearchAdapter;
60 public FilterElasticSearchAdapter(ElasticSearchAdapter elasticSearchAdapter) {
61 this.elasticSearchAdapter = elasticSearchAdapter;
65 * For a given UiFilterEntity, will attempt to contact an Elasticsearch instance
66 * and fetch all possible values for filter's field name.
68 * @param filter - Filter object against which the search will take place.
69 * @param sourceData - If present, contains the index name and field value to search against.
70 * @return - A List of strings if results were found, else empty list.
72 public List<String> fetchValuesForFilter(UiFilterEntity filter, UiFilterDataSourceConfig dataSourceConfig) {
73 ArrayList<String> filterValues = new ArrayList<String>();
75 if(dataSourceConfig != null) {
76 JsonObject filterValueQuery = null;
77 if(dataSourceConfig.getPathToField() != null) {
78 filterValueQuery = FilterQueryBuilder.createNestedFilterValueQueryObject(dataSourceConfig.getFieldName(), dataSourceConfig.getPathToField());
80 filterValueQuery = FilterQueryBuilder.createFilterValueQueryObject(dataSourceConfig.getFieldName());
83 OperationResult opResult = elasticSearchAdapter.doPost(
84 elasticSearchAdapter.buildElasticSearchUrlForApi(dataSourceConfig.getIndexName(),
85 SparkyConstants.ES_SEARCH_API),
86 filterValueQuery.toString(), MediaType.APPLICATION_JSON_TYPE);
88 String result = opResult.getResult();
89 if(opResult.wasSuccessful() && result != null) {
90 JSONObject responseJson = new JSONObject(result);
91 JSONObject aggJson = responseJson.getJSONObject(AGGS);
93 JSONObject containerJson = null;
94 if(dataSourceConfig.getPathToField() != null) {
95 JSONObject nestedContainer = aggJson.getJSONObject(dataSourceConfig.getPathToField());
96 containerJson = nestedContainer.getJSONObject(dataSourceConfig.getFieldName());
98 containerJson = aggJson.getJSONObject(CONTAINER);
101 JSONArray buckets = containerJson.getJSONArray(BUCKETS);
103 int bucketLength = buckets.length();
104 for(int i = 0; i < bucketLength; i++) {
105 JSONObject filterBucket = buckets.getJSONObject(i);
107 String filterValue = filterBucket.getString(FILTER_VALUE_KEY);
108 if(filterValue != null && !filterValue.isEmpty()) {
109 filterValues.add(filterValue);
113 LOG.error(AaiUiMsgs.ERROR_FETCHING_FILTER_VALUES, String.valueOf(opResult.getResultCode()), filter.getFilterName());
116 filterValues.sort(String::compareToIgnoreCase);