update sync queries to use searh data service
[aai/sparky-be.git] / sparkybe-onap-service / 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.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;
38
39
40 /**
41  * Performs all Elasticsearch related queries for filters related to
42  * the Sparky-FE.
43  * 
44  * @author RICHARV
45  */
46 public class FilterElasticSearchAdapter {
47   
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;
54   
55   public FilterElasticSearchAdapter(SearchServiceAdapter searchServiceAdapter) {
56     this.searchServiceAdapter = searchServiceAdapter;
57   }
58   
59   /**
60    * For a given UiFilterEntity, will attempt to contact an Elasticsearch instance
61    * and fetch all possible values for filter's field name.
62    * 
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.
66    */
67   public List<String> fetchValuesForFilter(UiFilterEntity filter, UiFilterDataSourceConfig dataSourceConfig) {
68     ArrayList<String> filterValues = new ArrayList<String>();
69     
70     if(dataSourceConfig != null) {
71       JsonObject filterValueQuery = null;
72       if(dataSourceConfig.getPathToField() != null) {
73         filterValueQuery = FilterQueryBuilder.createNestedFilterValueQueryObject(dataSourceConfig.getFieldName(), dataSourceConfig.getPathToField());
74       } else {
75         filterValueQuery = FilterQueryBuilder.createFilterValueQueryObject(dataSourceConfig.getFieldName());
76       }
77       
78       OperationResult opResult = searchServiceAdapter.doPost(
79                   searchServiceAdapter.buildSearchServiceQueryUrl(dataSourceConfig.getIndexName()),
80           filterValueQuery.toString(), "application/json");
81       
82       String result = opResult.getResult();
83       if(opResult.wasSuccessful() && result != null) {
84         JSONObject responseJson = new JSONObject(result);
85         JSONObject aggJson = responseJson.getJSONObject(AGGS);
86         
87         JSONObject containerJson = null;
88         if(dataSourceConfig.getPathToField() != null) {
89           JSONObject nestedContainer = aggJson.getJSONObject(dataSourceConfig.getPathToField());
90           containerJson = nestedContainer.getJSONObject(dataSourceConfig.getFieldName());
91         } else {
92           containerJson = aggJson.getJSONObject(CONTAINER);
93         }
94         
95         JSONArray buckets = containerJson.getJSONArray(BUCKETS);
96         
97         int bucketLength = buckets.length();
98         for(int i = 0; i < bucketLength; i++) {
99           JSONObject filterBucket = buckets.getJSONObject(i);
100           
101           String filterValue = filterBucket.getString(FILTER_VALUE_KEY);
102           if(filterValue != null && !filterValue.isEmpty()) {
103             filterValues.add(filterValue);
104           }
105         }
106       } else {
107         LOG.error(AaiUiMsgs.ERROR_FETCHING_FILTER_VALUES, String.valueOf(opResult.getResultCode()), filter.getFilterName());
108       }
109     }
110     filterValues.sort(String::compareToIgnoreCase);
111     return filterValues;
112   }
113 }