CSIT Fix for SDC-2585
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / api / ESGenericSearchDAO.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.dao.api;
22
23 import org.elasticsearch.action.search.SearchRequestBuilder;
24 import org.elasticsearch.action.search.SearchResponse;
25 import org.elasticsearch.index.query.QueryBuilder;
26 import org.elasticsearch.search.sort.SortBuilder;
27 import org.openecomp.sdc.be.dao.es.ElasticSearchClient;
28
29 import javax.annotation.Resource;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
35  * Elastic search dao that manages search operations.
36  *
37  * @author luc boutier
38  */
39 public class ESGenericSearchDAO extends ESGenericIdDAO implements IGenericSearchDAO {
40
41         private static final int MAX_SEARCH_SIZE = 1000;
42
43         @Resource(name = "elasticsearch-client")
44         private ElasticSearchClient esClient;
45
46         @Override
47         public long count(String indexName, String typeName, QueryBuilder query) {
48
49                 SearchRequestBuilder searchRequestBuilder = esClient.getClient().prepareSearch(indexName).setTypes(typeName)
50                                 .setSize(0);
51                 if (query != null) {
52                         searchRequestBuilder.setQuery(query);
53                 }
54
55                 SearchResponse response = searchRequestBuilder.execute().actionGet();
56                 if (!somethingFound(response)) {
57                         return 0;
58                 } else {
59                         return response.getHits().getTotalHits();
60                 }
61         }
62
63         /**
64          * Convert a SearchResponse into a list of objects (json deserialization.)
65          *
66          * @param searchResponse
67          *            The actual search response from elastic-search.
68          * @param clazz
69          *            The type of objects to de-serialize.
70          * @return A list of instances that contains de-serialized data.
71          */
72         public <T> List<T> toGetListOfData(SearchResponse searchResponse, Class<T> clazz) {
73                 // return null if no data has been found in elastic search.
74                 if (!somethingFound(searchResponse)) {
75                         return null;
76                 }
77
78                 List<T> result = new ArrayList<>();
79
80                 for (int i = 0; i < searchResponse.getHits().getHits().length; i++) {
81                         try {
82                                 result.add(getJsonMapper().readValue(searchResponse.getHits().getAt(i).getSourceAsString(), clazz));
83                         } catch (IOException e) {
84                                 throw new RuntimeException(e);
85                         }
86                 }
87
88                 return result;
89         }
90
91         public <T> List<T> doCustomFind(Class<T> clazz, String indexName, String typeName, QueryBuilder query,
92                         SortBuilder sortBuilder) {
93
94                 List<T> result = new ArrayList<>();
95                 SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(indexName).setTypes(typeName)
96                                 .setSize(MAX_SEARCH_SIZE);
97                 if (query != null) {
98                         searchRequestBuilder.setQuery(query);
99                 }
100                 if (sortBuilder != null) {
101                         searchRequestBuilder.addSort(sortBuilder);
102                 }
103                 SearchResponse response = searchRequestBuilder.execute().actionGet();
104                 if (!somethingFound(response)) {
105                         return null;
106                 } else {
107                         for (int i = 0; i < response.getHits().getHits().length; i++) {
108                                 String hit = response.getHits().getAt(i).sourceAsString();
109
110                                 T val = null;
111                                 try {
112                                         val = getJsonMapper().readValue(hit, clazz);
113                                         result.add(val);
114                                 } catch (IOException e) {
115                                         throw new RuntimeException(e);
116                                 }
117                         }
118                         return result;
119                 }
120         }
121
122         private boolean somethingFound(final SearchResponse searchResponse) {
123                 if (searchResponse == null || searchResponse.getHits() == null || searchResponse.getHits().getHits() == null
124                                 || searchResponse.getHits().getHits().length == 0) {
125                         return false;
126                 }
127                 return true;
128         }
129
130 }