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.sync.entity;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
29 import org.json.JSONArray;
30 import org.json.JSONObject;
31 import org.onap.aai.sparky.config.oxm.SuggestionEntityLookup;
32 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
33 import org.onap.aai.sparky.search.filters.config.FiltersDetailsConfig;
34 import org.onap.aai.sparky.search.filters.config.UiFilterConfig;
35 import org.onap.aai.sparky.util.NodeUtils;
36 import org.onap.aai.sparky.util.SuggestionsPermutation;
38 import com.fasterxml.jackson.databind.JsonNode;
39 import com.fasterxml.jackson.databind.ObjectMapper;
41 public class SuggestionSearchEntity extends IndexableEntity implements IndexDocument {
42 private static final String FILTER_ID = "filterId";
43 private static final String FILTER_VALUE = "filterValue";
44 private static final String FILTER_LIST = "filterList";
46 private String entityType;
47 private List<String> suggestionConnectorWords = new ArrayList<String>();
48 private List<String> suggestionAttributeTypes = new ArrayList<String>();
49 private List<String> suggestionAttributeValues = new ArrayList<String>();
50 private List<String> suggestionTypeAliases = new ArrayList<String>();
51 private List<String> suggestionInputPermutations = new ArrayList<String>();
52 private List<String> suggestableAttr = new ArrayList<String>();
54 private Map<String, String> inputOutputData = new HashMap<String, String>();
55 Map<String, UiFilterConfig> filters = new HashMap<String, UiFilterConfig>();
56 private JSONObject payload = new JSONObject();
57 private JSONArray payloadFilters = new JSONArray();
58 private StringBuffer outputString = new StringBuffer();
59 private String aliasToUse;
61 private SuggestionEntityLookup entityLookup;
63 public JSONObject getPayload() {
67 public void setPayload(JSONObject payload) {
68 this.payload = payload;
71 protected ObjectMapper mapper = new ObjectMapper();
73 public SuggestionSearchEntity(FiltersConfig filtersConfig) {
76 FiltersDetailsConfig filterConfigList = filtersConfig.getFiltersConfig();
77 // Populate the map with keys that will match the suggestableAttr values
78 for(UiFilterConfig filter : filterConfigList.getFilters()) {
79 if(filter.getDataSource() != null) {
80 filters.put(filter.getDataSource().getFieldName(), filter);
85 public SuggestionSearchEntity(FiltersConfig filtersConfig, SuggestionEntityLookup entityLookup) {
87 this.entityLookup = entityLookup;
89 FiltersDetailsConfig filterConfigList = filtersConfig.getFiltersConfig();
90 // Populate the map with keys that will match the suggestableAttr values
91 for(UiFilterConfig filter : filterConfigList.getFilters()) {
92 if(filter.getDataSource() != null) {
93 filters.put(filter.getDataSource().getFieldName(), filter);
98 public SuggestionSearchEntity(SuggestionEntityLookup entityLookup, FiltersConfig config) {
100 FiltersDetailsConfig filterConfigList = config.getFiltersConfig();
101 // Populate the map with keys that will match the suggestableAttr values
102 for(UiFilterConfig filter : filterConfigList.getFilters()) {
103 if(filter.getDataSource() != null) {
104 filters.put(filter.getDataSource().getFieldName(), filter);
109 public void setSuggestableAttr(ArrayList<String> attributes) {
110 for (String attribute : attributes) {
111 this.suggestableAttr.add(attribute);
115 public void setPayloadFromResponse(JsonNode node) {
116 if (suggestableAttr != null) {
117 JSONObject nodePayload = new JSONObject();
118 for (String attribute : suggestableAttr) {
119 if (node.get(attribute) != null) {
120 inputOutputData.put(attribute, node.get(attribute).asText());
121 this.payload.put(attribute, node.get(attribute).asText());
127 public void setFilterBasedPayloadFromResponse(JsonNode node, String entityName, ArrayList<String> uniqueList) {
129 HashMap<String, String> desc = entityLookup.getSuggestionSearchEntityOxmModel().get(entityName);
131 if ( desc == null ) {
135 String attr = desc.get("suggestibleAttributes");
137 if ( attr == null ) {
141 List<String> suggestableAttrOxm = Arrays.asList(attr.split(","));
145 * (1) 'uniqueList' is one item within the power set of the suggestable attributes.
146 * (2) 'inputeOutputData' is used to generate permutations of strings
148 for (String selectiveAttr: uniqueList) {
149 if (node.get(selectiveAttr) != null) {
150 inputOutputData.put(selectiveAttr, node.get(selectiveAttr).asText());
154 if (suggestableAttrOxm != null) {
155 for (String attribute : suggestableAttrOxm) {
156 if (node.get(attribute) != null && uniqueList.contains(attribute)) {
157 UiFilterConfig filterConfig = filters.get(attribute);
158 if(filterConfig != null) {
159 JSONObject filterPayload = new JSONObject();
160 filterPayload.put(FILTER_ID, filterConfig.getFilterId());
161 filterPayload.put(FILTER_VALUE, node.get(attribute).asText());
162 this.payloadFilters.put(filterPayload);
164 this.payload.put(attribute, node.get(attribute).asText());
167 UiFilterConfig emptyValueFilterConfig = filters.get(attribute);
168 if(emptyValueFilterConfig != null) {
169 JSONObject emptyValueFilterPayload = new JSONObject();
170 emptyValueFilterPayload.put(FILTER_ID, emptyValueFilterConfig.getFilterId());
171 this.payloadFilters.put(emptyValueFilterPayload);
175 this.payload.put(FILTER_LIST, this.payloadFilters);
180 public String getEntityType() {
185 public void setEntityType(String entityType) {
186 this.entityType = entityType;
189 public List<String> getSuggestionConnectorWords() {
190 return suggestionConnectorWords;
193 public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
194 this.suggestionConnectorWords = suggestionConnectorWords;
197 public List<String> getSuggestionPropertyTypes() {
198 return this.suggestionAttributeTypes;
201 public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
202 this.suggestionAttributeTypes = suggestionPropertyTypes;
205 public List<String> getSuggestionAttributeValues() {
206 return this.suggestionAttributeValues;
209 public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
210 this.suggestionAttributeValues = suggestionAttributeValues;
213 public List<String> getSuggestionAliases() {
214 return this.suggestionTypeAliases;
217 public void setSuggestionAliases(List<String> suggestionAliases) {
218 this.suggestionTypeAliases = suggestionAliases;
221 public List<String> getSuggestionInputPermutations() {
222 return this.suggestionInputPermutations;
225 public void setSuggestionInputPermutations(List<String> permutations) {
226 this.suggestionInputPermutations = permutations;
229 public void generateSuggestionInputPermutations() {
231 List<String> entityNames = new ArrayList<>();
232 entityNames.add(entityType);
233 HashMap<String, String> desc = entityLookup.getSuggestionSearchEntityOxmModel().get(this.entityType);
234 String attr = desc.get("suggestionAliases");
235 String[] suggestionAliasesArray = attr.split(",");
236 suggestionTypeAliases = Arrays.asList(suggestionAliasesArray);
237 this.setAliasToUse(suggestionAliasesArray[suggestionAliasesArray.length - 1]);
238 for (String alias : suggestionTypeAliases) {
239 entityNames.add(alias);
242 ArrayList<String> listToPermutate = new ArrayList<>(inputOutputData.values());
244 for (String entity : entityNames){
245 listToPermutate.add(entity); // add entity-name or alias in list to permutate
246 List<List<String>> lists = SuggestionsPermutation.getListPermutations(listToPermutate);
247 for (List<String> li : lists){
248 suggestionInputPermutations.add(String.join(" ", li));
250 // prepare for the next pass: remove the entity-name or alias from the list
251 listToPermutate.remove(entity);
255 public boolean isSuggestableDoc() {
256 return this.getPayload().length() != 0;
261 public void deriveFields() {
263 int entryCounter = 1;
264 for (Map.Entry<String, String> outputValue : inputOutputData.entrySet()) {
265 if (outputValue.getValue() != null && outputValue.getValue().length() > 0) {
266 this.outputString.append(outputValue.getValue());
267 if (entryCounter < inputOutputData.entrySet().size()) {
268 this.outputString.append(" and ");
270 this.outputString.append(" ");
276 this.outputString.append(this.getAliasToUse());
277 this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
281 public String getAsJson() {
282 // TODO Auto-generated method stub
283 JSONObject rootNode = new JSONObject();
285 JSONArray suggestionsArray = new JSONArray();
286 for (String suggestion : suggestionInputPermutations) {
287 suggestionsArray.put(suggestion);
290 JSONObject entitySuggest = new JSONObject();
292 entitySuggest.put("input", suggestionsArray);
293 entitySuggest.put("output", this.outputString);
294 entitySuggest.put("payload", this.payload);
295 rootNode.put("entity_suggest", entitySuggest);
297 return rootNode.toString();
300 public String getAliasToUse() {
304 public void setAliasToUse(String aliasToUse) {
305 this.aliasToUse = aliasToUse;
308 public Map<String, String> getInputOutputData() {
309 return inputOutputData;
312 public void setInputOutputData(Map<String, String> inputOutputData) {
313 this.inputOutputData = inputOutputData;
317 public String toString() {
318 return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
319 + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
320 + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
321 + suggestionTypeAliases + ", mapper=" + mapper + "]";