2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.aggregatevnf.search;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.json.JsonObject;
29 import javax.ws.rs.core.MediaType;
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;
46 import com.fasterxml.jackson.databind.ObjectMapper;
48 public class AggregateVnfSearchProvider implements SearchProvider {
50 private static final Logger LOG = LoggerFactory.getInstance().getLogger(AggregateVnfSearchProvider.class);
52 private ObjectMapper mapper;
53 private ElasticSearchAdapter elasticSearchAdapter = null;
54 private String autoSuggestIndexName;
55 private String vnfSearchSuggestionRoute;
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;
65 public void setAutoSuggestIndexName(String autoSuggestIndexName) {
66 this.autoSuggestIndexName = autoSuggestIndexName;
70 public List<SearchSuggestion> search(QuerySearchEntity queryRequest) {
72 List<SearchSuggestion> returnList = new ArrayList<SearchSuggestion>();
76 /* Create suggestions query */
77 JsonObject vnfSearch = VnfSearchQueryBuilder.createSuggestionsQuery(String.valueOf(queryRequest.getMaxResults()), queryRequest.getQueryStr());
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);
85 String result = opResult.getResult();
87 if (!opResult.wasSuccessful()) {
88 LOG.error(AaiUiMsgs.ERROR_PARSING_JSON_PAYLOAD_VERBOSE, result);
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")));
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);
116 returnList.add(responseSuggestion);
120 } catch (Exception exc) {
121 LOG.error(AaiUiMsgs.ERROR_GENERIC, "Search failed due to error = " + exc.getMessage());