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.openecomp.datarouter.entity;
25 import java.io.IOException;
26 import java.io.Serializable;
27 import java.security.NoSuchAlgorithmException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.openecomp.datarouter.search.filters.config.UiFilterConfig;
36 import org.openecomp.datarouter.search.filters.config.UiFiltersConfig;
37 import org.openecomp.datarouter.search.filters.config.UiFiltersSchemaUtility;
38 import org.openecomp.datarouter.util.NodeUtils;
39 import org.openecomp.datarouter.util.SearchSuggestionPermutation;
41 import com.fasterxml.jackson.databind.JsonNode;
43 public class SuggestionSearchEntity implements DocumentStoreDataEntity, Serializable {
44 private static final long serialVersionUID = -3636393943669310760L;
46 private static final String FILTER_ID = "filterId";
47 private static final String FILTER_VALUE = "filterValue";
48 private static final String FILTER_LIST = "filterList";
50 protected String id; // generated SHA-256 digest
51 private String entityType;
52 private List<String> entityTypeAliases;
53 private List<String> suggestionInputPermutations = new ArrayList<>();
54 private List<String> statusPermutations = new ArrayList<>();
55 private List<String> suggestableAttr = new ArrayList<>();
57 private Map<String, String> inputOutputData = new HashMap<>();
58 private Map<String, UiFilterConfig> filters = new HashMap<>();
59 private JSONObject filterPayload = new JSONObject();
60 private StringBuffer searchSuggestionDisplayString = new StringBuffer();
61 private JSONArray payloadFilters = new JSONArray();
62 private UiFiltersSchemaUtility filtersSchemaUtility = new UiFiltersSchemaUtility();
64 public SuggestionSearchEntity() {
65 UiFiltersConfig filterConfigList = filtersSchemaUtility.loadUiFiltersConfig();
67 // Populate the map with keys that will match the suggestableAttr values
68 for (UiFilterConfig filter : filterConfigList.getFilters()) {
69 if (filter.getDataSource() != null) {
70 filters.put(filter.getDataSource().getFieldName(), filter);
76 * Create the search suggestion string to display to the user in the search suggestion drop-down
78 * @throws NoSuchAlgorithmException
80 public void generateSearchSuggestionDisplayStringAndId() throws NoSuchAlgorithmException {
81 int payloadEntryCounter = 1;
83 for (Map.Entry<String, String> outputValue : inputOutputData.entrySet()) {
84 if (outputValue.getValue() != null && outputValue.getValue().length() > 0) {
85 this.searchSuggestionDisplayString.append(outputValue.getValue());
87 if (payloadEntryCounter < inputOutputData.entrySet().size()) {
88 this.searchSuggestionDisplayString.append(" and ");
90 this.searchSuggestionDisplayString.append(" ");
94 payloadEntryCounter++;
97 this.searchSuggestionDisplayString.append(getEntityTypeAliases().get(0));
98 generateSearchSuggestionId(searchSuggestionDisplayString.toString());
102 * Generates an ID by encrypting the string to display to the user in the search suggestion
105 * @param outputString The string to create the encrypted ID from
107 private void generateSearchSuggestionId(String searchSuggestionDisplayString) {
108 this.id = NodeUtils.generateUniqueShaDigest(searchSuggestionDisplayString);
112 * Launch pad for performing permutations of the entity type, aliases, prov status and orchestration status.
113 * SHA-256 will result in an ID with a guaranteed uniqueness compared to just a java hashcode value
117 public List<String> generateSuggestionInputPermutations() {
118 List<String> entityNames = new ArrayList<>();
119 entityNames.add(entityType);
121 if ((entityTypeAliases != null) && !(entityTypeAliases.isEmpty())) {
122 for (String alias : entityTypeAliases) {
123 entityNames.add(alias);
127 ArrayList<String> listOfSearchSuggestionPermutations = new ArrayList<String>();
128 ArrayList<String> listToPermutate =
129 new ArrayList<>(this.getInputOutputData().values());
131 for (String entityName : entityNames) {
132 listToPermutate.add(entityName);
133 List<List<String>> lists = SearchSuggestionPermutation.getListPermutations(listToPermutate);
134 for (List<String> li : lists){
135 listOfSearchSuggestionPermutations.add(String.join(" ", li));
137 listToPermutate.remove(entityName);
140 return listOfSearchSuggestionPermutations;
144 * Return a custom JSON representation of this class
147 public String getAsJson() throws IOException {
148 if (entityType == null || suggestionInputPermutations == null) {
152 JSONObject rootNode = new JSONObject();
153 JSONArray inputArray = new JSONArray();
154 JSONObject payloadNode = new JSONObject();
155 StringBuffer outputString = new StringBuffer();
157 int payloadEntryCounter = 1;
159 // Add prov and orchestration status to search suggestion string
160 for (Map.Entry<String, String> payload : inputOutputData.entrySet()) {
161 payloadNode.put(payload.getKey(), payload.getValue());
162 outputString.append(payload.getValue());
164 if (payloadEntryCounter < inputOutputData.entrySet().size()) {
165 // Add the word "and" between prov and orchestration statuses, if both are present
166 outputString.append(" and ");
167 payloadEntryCounter++;
171 /* Add entity type to search suggestion string. We've decided to use the first entity type alias
173 outputString.append(" ").append(getEntityTypeAliases().get(0));
175 for (String permutation : suggestionInputPermutations) {
176 inputArray.put(permutation);
179 // Build up the search suggestion as JSON
180 JSONObject entitySuggest = new JSONObject();
181 entitySuggest.put("input", inputArray);
182 entitySuggest.put("output", outputString);
183 entitySuggest.put("payload", this.filterPayload);
184 rootNode.put("entity_suggest", entitySuggest);
186 return rootNode.toString();
189 public boolean isSuggestableDoc() {
190 return this.getFilterPayload().length() != 0;
194 * Generate all permutations of Entity Type and (Prov Status and/or Orchestration Status)
196 * @param list The list of unique elements to create permutations of
197 * @param permutation A list to hold the current permutation used during
198 * @param size To keep track of the original size of the number of unique elements
199 * @param listOfSearchSuggestionPermutationList The list to hold all of the different permutations
201 private void permutateList(List<String> list, List<String> permutation, int size,
202 List<String> listOfSearchSuggestionPermutationList) {
203 if (permutation.size() == size) {
204 StringBuilder newPermutation = new StringBuilder();
206 for (int i = 0; i < permutation.size(); i++) {
207 newPermutation.append(permutation.get(i)).append(" ");
210 listOfSearchSuggestionPermutationList.add(newPermutation.toString().trim());
215 String[] availableItems = list.toArray(new String[0]);
217 for (String i : availableItems) {
220 permutateList(list, permutation, size, listOfSearchSuggestionPermutationList);
222 permutation.remove(i);
227 * Populate a string that will represent the UI filters portion of the JSON payload that's stored in the
230 * @param entityFromUebEvent
231 * @param suggestibleAttrInPayload
233 public void setFilterBasedPayloadFromResponse(JsonNode entityFromUebEvent,
234 List<String> suggestibleAttrInOxm, List<String> suggestibleAttrInPayload) {
235 if (suggestibleAttrInOxm != null) {
236 for (String attribute : suggestibleAttrInOxm) {
237 UiFilterConfig filterConfig = filters.get(attribute);
239 if (suggestibleAttrInPayload.contains(attribute)) {
240 inputOutputData.put(attribute, entityFromUebEvent.get(attribute).asText());
242 if(filterConfig != null) {
243 JSONObject filterPayload = new JSONObject();
244 filterPayload.put(FILTER_ID, filterConfig.getFilterId());
245 filterPayload.put(FILTER_VALUE, entityFromUebEvent.get(attribute).asText());
246 this.payloadFilters.put(filterPayload);
248 this.filterPayload.put(attribute, entityFromUebEvent.get(attribute).asText());
251 if(filterConfig != null) {
252 JSONObject emptyValueFilterPayload = new JSONObject();
253 emptyValueFilterPayload.put(FILTER_ID, filterConfig.getFilterId());
254 this.payloadFilters.put(emptyValueFilterPayload);
259 this.filterPayload.put(FILTER_LIST, this.payloadFilters);
263 public void setPayloadFromResponse(JsonNode node) {
264 if (suggestableAttr != null) {
265 for (String attribute : suggestableAttr) {
266 if (node.get(attribute) != null) {
267 inputOutputData.put(attribute, node.get(attribute).asText());
268 this.filterPayload.put(attribute, node.get(attribute).asText());
274 public String getEntityType() {
278 public void setEntityType(String entityType) {
279 this.entityType = entityType;
282 public List<String> getEntityTypeAliases() {
283 return entityTypeAliases;
286 public void setEntityTypeAliases(List<String> entityTypeAliases) {
287 this.entityTypeAliases = entityTypeAliases;
291 public String getId() {
295 public StringBuffer getSearchSuggestionDisplayString() {
296 return searchSuggestionDisplayString;
299 public JSONObject getFilterPayload() {
300 return filterPayload;
303 public List<String> getStatusPermutations() {
304 return statusPermutations;
307 public List<String> getSuggestableAttr() {
308 return suggestableAttr;
311 public List<String> getSuggestionInputPermutations() {
312 return this.suggestionInputPermutations;
315 public void setId(String id) {
319 public void setInputOutputData(Map<String, String> inputOutputData) {
320 this.inputOutputData = inputOutputData;
323 public Map<String, String> getInputOutputData() {
324 return inputOutputData;
327 public void setSearchSuggestionDisplayString(StringBuffer searchSuggestionDisplayString) {
328 this.searchSuggestionDisplayString = searchSuggestionDisplayString;
331 public void setFilterPayload(JSONObject filterPayload) {
332 this.filterPayload = filterPayload;
335 public void setFiltersSchemaUtility(UiFiltersSchemaUtility filtersSchemaUtility) {
336 this.filtersSchemaUtility = filtersSchemaUtility;
339 public void setStatusPermutations(List<String> statusPermutations) {
340 this.statusPermutations = statusPermutations;
343 public void setSuggestableAttr(List<String> attributes) {
344 for (String attribute : attributes) {
345 this.suggestableAttr.add(attribute);
349 public void setSuggestionInputPermutations(List<String> permutations) {
350 this.suggestionInputPermutations = permutations;
354 public String toString() {
355 return "SuggestionSearchEntity [id=" + id + ", entityType=" + entityType
356 + ", entityTypeAliases=" + entityTypeAliases + ", suggestionInputPermutations="
357 + suggestionInputPermutations + ", statusPermutations=" + statusPermutations
358 + ", suggestableAttr=" + suggestableAttr + ", inputOutputData=" + inputOutputData
359 + ", filters=" + filters + ", filterPayload=" + filterPayload
360 + ", searchSuggestionDisplayString=" + searchSuggestionDisplayString + ", payloadFilters="
361 + payloadFilters + "]";