Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / search / filters / FilterElasticSearchAdapter.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.search.filters;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import javax.json.JsonObject;
31 import javax.ws.rs.core.MediaType;
32
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;
43
44
45 /**
46  * Performs all Elasticsearch related queries for filters related to
47  * the Sparky-FE.
48  * 
49  * @author RICHARV
50  */
51 public class FilterElasticSearchAdapter {
52   
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;
59   
60   public FilterElasticSearchAdapter(ElasticSearchAdapter elasticSearchAdapter) {
61     this.elasticSearchAdapter = elasticSearchAdapter;
62   }
63   
64   /**
65    * For a given UiFilterEntity, will attempt to contact an Elasticsearch instance
66    * and fetch all possible values for filter's field name.
67    * 
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.
71    */
72   public List<String> fetchValuesForFilter(UiFilterEntity filter, UiFilterDataSourceConfig dataSourceConfig) {
73     ArrayList<String> filterValues = new ArrayList<String>();
74     
75     if(dataSourceConfig != null) {
76       JsonObject filterValueQuery = null;
77       if(dataSourceConfig.getPathToField() != null) {
78         filterValueQuery = FilterQueryBuilder.createNestedFilterValueQueryObject(dataSourceConfig.getFieldName(), dataSourceConfig.getPathToField());
79       } else {
80         filterValueQuery = FilterQueryBuilder.createFilterValueQueryObject(dataSourceConfig.getFieldName());
81       }
82       
83       OperationResult opResult = elasticSearchAdapter.doPost(
84           elasticSearchAdapter.buildElasticSearchUrlForApi(dataSourceConfig.getIndexName(),
85               SparkyConstants.ES_SEARCH_API),
86           filterValueQuery.toString(), MediaType.APPLICATION_JSON_TYPE);
87       
88       String result = opResult.getResult();
89       if(opResult.wasSuccessful() && result != null) {
90         JSONObject responseJson = new JSONObject(result);
91         JSONObject aggJson = responseJson.getJSONObject(AGGS);
92         
93         JSONObject containerJson = null;
94         if(dataSourceConfig.getPathToField() != null) {
95           JSONObject nestedContainer = aggJson.getJSONObject(dataSourceConfig.getPathToField());
96           containerJson = nestedContainer.getJSONObject(dataSourceConfig.getFieldName());
97         } else {
98           containerJson = aggJson.getJSONObject(CONTAINER);
99         }
100         
101         JSONArray buckets = containerJson.getJSONArray(BUCKETS);
102         
103         int bucketLength = buckets.length();
104         for(int i = 0; i < bucketLength; i++) {
105           JSONObject filterBucket = buckets.getJSONObject(i);
106           
107           String filterValue = filterBucket.getString(FILTER_VALUE_KEY);
108           if(filterValue != null && !filterValue.isEmpty()) {
109             filterValues.add(filterValue);
110           }
111         }
112       } else {
113         LOG.error(AaiUiMsgs.ERROR_FETCHING_FILTER_VALUES, String.valueOf(opResult.getResultCode()), filter.getFilterName());
114       }
115     }
116     filterValues.sort(String::compareToIgnoreCase);
117     return filterValues;
118   }
119 }