summaryrefslogtreecommitdiffstats
path: root/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/aggregatevnf/search/AggregateVnfSearchProvider.java
blob: 574983055d7e68f88e8eec68c6c4d6b621c77438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * Copyright © 2019 IBM
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.sparky.aggregatevnf.search;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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.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 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 SearchServiceAdapter searchServiceAdapter = null;
  private String autoSuggestIndexName;
  private String vnfSearchSuggestionRoute;

  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) {
    this.searchServiceAdapter = searchServiceAdapter;
    this.autoSuggestIndexName = autoSuggestIndexName;
    this.vnfSearchSuggestionRoute = vnfSearchSuggestionRoute;
  }

  public void setAutoSuggestIndexName(String autoSuggestIndexName) {
    this.autoSuggestIndexName = autoSuggestIndexName;
  }

  @Override
  public List<SearchSuggestion> search(QuerySearchEntity queryRequest) {

    List<SearchSuggestion> returnList = new ArrayList<>();
    try {

      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);
      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());
    }

    return returnList;
  }

  private List<SearchSuggestion> generateSuggestionsForSearchResponse(String operationResult) {

    if (operationResult == null || operationResult.length() == 0) {
      return null;
    }

    ObjectMapper mapper = new ObjectMapper();
    List<SearchSuggestion> suggestionEntityList = new ArrayList<>();

    try {
      JsonNode 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();
        while (nodeIterator.hasNext()) {
          JsonNode entityNode = nodeIterator.next();
          if(entityNode != null) {
            String responseText = getValueFromNode(entityNode, KEY_TEXT);

            CommonSearchSuggestion responseSuggestion = createCommonSearchSuggestion(mapper, entityNode, responseText);
            suggestionEntityList.add(responseSuggestion);
          }
        }
      }
    } catch (IOException exc) {
      LOG.warn(AaiUiMsgs.SEARCH_RESPONSE_BUILDING_EXCEPTION, exc.getLocalizedMessage());
    }
    return suggestionEntityList;

  }

  private CommonSearchSuggestion createCommonSearchSuggestion(ObjectMapper mapper, JsonNode entityNode, String responseText) throws IOException {
    // do the point transformation as we build the response?
    CommonSearchSuggestion responseSuggestion = new CommonSearchSuggestion();
    responseSuggestion.setRoute(vnfSearchSuggestionRoute);
    responseSuggestion.setText(responseText);
    responseSuggestion.setHashId(NodeUtils.generateUniqueShaDigest(responseText));

    JsonNode keyDocument = entityNode.get(KEY_DOCUMENT);
    JsonNode sourceNode = keyDocument.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);
      }
    }
    return responseSuggestion;
  }

  private String getValueFromNode(JsonNode node, String fieldName) {

    if (fieldName == null) {
      return null;
    }

    JsonNode valueNode = node.get(fieldName);

    if (valueNode != null) {
      return valueNode.asText();
    }

    return null;

  }

}