Update the dependencies to use project version
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / sync / entity / SuggestionSearchEntity.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.sync.entity;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.json.JSONArray;
32 import org.json.JSONObject;
33 import org.onap.aai.sparky.config.oxm.SuggestionEntityLookup;
34 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
35 import org.onap.aai.sparky.search.filters.config.FiltersDetailsConfig;
36 import org.onap.aai.sparky.search.filters.config.UiFilterConfig;
37 import org.onap.aai.sparky.util.NodeUtils;
38 import org.onap.aai.sparky.util.SuggestionsPermutation;
39
40 import com.fasterxml.jackson.databind.JsonNode;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42
43 public class SuggestionSearchEntity extends IndexableEntity implements IndexDocument {
44   private static final String FILTER_ID = "filterId";
45   private static final String FILTER_VALUE = "filterValue";
46   private static final String FILTER_LIST = "filterList";
47
48   private String entityType;
49   private List<String> suggestionConnectorWords = new ArrayList<String>();
50   private List<String> suggestionAttributeTypes = new ArrayList<String>();
51   private List<String> suggestionAttributeValues = new ArrayList<String>();
52   private List<String> suggestionTypeAliases = new ArrayList<String>();
53   private List<String> suggestionInputPermutations = new ArrayList<String>();
54   private List<String> suggestableAttr = new ArrayList<String>();
55
56   private Map<String, String> inputOutputData = new HashMap<String, String>();
57   Map<String, UiFilterConfig> filters = new HashMap<String, UiFilterConfig>();
58   private JSONObject payload = new JSONObject();
59   private JSONArray payloadFilters = new JSONArray();
60   private StringBuffer outputString = new StringBuffer();
61   private String aliasToUse;
62
63   private SuggestionEntityLookup entityLookup;
64
65   public JSONObject getPayload() {
66     return payload;
67   }
68
69   public void setPayload(JSONObject payload) {
70     this.payload = payload;
71   }
72
73   protected ObjectMapper mapper = new ObjectMapper();
74
75   public SuggestionSearchEntity() {
76     super();
77
78     FiltersDetailsConfig filterConfigList = FiltersConfig.getInstance().getFiltersConfig();
79     // Populate the map with keys that will match the suggestableAttr values
80     for (UiFilterConfig filter : filterConfigList.getFilters()) {
81       if (filter.getDataSource() != null) {
82         filters.put(filter.getDataSource().getFieldName(), filter);
83       }
84     }
85   }
86
87   public SuggestionSearchEntity(SuggestionEntityLookup entityLookup) {
88
89     this.entityLookup = entityLookup;
90
91     FiltersDetailsConfig filterConfigList = FiltersConfig.getInstance().getFiltersConfig();
92     // Populate the map with keys that will match the suggestableAttr values
93     for (UiFilterConfig filter : filterConfigList.getFilters()) {
94       if (filter.getDataSource() != null) {
95         filters.put(filter.getDataSource().getFieldName(), filter);
96       }
97     }
98   }
99
100   public SuggestionSearchEntity(SuggestionEntityLookup entityLookup, FiltersConfig config) {
101
102     FiltersDetailsConfig filterConfigList = config.getFiltersConfig();
103     // Populate the map with keys that will match the suggestableAttr values
104     for (UiFilterConfig filter : filterConfigList.getFilters()) {
105       if (filter.getDataSource() != null) {
106         filters.put(filter.getDataSource().getFieldName(), filter);
107       }
108     }
109   }
110
111   public void setSuggestableAttr(ArrayList<String> attributes) {
112     for (String attribute : attributes) {
113       this.suggestableAttr.add(attribute);
114     }
115   }
116
117   public void setPayloadFromResponse(JsonNode node) {
118     if (suggestableAttr != null) {
119       JSONObject nodePayload = new JSONObject();
120       for (String attribute : suggestableAttr) {
121         if (node.get(attribute) != null) {
122           inputOutputData.put(attribute, node.get(attribute).asText());
123           this.payload.put(attribute, node.get(attribute).asText());
124         }
125       }
126     }
127   }
128
129   public void setFilterBasedPayloadFromResponse(JsonNode node, String entityName,
130       ArrayList<String> uniqueList) {
131
132     HashMap<String, String> desc = entityLookup.getSuggestionSearchEntityOxmModel().get(entityName);
133
134     if (desc == null) {
135       return;
136     }
137
138     String attr = desc.get("suggestibleAttributes");
139
140     if (attr == null) {
141       return;
142     }
143
144     List<String> suggestableAttrOxm = Arrays.asList(attr.split(","));
145
146     /*
147      * Note: (1) 'uniqueList' is one item within the power set of the suggestable attributes. (2)
148      * 'inputeOutputData' is used to generate permutations of strings
149      */
150     for (String selectiveAttr : uniqueList) {
151       if (node.get(selectiveAttr) != null) {
152         inputOutputData.put(selectiveAttr, node.get(selectiveAttr).asText());
153       }
154     }
155
156     if (suggestableAttrOxm != null) {
157       for (String attribute : suggestableAttrOxm) {
158         if (node.get(attribute) != null && uniqueList.contains(attribute)) {
159           UiFilterConfig filterConfig = filters.get(attribute);
160           if (filterConfig != null) {
161             JSONObject filterPayload = new JSONObject();
162             filterPayload.put(FILTER_ID, filterConfig.getFilterId());
163             filterPayload.put(FILTER_VALUE, node.get(attribute).asText());
164             this.payloadFilters.put(filterPayload);
165           } else {
166             this.payload.put(attribute, node.get(attribute).asText());
167           }
168         } else {
169           UiFilterConfig emptyValueFilterConfig = filters.get(attribute);
170           if (emptyValueFilterConfig != null) {
171             JSONObject emptyValueFilterPayload = new JSONObject();
172             emptyValueFilterPayload.put(FILTER_ID, emptyValueFilterConfig.getFilterId());
173             this.payloadFilters.put(emptyValueFilterPayload);
174           }
175         }
176       }
177       this.payload.put(FILTER_LIST, this.payloadFilters);
178     }
179   }
180
181   @Override
182   public String getEntityType() {
183     return entityType;
184   }
185
186   @Override
187   public void setEntityType(String entityType) {
188     this.entityType = entityType;
189   }
190
191   public List<String> getSuggestionConnectorWords() {
192     return suggestionConnectorWords;
193   }
194
195   public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
196     this.suggestionConnectorWords = suggestionConnectorWords;
197   }
198
199   public List<String> getSuggestionPropertyTypes() {
200     return this.suggestionAttributeTypes;
201   }
202
203   public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
204     this.suggestionAttributeTypes = suggestionPropertyTypes;
205   }
206
207   public List<String> getSuggestionAttributeValues() {
208     return this.suggestionAttributeValues;
209   }
210
211   public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
212     this.suggestionAttributeValues = suggestionAttributeValues;
213   }
214
215   public List<String> getSuggestionAliases() {
216     return this.suggestionTypeAliases;
217   }
218
219   public void setSuggestionAliases(List<String> suggestionAliases) {
220     this.suggestionTypeAliases = suggestionAliases;
221   }
222
223   public List<String> getSuggestionInputPermutations() {
224     return this.suggestionInputPermutations;
225   }
226
227   public void setSuggestionInputPermutations(List<String> permutations) {
228     this.suggestionInputPermutations = permutations;
229   }
230
231   public void generateSuggestionInputPermutations() {
232
233     List<String> entityNames = new ArrayList<>();
234     entityNames.add(entityType);
235     HashMap<String, String> desc =
236         entityLookup.getSuggestionSearchEntityOxmModel().get(this.entityType);
237     String attr = desc.get("suggestionAliases");
238     String[] suggestionAliasesArray = attr.split(",");
239     suggestionTypeAliases = Arrays.asList(suggestionAliasesArray);
240     this.setAliasToUse(suggestionAliasesArray[suggestionAliasesArray.length - 1]);
241     for (String alias : suggestionTypeAliases) {
242       entityNames.add(alias);
243     }
244
245     ArrayList<String> listToPermutate = new ArrayList<>(inputOutputData.values());
246
247     for (String entity : entityNames) {
248       listToPermutate.add(entity); // add entity-name or alias in list to permutate
249       List<List<String>> lists = SuggestionsPermutation.getListPermutations(listToPermutate);
250       for (List<String> li : lists) {
251         suggestionInputPermutations.add(String.join(" ", li));
252       }
253       // prepare for the next pass: remove the entity-name or alias from the list
254       listToPermutate.remove(entity);
255     }
256   }
257
258   public boolean isSuggestableDoc() {
259     return this.getPayload().length() != 0;
260   }
261
262
263   @Override
264   public void deriveFields() {
265
266     int entryCounter = 1;
267     for (Map.Entry<String, String> outputValue : inputOutputData.entrySet()) {
268       if (outputValue.getValue() != null && outputValue.getValue().length() > 0) {
269         this.outputString.append(outputValue.getValue());
270         if (entryCounter < inputOutputData.entrySet().size()) {
271           this.outputString.append(" and ");
272         } else {
273           this.outputString.append(" ");
274         }
275       }
276       entryCounter++;
277     }
278
279     this.outputString.append(this.getAliasToUse());
280     this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
281   }
282
283   @Override
284   public String getAsJson() {
285     // TODO Auto-generated method stub
286     JSONObject rootNode = new JSONObject();
287
288     JSONArray suggestionsArray = new JSONArray();
289     for (String suggestion : suggestionInputPermutations) {
290       suggestionsArray.put(suggestion);
291     }
292
293     JSONObject entitySuggest = new JSONObject();
294
295     entitySuggest.put("input", suggestionsArray);
296     entitySuggest.put("output", this.outputString);
297     entitySuggest.put("payload", this.payload);
298     rootNode.put("entity_suggest", entitySuggest);
299
300     return rootNode.toString();
301   }
302
303   public String getAliasToUse() {
304     return aliasToUse;
305   }
306
307   public void setAliasToUse(String aliasToUse) {
308     this.aliasToUse = aliasToUse;
309   }
310
311   public Map<String, String> getInputOutputData() {
312     return inputOutputData;
313   }
314
315   public void setInputOutputData(Map<String, String> inputOutputData) {
316     this.inputOutputData = inputOutputData;
317   }
318
319   @Override
320   public String toString() {
321     return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
322         + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
323         + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
324         + suggestionTypeAliases + ", mapper=" + mapper + "]";
325   }
326 }