6ff779d39b4418995a860eda078537f7e9f2ac20
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / aggregatevnf / search / AggregateVnfSearchProvider.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.aggregatevnf.search;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.json.JsonObject;
29 import javax.ws.rs.core.MediaType;
30
31 import org.json.JSONArray;
32 import org.json.JSONObject;
33 import org.onap.aai.cl.api.Logger;
34 import org.onap.aai.cl.eelf.LoggerFactory;
35 import org.onap.aai.restclient.client.OperationResult;
36 import org.onap.aai.sparky.common.search.CommonSearchSuggestion;
37 import org.onap.aai.sparky.dal.ElasticSearchAdapter;
38 import org.onap.aai.sparky.logging.AaiUiMsgs;
39 import org.onap.aai.sparky.search.api.SearchProvider;
40 import org.onap.aai.sparky.search.entity.QuerySearchEntity;
41 import org.onap.aai.sparky.search.entity.SearchSuggestion;
42 import org.onap.aai.sparky.search.filters.entity.UiFilterValueEntity;
43 import org.onap.aai.sparky.util.NodeUtils;
44 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
45
46 import com.fasterxml.jackson.databind.ObjectMapper;
47
48 public class AggregateVnfSearchProvider implements SearchProvider {
49   
50   private static final Logger LOG = LoggerFactory.getInstance().getLogger(AggregateVnfSearchProvider.class);
51
52   private ObjectMapper mapper;
53   private ElasticSearchAdapter elasticSearchAdapter = null;
54   private String autoSuggestIndexName;
55   private String vnfSearchSuggestionRoute;
56
57   public AggregateVnfSearchProvider(ElasticSearchAdapter elasticSearchAdapter,
58       String autoSuggestIndexName, String vnfSearchSuggestionRoute) {
59     mapper = new ObjectMapper();
60     this.elasticSearchAdapter = elasticSearchAdapter;
61     this.autoSuggestIndexName = autoSuggestIndexName;
62     this.vnfSearchSuggestionRoute = vnfSearchSuggestionRoute;
63   }
64   
65   public void setAutoSuggestIndexName(String autoSuggestIndexName) {
66     this.autoSuggestIndexName = autoSuggestIndexName;
67   }
68
69   @Override
70   public List<SearchSuggestion> search(QuerySearchEntity queryRequest) {
71
72     List<SearchSuggestion> returnList = new ArrayList<SearchSuggestion>();
73
74     try {
75
76       /* Create suggestions query */
77       JsonObject vnfSearch = VnfSearchQueryBuilder.createSuggestionsQuery(String.valueOf(queryRequest.getMaxResults()), queryRequest.getQueryStr());
78
79       /* Parse suggestions response */
80       OperationResult opResult = elasticSearchAdapter.doPost(
81           elasticSearchAdapter.buildElasticSearchUrlForApi(autoSuggestIndexName,
82               SparkyConstants.ES_SUGGEST_API),
83           vnfSearch.toString(), MediaType.APPLICATION_JSON_TYPE);
84
85       String result = opResult.getResult();
86
87       if (!opResult.wasSuccessful()) {
88         LOG.error(AaiUiMsgs.ERROR_PARSING_JSON_PAYLOAD_VERBOSE, result);
89         return returnList;
90       }
91
92       JSONObject responseJson = new JSONObject(result);
93       String suggestionsKey = "vnfs";
94       JSONArray suggestionsArray = new JSONArray();
95       JSONArray suggestions = responseJson.getJSONArray(suggestionsKey);
96       if (suggestions.length() > 0) {
97         suggestionsArray = suggestions.getJSONObject(0).getJSONArray("options");
98         for (int i = 0; i < suggestionsArray.length(); i++) {
99           JSONObject querySuggestion = suggestionsArray.getJSONObject(i);
100           if (querySuggestion != null) {
101             CommonSearchSuggestion responseSuggestion = new CommonSearchSuggestion();
102             responseSuggestion.setText(querySuggestion.getString("text"));
103             responseSuggestion.setRoute(vnfSearchSuggestionRoute);
104             responseSuggestion.setHashId(NodeUtils.generateUniqueShaDigest(querySuggestion.getString("text")));
105
106             // Extract filter list from JSON and add to response suggestion
107             JSONObject payload = querySuggestion.getJSONObject("payload");
108             if (payload.length() > 0) {
109               JSONArray filterList = payload.getJSONArray("filterList");
110               for (int filter = 0; filter < filterList.length(); filter++) {
111                 String filterValueString = filterList.getJSONObject(filter).toString();
112                 UiFilterValueEntity filterValue = mapper.readValue(filterValueString, UiFilterValueEntity.class);
113                 responseSuggestion.getFilterValues().add(filterValue);
114               }
115             }
116             returnList.add(responseSuggestion);
117           }
118         }
119       }
120     } catch (Exception exc) {
121       LOG.error(AaiUiMsgs.ERROR_GENERIC, "Search failed due to error = " + exc.getMessage());
122     }
123
124     return returnList;
125   }
126   
127 }