diff options
Diffstat (limited to 'src/main')
4 files changed, 158 insertions, 98 deletions
diff --git a/src/main/java/org/onap/aai/sparky/aggregatevnf/search/AggregateSummaryProcessor.java b/src/main/java/org/onap/aai/sparky/aggregatevnf/search/AggregateSummaryProcessor.java index 0de028e..941c1e5 100644 --- a/src/main/java/org/onap/aai/sparky/aggregatevnf/search/AggregateSummaryProcessor.java +++ b/src/main/java/org/onap/aai/sparky/aggregatevnf/search/AggregateSummaryProcessor.java @@ -46,32 +46,37 @@ import org.restlet.data.Status; public class AggregateSummaryProcessor { - private static final Logger LOG = LoggerFactory.getInstance().getLogger(AggregateSummaryProcessor.class); + private static final Logger LOG = + LoggerFactory.getInstance().getLogger(AggregateSummaryProcessor.class); private static final String KEY_FILTERS = "filters"; private ElasticSearchAdapter elasticSearchAdapter = null; - + private String vnfAggregationIndexName; private FiltersConfig filtersConfig; - - public AggregateSummaryProcessor(ElasticSearchAdapter elasticSearchAdapter, FiltersConfig filtersConfig) { + + public AggregateSummaryProcessor(ElasticSearchAdapter elasticSearchAdapter, + FiltersConfig filtersConfig) { this.elasticSearchAdapter = elasticSearchAdapter; this.filtersConfig = filtersConfig; } - + public void setVnfAggregationIndexName(String vnfAggregationIndexName) { this.vnfAggregationIndexName = vnfAggregationIndexName; } - + public void getFilteredAggregation(Exchange exchange) { - - Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class); + + Response response = + exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class); Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class); - /* Disables automatic Apache Camel Restlet component logging which prints out an undesirable log entry - which includes client (e.g. browser) information */ + /* + * Disables automatic Apache Camel Restlet component logging which prints out an undesirable log + * entry which includes client (e.g. browser) information + */ request.setLoggable(false); try { @@ -93,29 +98,29 @@ public class AggregateSummaryProcessor { if (parameters.has(KEY_FILTERS)) { requestFilters = parameters.getJSONArray(KEY_FILTERS); } else { - + JSONObject zeroResponsePayload = new JSONObject(); zeroResponsePayload.put("count", 0); response.setStatus(Status.SUCCESS_OK); response.setEntity(zeroResponsePayload.toString(), MediaType.APPLICATION_JSON); exchange.getOut().setBody(response); - + LOG.error(AaiUiMsgs.ERROR_FILTERS_NOT_FOUND); return; } - + if (requestFilters != null && requestFilters.length() > 0) { List<JSONObject> filtersToQuery = new ArrayList<JSONObject>(); - for(int i = 0; i < requestFilters.length(); i++) { + for (int i = 0; i < requestFilters.length(); i++) { JSONObject filterEntry = requestFilters.getJSONObject(i); filtersToQuery.add(filterEntry); } - + String jsonResponsePayload = getVnfFilterAggregations(filtersToQuery); response.setStatus(Status.SUCCESS_OK); response.setEntity(jsonResponsePayload, MediaType.APPLICATION_JSON); exchange.getOut().setBody(response); - + } else { String emptyResponse = getEmptyAggResponse(); response.setStatus(Status.SUCCESS_OK); @@ -125,10 +130,11 @@ public class AggregateSummaryProcessor { } } } catch (Exception exc) { - LOG.error(AaiUiMsgs.ERROR_GENERIC, "FilterProcessor failed to get filter list due to error = " + exc.getMessage()); + LOG.error(AaiUiMsgs.ERROR_GENERIC, + "FilterProcessor failed to get filter list due to error = " + exc.getMessage()); } } - + private String getEmptyAggResponse() { JSONObject aggPayload = new JSONObject(); aggPayload.put("totalChartHits", 0); @@ -137,66 +143,65 @@ public class AggregateSummaryProcessor { payload.append("groupby_aggregation", aggPayload); return payload.toString(); - } - + } + private static final String FILTER_ID_KEY = "filterId"; private static final String FILTER_VALUE_KEY = "filterValue"; private static final int DEFAULT_SHOULD_MATCH_SCORE = 1; private static final String VNF_FILTER_AGGREGATION = "vnfFilterAggregation"; - private String getVnfFilterAggregations(List<JSONObject> filtersToQuery) throws IOException { - + List<SearchFilter> searchFilters = new ArrayList<SearchFilter>(); - for(JSONObject filterEntry : filtersToQuery) { - + for (JSONObject filterEntry : filtersToQuery) { + String filterId = filterEntry.getString(FILTER_ID_KEY); - if(filterId != null) { + if (filterId != null) { SearchFilter filter = new SearchFilter(); filter.setFilterId(filterId); - - if(filterEntry.has(FILTER_VALUE_KEY)) { + + if (filterEntry.has(FILTER_VALUE_KEY)) { String filterValue = filterEntry.getString(FILTER_VALUE_KEY); filter.addValue(filterValue); } - + searchFilters.add(filter); } } - + // Create query for summary by entity type - JsonObject vnfSearch = FilterQueryBuilder.createCombinedBoolAndAggQuery(filtersConfig, searchFilters, DEFAULT_SHOULD_MATCH_SCORE); + JsonObject vnfSearch = FilterQueryBuilder.createCombinedBoolAndAggQuery(filtersConfig, + searchFilters, DEFAULT_SHOULD_MATCH_SCORE); // Parse response for summary by entity type query OperationResult opResult = elasticSearchAdapter.doPost( elasticSearchAdapter.buildElasticSearchUrlForApi(vnfAggregationIndexName, SparkyConstants.ES_SEARCH_API), vnfSearch.toString(), javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE); - + return buildAggregateVnfResponseJson(opResult.getResult()); - + } - + private String buildAggregateVnfResponseJson(String responseJsonStr) { - + JSONObject finalOutputToFe = new JSONObject(); JSONObject responseJson = new JSONObject(responseJsonStr); - - + JSONObject hits = responseJson.getJSONObject("hits"); int totalHits = hits.getInt("total"); finalOutputToFe.put("total", totalHits); - + JSONObject aggregations = responseJson.getJSONObject("aggregations"); String[] aggKeys = JSONObject.getNames(aggregations); JSONObject aggregationsList = new JSONObject(); - - for(String aggName : aggKeys) { + + for (String aggName : aggKeys) { JSONObject aggregation = aggregations.getJSONObject(aggName); JSONArray buckets = aggregation.getJSONArray("buckets"); aggregationsList.put(aggName, buckets); } - + finalOutputToFe.put("aggregations", aggregationsList); return finalOutputToFe.toString(); 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 252ff75..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 @@ -20,46 +20,55 @@ */ 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; } @@ -68,58 +77,96 @@ public class AggregateVnfSearchProvider implements SearchProvider { public List<SearchSuggestion> search(QuerySearchEntity queryRequest) { List<SearchSuggestion> returnList = new ArrayList<SearchSuggestion>(); - 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<SearchSuggestion> 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; + } - 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); - } + ObjectMapper mapper = new ObjectMapper(); + JsonNode rootNode = null; + List<SearchSuggestion> suggestionEntityList = new ArrayList<SearchSuggestion>(); + + 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<JsonNode> 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; - return returnList; } - + + 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; + + } + } diff --git a/src/main/java/org/onap/aai/sparky/aggregatevnf/search/VnfSearchQueryBuilder.java b/src/main/java/org/onap/aai/sparky/aggregatevnf/search/VnfSearchQueryBuilder.java index 62d287e..8542c9a 100644 --- a/src/main/java/org/onap/aai/sparky/aggregatevnf/search/VnfSearchQueryBuilder.java +++ b/src/main/java/org/onap/aai/sparky/aggregatevnf/search/VnfSearchQueryBuilder.java @@ -28,13 +28,11 @@ import javax.json.JsonArrayBuilder; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; - /** * Build a JSON payload to send to elastic search to get vnf search data. */ public class VnfSearchQueryBuilder { - /** * Creates the suggestions query. @@ -134,8 +132,6 @@ public class VnfSearchQueryBuilder { Json.createObjectBuilder().add("must", mustBlobBuilder))); } - - public static JsonObject createSummaryByEntityTypeQuery(Map<String, String> attributes, String groupByKey) { JsonObjectBuilder jsonBuilder = Json.createObjectBuilder(); diff --git a/src/main/java/org/onap/aai/sparky/search/SearchServiceAdapter.java b/src/main/java/org/onap/aai/sparky/search/SearchServiceAdapter.java index 59de87c..d4fc11f 100644 --- a/src/main/java/org/onap/aai/sparky/search/SearchServiceAdapter.java +++ b/src/main/java/org/onap/aai/sparky/search/SearchServiceAdapter.java @@ -42,6 +42,7 @@ import org.slf4j.MDC; public class SearchServiceAdapter { private static final String VALUE_QUERY = "query"; + private static final String SUGGEST_QUERY = "suggest"; private RestClient client; private RestEndpointConfig endpointConfig; @@ -124,6 +125,17 @@ public class SearchServiceAdapter { public String buildSearchServiceQueryUrl(String indexName) { return buildSearchServiceUrlForApi(indexName, VALUE_QUERY); } + + /** + * Get Full URL for search + * + * @param api the api + * @param indexName + * @return the full url + */ + public String buildSuggestServiceQueryUrl(String indexName) { + return buildSearchServiceUrlForApi(indexName, SUGGEST_QUERY); + } public String buildSearchServiceUrlForApi(String indexName, String api) { return String.format("https://%s:%s/services/search-data-service/%s/search/indexes/%s/%s", |