2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 
   6  * Copyright © 2017-2018 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 package org.onap.aai.sparky.aggregatevnf.search;
 
  23 import java.io.IOException;
 
  24 import java.util.ArrayList;
 
  25 import java.util.Iterator;
 
  26 import java.util.List;
 
  28 import org.onap.aai.cl.api.Logger;
 
  29 import org.onap.aai.cl.eelf.LoggerFactory;
 
  30 import org.onap.aai.restclient.client.OperationResult;
 
  31 import org.onap.aai.sparky.common.search.CommonSearchSuggestion;
 
  32 import org.onap.aai.sparky.logging.AaiUiMsgs;
 
  33 import org.onap.aai.sparky.search.SearchServiceAdapter;
 
  34 import org.onap.aai.sparky.search.api.SearchProvider;
 
  35 import org.onap.aai.sparky.search.entity.QuerySearchEntity;
 
  36 import org.onap.aai.sparky.search.entity.SearchSuggestion;
 
  37 import org.onap.aai.sparky.search.filters.entity.UiFilterValueEntity;
 
  38 import org.onap.aai.sparky.util.NodeUtils;
 
  40 import com.fasterxml.jackson.databind.JsonNode;
 
  41 import com.fasterxml.jackson.databind.ObjectMapper;
 
  42 import com.fasterxml.jackson.databind.node.ArrayNode;
 
  44 public class AggregateVnfSearchProvider implements SearchProvider {
 
  46   private static final Logger LOG =
 
  47       LoggerFactory.getInstance().getLogger(AggregateVnfSearchProvider.class);
 
  49   private ObjectMapper mapper;
 
  50   private SearchServiceAdapter searchServiceAdapter = null;
 
  51   private String autoSuggestIndexName;
 
  52   private String vnfSearchSuggestionRoute;
 
  54   private static final String AUTO_SUGGEST_TEMPLATE = "{ " + "\"results-size\": %d,"
 
  55       + "\"suggest-text\": \"%s\"," + "\"suggest-field\": \"%s\"" + "}";
 
  57   private static final String KEY_SEARCH_RESULT = "searchResult";
 
  58   private static final String KEY_HITS = "hits";
 
  59   private static final String KEY_DOCUMENT = "document";
 
  60   private static final String KEY_CONTENT = "content";
 
  61   private static final String KEY_TEXT = "text";
 
  62   private static final String KEY_FILTER_LIST = "filterList";
 
  64   public AggregateVnfSearchProvider(SearchServiceAdapter searchServiceAdapter,
 
  65       String autoSuggestIndexName, String vnfSearchSuggestionRoute) {
 
  66     mapper = new ObjectMapper();
 
  67     this.searchServiceAdapter = searchServiceAdapter;
 
  68     this.autoSuggestIndexName = autoSuggestIndexName;
 
  69     this.vnfSearchSuggestionRoute = vnfSearchSuggestionRoute;
 
  72   public void setAutoSuggestIndexName(String autoSuggestIndexName) {
 
  73     this.autoSuggestIndexName = autoSuggestIndexName;
 
  77   public List<SearchSuggestion> search(QuerySearchEntity queryRequest) {
 
  79     List<SearchSuggestion> returnList = new ArrayList<SearchSuggestion>();
 
  82       final String fullUrlStr =
 
  83           searchServiceAdapter.buildSuggestServiceQueryUrl(autoSuggestIndexName);
 
  85           String.format(AUTO_SUGGEST_TEMPLATE, Integer.parseInt(queryRequest.getMaxResults()),
 
  86               queryRequest.getQueryStr(), "entity_suggest");
 
  87       OperationResult opResult =
 
  88           searchServiceAdapter.doPost(fullUrlStr, postBody, "application/json");
 
  89       if (opResult.getResultCode() == 200) {
 
  90         returnList = generateSuggestionsForSearchResponse(opResult.getResult());
 
  92         LOG.error(AaiUiMsgs.ERROR_PARSING_JSON_PAYLOAD_VERBOSE, opResult.getResult());
 
  95     } catch (Exception exc) {
 
  96       LOG.error(AaiUiMsgs.ERROR_GENERIC, "Search failed due to error = " + exc.getMessage());
 
 102   private List<SearchSuggestion> generateSuggestionsForSearchResponse(String operationResult) {
 
 104     if (operationResult == null || operationResult.length() == 0) {
 
 108     ObjectMapper mapper = new ObjectMapper();
 
 109     List<SearchSuggestion> suggestionEntityList = new ArrayList<SearchSuggestion>();
 
 112       JsonNode rootNode = mapper.readTree(operationResult);
 
 113       JsonNode hitsNode = rootNode.get(KEY_SEARCH_RESULT);
 
 114       // Check if there are hits that are coming back
 
 115       if (hitsNode.has(KEY_HITS)) {
 
 116         ArrayNode hitsArray = (ArrayNode) hitsNode.get(KEY_HITS);
 
 119          * next we iterate over the values in the hit array elements
 
 121         Iterator<JsonNode> nodeIterator = hitsArray.elements();
 
 122         while (nodeIterator.hasNext()) {
 
 123           JsonNode entityNode = nodeIterator.next();
 
 124           if(entityNode != null) {
 
 125             String responseText = getValueFromNode(entityNode, KEY_TEXT);
 
 127             CommonSearchSuggestion responseSuggestion = createCommonSearchSuggestion(mapper, entityNode, responseText);
 
 128             suggestionEntityList.add(responseSuggestion);
 
 132     } catch (IOException exc) {
 
 133       LOG.warn(AaiUiMsgs.SEARCH_RESPONSE_BUILDING_EXCEPTION, exc.getLocalizedMessage());
 
 135     return suggestionEntityList;
 
 139   private CommonSearchSuggestion createCommonSearchSuggestion(ObjectMapper mapper, JsonNode entityNode, String responseText) throws IOException {
 
 140     // do the point transformation as we build the response?
 
 141     CommonSearchSuggestion responseSuggestion = new CommonSearchSuggestion();
 
 142     responseSuggestion.setRoute(vnfSearchSuggestionRoute);
 
 143     responseSuggestion.setText(responseText);
 
 144     responseSuggestion.setHashId(NodeUtils.generateUniqueShaDigest(responseText));
 
 146     JsonNode keyDocument = entityNode.get(KEY_DOCUMENT);
 
 147     JsonNode sourceNode = keyDocument.get(KEY_CONTENT);
 
 148     if (sourceNode.has(KEY_FILTER_LIST)) {
 
 149       ArrayNode filtersArray = (ArrayNode) sourceNode.get(KEY_FILTER_LIST);
 
 150       for (int i = 0; i < filtersArray.size(); i++) {
 
 151         String filterValueString = filtersArray.get(i).toString();
 
 152         UiFilterValueEntity filterValue =
 
 153             mapper.readValue(filterValueString, UiFilterValueEntity.class);
 
 154         responseSuggestion.getFilterValues().add(filterValue);
 
 157     return responseSuggestion;
 
 160   private String getValueFromNode(JsonNode node, String fieldName) {
 
 162     if (fieldName == null) {
 
 166     JsonNode valueNode = node.get(fieldName);
 
 168     if (valueNode != null) {
 
 169       return valueNode.asText();