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;
30 import org.json.JSONArray;
31 import org.json.JSONObject;
32 import org.onap.aai.cl.api.Logger;
33 import org.onap.aai.cl.eelf.LoggerFactory;
34 import org.onap.aai.restclient.client.OperationResult;
35 import org.onap.aai.sparky.common.search.CommonSearchSuggestion;
36 import org.onap.aai.sparky.dal.elasticsearch.SearchAdapter;
37 import org.onap.aai.sparky.dataintegrity.config.DiUiConstants;
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.TierSupportUiConstants;
46 import com.fasterxml.jackson.databind.ObjectMapper;
48 public class AggregateVnfSearchProvider implements SearchProvider {
50 private static final Logger LOG =
51 LoggerFactory.getInstance().getLogger(AggregateVnfSearchProvider.class);
53 private ObjectMapper mapper;
54 private static SearchAdapter search = null;
56 private String autoSuggestIndexName;
57 private String elasticSearchIp;
58 private String elatsticSearchPort;
60 public AggregateVnfSearchProvider() {
62 mapper = new ObjectMapper();
66 search = new SearchAdapter();
68 } catch (Exception exc) {
69 LOG.error(AaiUiMsgs.CONFIGURATION_ERROR,
70 "Search Configuration Error. Error = " + exc.getMessage());
74 public void setAutoSuggestIndexName(String autoSuggestIndexName) {
75 this.autoSuggestIndexName = autoSuggestIndexName;
78 public void setElasticSearchIp(String elasticSearchIp) {
79 this.elasticSearchIp = elasticSearchIp;
82 public void setElatsticSearchPort(String elatsticSearchPort) {
83 this.elatsticSearchPort = elatsticSearchPort;
87 * Get Full URL for search using elastic search configuration.
90 * @return the full url
92 private String getFullUrl(String indexName, String api) {
93 final String host = elasticSearchIp;
94 final String port = elatsticSearchPort;
95 return String.format("http://%s:%s/%s/%s", host, port, indexName, api);
99 public List<SearchSuggestion> search(QuerySearchEntity queryRequest) {
101 List<SearchSuggestion> returnList = new ArrayList<SearchSuggestion>();
105 /* Create suggestions query */
106 JsonObject vnfSearch = VnfSearchQueryBuilder.createSuggestionsQuery(
107 String.valueOf(queryRequest.getMaxResults()), queryRequest.getQueryStr());
109 /* Parse suggestions response */
110 OperationResult opResult =
111 search.doPost(getFullUrl(autoSuggestIndexName, TierSupportUiConstants.ES_SUGGEST_API),
112 vnfSearch.toString(), DiUiConstants.APP_JSON);
114 String result = opResult.getResult();
116 if (!opResult.wasSuccessful()) {
117 LOG.error(AaiUiMsgs.ERROR_PARSING_JSON_PAYLOAD_VERBOSE, result);
121 JSONObject responseJson = new JSONObject(result);
122 String suggestionsKey = "vnfs";
123 JSONArray suggestionsArray = new JSONArray();
124 JSONArray suggestions = responseJson.getJSONArray(suggestionsKey);
125 if (suggestions.length() > 0) {
126 suggestionsArray = suggestions.getJSONObject(0).getJSONArray("options");
127 for (int i = 0; i < suggestionsArray.length(); i++) {
128 JSONObject querySuggestion = suggestionsArray.getJSONObject(i);
129 if (querySuggestion != null) {
130 CommonSearchSuggestion responseSuggestion = new CommonSearchSuggestion();
131 responseSuggestion.setText(querySuggestion.getString("text"));
132 responseSuggestion.setRoute("vnfSearch"); // TODO -> Read route from
133 // suggestive-search.properties instead of
136 .setHashId(NodeUtils.generateUniqueShaDigest(querySuggestion.getString("text")));
138 // Extract filter list from JSON and add to response suggestion
139 JSONObject payload = querySuggestion.getJSONObject("payload");
140 if (payload.length() > 0) {
141 JSONArray filterList = payload.getJSONArray("filterList");
142 for (int filter = 0; filter < filterList.length(); filter++) {
143 String filterValueString = filterList.getJSONObject(filter).toString();
144 UiFilterValueEntity filterValue =
145 mapper.readValue(filterValueString, UiFilterValueEntity.class);
146 responseSuggestion.getFilterValues().add(filterValue);
149 returnList.add(responseSuggestion);
153 } catch (Exception exc) {
154 LOG.error(AaiUiMsgs.ERROR_GENERIC, "Search failed due to error = " + exc.getMessage());