2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.aggregatevnf.search;
27 import java.util.ArrayList;
28 import java.util.List;
30 import javax.json.JsonObject;
31 import javax.ws.rs.core.MediaType;
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.common.search.CommonSearchSuggestion;
39 import org.onap.aai.sparky.dal.ElasticSearchAdapter;
40 import org.onap.aai.sparky.logging.AaiUiMsgs;
41 import org.onap.aai.sparky.search.api.SearchProvider;
42 import org.onap.aai.sparky.search.entity.QuerySearchEntity;
43 import org.onap.aai.sparky.search.entity.SearchSuggestion;
44 import org.onap.aai.sparky.search.filters.entity.UiFilterValueEntity;
45 import org.onap.aai.sparky.util.NodeUtils;
46 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
48 import com.fasterxml.jackson.databind.ObjectMapper;
50 public class AggregateVnfSearchProvider implements SearchProvider {
52 private static final Logger LOG = LoggerFactory.getInstance().getLogger(AggregateVnfSearchProvider.class);
54 private ObjectMapper mapper;
55 private ElasticSearchAdapter elasticSearchAdapter = null;
56 private String autoSuggestIndexName;
57 private String vnfSearchSuggestionRoute;
59 public AggregateVnfSearchProvider(ElasticSearchAdapter elasticSearchAdapter,
60 String autoSuggestIndexName, String vnfSearchSuggestionRoute) {
61 mapper = new ObjectMapper();
62 this.elasticSearchAdapter = elasticSearchAdapter;
63 this.autoSuggestIndexName = autoSuggestIndexName;
64 this.vnfSearchSuggestionRoute = vnfSearchSuggestionRoute;
67 public void setAutoSuggestIndexName(String autoSuggestIndexName) {
68 this.autoSuggestIndexName = autoSuggestIndexName;
72 public List<SearchSuggestion> search(QuerySearchEntity queryRequest) {
74 List<SearchSuggestion> returnList = new ArrayList<SearchSuggestion>();
78 /* Create suggestions query */
79 JsonObject vnfSearch = VnfSearchQueryBuilder.createSuggestionsQuery(String.valueOf(queryRequest.getMaxResults()), queryRequest.getQueryStr());
81 /* Parse suggestions response */
82 OperationResult opResult = elasticSearchAdapter.doPost(
83 elasticSearchAdapter.buildElasticSearchUrlForApi(autoSuggestIndexName,
84 SparkyConstants.ES_SUGGEST_API),
85 vnfSearch.toString(), MediaType.APPLICATION_JSON_TYPE);
87 String result = opResult.getResult();
89 if (!opResult.wasSuccessful()) {
90 LOG.error(AaiUiMsgs.ERROR_PARSING_JSON_PAYLOAD_VERBOSE, result);
94 JSONObject responseJson = new JSONObject(result);
95 String suggestionsKey = "vnfs";
96 JSONArray suggestionsArray = new JSONArray();
97 JSONArray suggestions = responseJson.getJSONArray(suggestionsKey);
98 if (suggestions.length() > 0) {
99 suggestionsArray = suggestions.getJSONObject(0).getJSONArray("options");
100 for (int i = 0; i < suggestionsArray.length(); i++) {
101 JSONObject querySuggestion = suggestionsArray.getJSONObject(i);
102 if (querySuggestion != null) {
103 CommonSearchSuggestion responseSuggestion = new CommonSearchSuggestion();
104 responseSuggestion.setText(querySuggestion.getString("text"));
105 responseSuggestion.setRoute(vnfSearchSuggestionRoute);
106 responseSuggestion.setHashId(NodeUtils.generateUniqueShaDigest(querySuggestion.getString("text")));
108 // Extract filter list from JSON and add to response suggestion
109 JSONObject payload = querySuggestion.getJSONObject("payload");
110 if (payload.length() > 0) {
111 JSONArray filterList = payload.getJSONArray("filterList");
112 for (int filter = 0; filter < filterList.length(); filter++) {
113 String filterValueString = filterList.getJSONObject(filter).toString();
114 UiFilterValueEntity filterValue = mapper.readValue(filterValueString, UiFilterValueEntity.class);
115 responseSuggestion.getFilterValues().add(filterValue);
118 returnList.add(responseSuggestion);
122 } catch (Exception exc) {
123 LOG.error(AaiUiMsgs.ERROR_GENERIC, "Search failed due to error = " + exc.getMessage());