707f907ce66eb8d2d79fa188491ff49ffdb3a7ea
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / search / filters / FilterElasticSearchAdapter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sparky.search.filters;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.json.JsonObject;
27 import javax.ws.rs.core.MediaType;
28
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;
39
40
41 /**
42  * Performs all Elasticsearch related queries for filters related to
43  * the Sparky-FE.
44  * 
45  * @author RICHARV
46  */
47 public class FilterElasticSearchAdapter {
48   
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;
55   
56   public FilterElasticSearchAdapter(ElasticSearchAdapter elasticSearchAdapter) {
57     this.elasticSearchAdapter = elasticSearchAdapter;
58   }
59   
60   /**
61    * For a given UiFilterEntity, will attempt to contact an Elasticsearch instance
62    * and fetch all possible values for filter's field name.
63    * 
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.
67    */
68   public List<String> fetchValuesForFilter(UiFilterEntity filter, UiFilterDataSourceConfig dataSourceConfig) {
69     ArrayList<String> filterValues = new ArrayList<String>();
70     
71     if(dataSourceConfig != null) {
72       JsonObject filterValueQuery = null;
73       if(dataSourceConfig.getPathToField() != null) {
74         filterValueQuery = FilterQueryBuilder.createNestedFilterValueQueryObject(dataSourceConfig.getFieldName(), dataSourceConfig.getPathToField());
75       } else {
76         filterValueQuery = FilterQueryBuilder.createFilterValueQueryObject(dataSourceConfig.getFieldName());
77       }
78       
79       OperationResult opResult = elasticSearchAdapter.doPost(
80           elasticSearchAdapter.buildElasticSearchUrlForApi(dataSourceConfig.getIndexName(),
81               SparkyConstants.ES_SEARCH_API),
82           filterValueQuery.toString(), MediaType.APPLICATION_JSON_TYPE);
83       
84       String result = opResult.getResult();
85       if(opResult.wasSuccessful() && result != null) {
86         JSONObject responseJson = new JSONObject(result);
87         JSONObject aggJson = responseJson.getJSONObject(AGGS);
88         
89         JSONObject containerJson = null;
90         if(dataSourceConfig.getPathToField() != null) {
91           JSONObject nestedContainer = aggJson.getJSONObject(dataSourceConfig.getPathToField());
92           containerJson = nestedContainer.getJSONObject(dataSourceConfig.getFieldName());
93         } else {
94           containerJson = aggJson.getJSONObject(CONTAINER);
95         }
96         
97         JSONArray buckets = containerJson.getJSONArray(BUCKETS);
98         
99         int bucketLength = buckets.length();
100         for(int i = 0; i < bucketLength; i++) {
101           JSONObject filterBucket = buckets.getJSONObject(i);
102           
103           String filterValue = filterBucket.getString(FILTER_VALUE_KEY);
104           if(filterValue != null && !filterValue.isEmpty()) {
105             filterValues.add(filterValue);
106           }
107         }
108       } else {
109         LOG.error(AaiUiMsgs.ERROR_FETCHING_FILTER_VALUES, String.valueOf(opResult.getResultCode()), filter.getFilterName());
110       }
111     }
112     filterValues.sort(String::compareToIgnoreCase);
113     return filterValues;
114   }
115 }