X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fsparky%2Faggregatevnf%2Fsearch%2FAggregateVnfSearchProvider.java;h=5fd0dc97c88f16fc4e9e36d9988c53b284e81179;hb=ef7f255958e541ffaec0fd2a977440dd7b6fd6b8;hp=6ff779d39b4418995a860eda078537f7e9f2ac20;hpb=9eb9e258f0ab06a516f6d88d1aedc36fc22aeeaf;p=aai%2Fsparky-be.git diff --git a/src/main/java/org/onap/aai/sparky/aggregatevnf/search/AggregateVnfSearchProvider.java b/src/main/java/org/onap/aai/sparky/aggregatevnf/search/AggregateVnfSearchProvider.java index 6ff779d..5fd0dc9 100644 --- a/src/main/java/org/onap/aai/sparky/aggregatevnf/search/AggregateVnfSearchProvider.java +++ b/src/main/java/org/onap/aai/sparky/aggregatevnf/search/AggregateVnfSearchProvider.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017 Amdocs + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,51 +17,58 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. */ package org.onap.aai.sparky.aggregatevnf.search; +import java.io.IOException; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; -import javax.json.JsonObject; -import javax.ws.rs.core.MediaType; - -import org.json.JSONArray; -import org.json.JSONObject; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.restclient.client.OperationResult; import org.onap.aai.sparky.common.search.CommonSearchSuggestion; -import org.onap.aai.sparky.dal.ElasticSearchAdapter; import org.onap.aai.sparky.logging.AaiUiMsgs; +import org.onap.aai.sparky.search.SearchServiceAdapter; import org.onap.aai.sparky.search.api.SearchProvider; import org.onap.aai.sparky.search.entity.QuerySearchEntity; import org.onap.aai.sparky.search.entity.SearchSuggestion; import org.onap.aai.sparky.search.filters.entity.UiFilterValueEntity; import org.onap.aai.sparky.util.NodeUtils; -import org.onap.aai.sparky.viewandinspect.config.SparkyConstants; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; public class AggregateVnfSearchProvider implements SearchProvider { - - private static final Logger LOG = LoggerFactory.getInstance().getLogger(AggregateVnfSearchProvider.class); + + private static final Logger LOG = + LoggerFactory.getInstance().getLogger(AggregateVnfSearchProvider.class); private ObjectMapper mapper; - private ElasticSearchAdapter elasticSearchAdapter = null; + private SearchServiceAdapter searchServiceAdapter = null; private String autoSuggestIndexName; private String vnfSearchSuggestionRoute; - public AggregateVnfSearchProvider(ElasticSearchAdapter elasticSearchAdapter, + private static final String AUTO_SUGGEST_TEMPLATE = "{ " + "\"results-size\": %d," + + "\"suggest-text\": \"%s\"," + "\"suggest-field\": \"%s\"" + "}"; + + private static final String KEY_SEARCH_RESULT = "searchResult"; + private static final String KEY_HITS = "hits"; + private static final String KEY_DOCUMENT = "document"; + private static final String KEY_CONTENT = "content"; + private static final String KEY_TEXT = "text"; + private static final String KEY_FILTER_LIST = "filterList"; + + public AggregateVnfSearchProvider(SearchServiceAdapter searchServiceAdapter, String autoSuggestIndexName, String vnfSearchSuggestionRoute) { mapper = new ObjectMapper(); - this.elasticSearchAdapter = elasticSearchAdapter; + this.searchServiceAdapter = searchServiceAdapter; this.autoSuggestIndexName = autoSuggestIndexName; this.vnfSearchSuggestionRoute = vnfSearchSuggestionRoute; } - + public void setAutoSuggestIndexName(String autoSuggestIndexName) { this.autoSuggestIndexName = autoSuggestIndexName; } @@ -70,58 +77,96 @@ public class AggregateVnfSearchProvider implements SearchProvider { public List search(QuerySearchEntity queryRequest) { List returnList = new ArrayList(); - try { - /* Create suggestions query */ - JsonObject vnfSearch = VnfSearchQueryBuilder.createSuggestionsQuery(String.valueOf(queryRequest.getMaxResults()), queryRequest.getQueryStr()); + final String fullUrlStr = + searchServiceAdapter.buildSuggestServiceQueryUrl(autoSuggestIndexName); + String postBody = + String.format(AUTO_SUGGEST_TEMPLATE, Integer.parseInt(queryRequest.getMaxResults()), + queryRequest.getQueryStr(), "entity_suggest"); + OperationResult opResult = + searchServiceAdapter.doPost(fullUrlStr, postBody, "application/json"); + if (opResult.getResultCode() == 200) { + returnList = generateSuggestionsForSearchResponse(opResult.getResult()); + } else { + LOG.error(AaiUiMsgs.ERROR_PARSING_JSON_PAYLOAD_VERBOSE, opResult.getResult()); + return returnList; + } + } catch (Exception exc) { + LOG.error(AaiUiMsgs.ERROR_GENERIC, "Search failed due to error = " + exc.getMessage()); + } - /* Parse suggestions response */ - OperationResult opResult = elasticSearchAdapter.doPost( - elasticSearchAdapter.buildElasticSearchUrlForApi(autoSuggestIndexName, - SparkyConstants.ES_SUGGEST_API), - vnfSearch.toString(), MediaType.APPLICATION_JSON_TYPE); + return returnList; + } - String result = opResult.getResult(); + private List generateSuggestionsForSearchResponse(String operationResult) { - if (!opResult.wasSuccessful()) { - LOG.error(AaiUiMsgs.ERROR_PARSING_JSON_PAYLOAD_VERBOSE, result); - return returnList; - } + if (operationResult == null || operationResult.length() == 0) { + return null; + } + + ObjectMapper mapper = new ObjectMapper(); + JsonNode rootNode = null; + List suggestionEntityList = new ArrayList(); - JSONObject responseJson = new JSONObject(result); - String suggestionsKey = "vnfs"; - JSONArray suggestionsArray = new JSONArray(); - JSONArray suggestions = responseJson.getJSONArray(suggestionsKey); - if (suggestions.length() > 0) { - suggestionsArray = suggestions.getJSONObject(0).getJSONArray("options"); - for (int i = 0; i < suggestionsArray.length(); i++) { - JSONObject querySuggestion = suggestionsArray.getJSONObject(i); - if (querySuggestion != null) { - CommonSearchSuggestion responseSuggestion = new CommonSearchSuggestion(); - responseSuggestion.setText(querySuggestion.getString("text")); - responseSuggestion.setRoute(vnfSearchSuggestionRoute); - responseSuggestion.setHashId(NodeUtils.generateUniqueShaDigest(querySuggestion.getString("text"))); - - // Extract filter list from JSON and add to response suggestion - JSONObject payload = querySuggestion.getJSONObject("payload"); - if (payload.length() > 0) { - JSONArray filterList = payload.getJSONArray("filterList"); - for (int filter = 0; filter < filterList.length(); filter++) { - String filterValueString = filterList.getJSONObject(filter).toString(); - UiFilterValueEntity filterValue = mapper.readValue(filterValueString, UiFilterValueEntity.class); - responseSuggestion.getFilterValues().add(filterValue); - } + try { + rootNode = mapper.readTree(operationResult); + JsonNode hitsNode = rootNode.get(KEY_SEARCH_RESULT); + // Check if there are hits that are coming back + if (hitsNode.has(KEY_HITS)) { + ArrayNode hitsArray = (ArrayNode) hitsNode.get(KEY_HITS); + + /* + * next we iterate over the values in the hit array elements + */ + Iterator nodeIterator = hitsArray.elements(); + JsonNode entityNode = null; + CommonSearchSuggestion responseSuggestion = null; + JsonNode sourceNode = null; + + while (nodeIterator.hasNext()) { + entityNode = nodeIterator.next(); + String responseText = getValueFromNode(entityNode, KEY_TEXT); + // do the point transformation as we build the response? + responseSuggestion = new CommonSearchSuggestion(); + responseSuggestion.setRoute(vnfSearchSuggestionRoute); + responseSuggestion.setText(responseText); + responseSuggestion.setHashId(NodeUtils.generateUniqueShaDigest(responseText)); + + sourceNode = entityNode.get(KEY_DOCUMENT).get(KEY_CONTENT); + if (sourceNode.has(KEY_FILTER_LIST)) { + ArrayNode filtersArray = (ArrayNode) sourceNode.get(KEY_FILTER_LIST); + for (int i = 0; i < filtersArray.size(); i++) { + String filterValueString = filtersArray.get(i).toString(); + UiFilterValueEntity filterValue = + mapper.readValue(filterValueString, UiFilterValueEntity.class); + responseSuggestion.getFilterValues().add(filterValue); } - returnList.add(responseSuggestion); } + suggestionEntityList.add(responseSuggestion); } } - } catch (Exception exc) { - LOG.error(AaiUiMsgs.ERROR_GENERIC, "Search failed due to error = " + exc.getMessage()); + } catch (IOException exc) { + LOG.warn(AaiUiMsgs.SEARCH_RESPONSE_BUILDING_EXCEPTION, exc.getLocalizedMessage()); } + return suggestionEntityList; + + } + + private String getValueFromNode(JsonNode node, String fieldName) { + + if (node == null || fieldName == null) { + return null; + } + + JsonNode valueNode = node.get(fieldName); + + if (valueNode != null) { + return valueNode.asText(); + } + + return null; - return returnList; } - + }