Adding interfaces in documentation
[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-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
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 package org.onap.aai.sparky.sync.entity;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
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;
37
38 import com.fasterxml.jackson.databind.JsonNode;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
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"; 
45   
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>();
53   
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;
60   
61   private SuggestionEntityLookup entityLookup;
62   
63   public JSONObject getPayload() {
64     return payload;
65   }
66
67   public void setPayload(JSONObject payload) {
68     this.payload = payload;
69   }
70   
71   protected ObjectMapper mapper = new ObjectMapper();
72
73   public SuggestionSearchEntity(FiltersConfig filtersConfig) {
74     super();
75     
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);
81       }
82     }
83   }
84   
85   public SuggestionSearchEntity(FiltersConfig filtersConfig, SuggestionEntityLookup entityLookup) {
86     
87     this.entityLookup = entityLookup;
88     
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);
94       }
95     }
96   }
97   
98   public SuggestionSearchEntity(SuggestionEntityLookup entityLookup, FiltersConfig config) {
99     
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);
105       }
106     }
107   }
108
109   public void setSuggestableAttr(ArrayList<String> attributes) {
110     for (String attribute : attributes) {
111       this.suggestableAttr.add(attribute);
112     }
113   }
114
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());
122         }
123       }
124     }
125   }
126   
127   public void setFilterBasedPayloadFromResponse(JsonNode node, String entityName, ArrayList<String> uniqueList) {
128     
129     HashMap<String, String> desc = entityLookup.getSuggestionSearchEntityOxmModel().get(entityName);
130     
131     if ( desc == null ) {
132       return;
133     }
134     
135     String attr = desc.get("suggestibleAttributes");
136     
137     if ( attr == null ) {
138       return;
139     }
140     
141     List<String> suggestableAttrOxm = Arrays.asList(attr.split(","));
142     
143     /*
144      * Note: 
145      * (1) 'uniqueList' is one item within the power set of the suggestable attributes.
146      * (2) 'inputeOutputData' is used to generate permutations of strings
147      */
148     for (String selectiveAttr: uniqueList) {
149       if (node.get(selectiveAttr) != null) {
150         inputOutputData.put(selectiveAttr, node.get(selectiveAttr).asText());
151       }
152     }
153       
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);
163           } else {
164             this.payload.put(attribute, node.get(attribute).asText()); 
165           }
166         } else {
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);
172           }
173         }
174       }
175       this.payload.put(FILTER_LIST, this.payloadFilters);
176     }
177   }
178
179   @Override
180   public String getEntityType() {
181     return entityType;
182   }
183
184   @Override
185   public void setEntityType(String entityType) {
186     this.entityType = entityType;
187   }
188
189   public List<String> getSuggestionConnectorWords() {
190     return suggestionConnectorWords;
191   }
192
193   public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
194     this.suggestionConnectorWords = suggestionConnectorWords;
195   }
196
197   public List<String> getSuggestionPropertyTypes() {
198     return this.suggestionAttributeTypes;
199   }
200
201   public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
202     this.suggestionAttributeTypes = suggestionPropertyTypes;
203   }
204
205   public List<String> getSuggestionAttributeValues() {
206     return this.suggestionAttributeValues;
207   }
208
209   public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
210     this.suggestionAttributeValues = suggestionAttributeValues;
211   }
212
213   public List<String> getSuggestionAliases() {
214     return this.suggestionTypeAliases;
215   }
216
217   public void setSuggestionAliases(List<String> suggestionAliases) {
218     this.suggestionTypeAliases = suggestionAliases;
219   }
220
221   public List<String> getSuggestionInputPermutations() {
222     return this.suggestionInputPermutations;
223   }
224
225   public void setSuggestionInputPermutations(List<String> permutations) {
226     this.suggestionInputPermutations = permutations;
227   }
228
229   public void generateSuggestionInputPermutations() {
230
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);
240     }
241
242     ArrayList<String> listToPermutate = new ArrayList<>(inputOutputData.values());
243
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));
249       }
250       // prepare for the next pass: remove the entity-name or alias from the list
251       listToPermutate.remove(entity); 
252     }
253   }
254
255   public boolean isSuggestableDoc() {
256     return this.getPayload().length() != 0;
257   }
258
259
260   @Override
261   public void deriveFields() {
262     
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 ");
269         } else{
270           this.outputString.append(" ");
271         }
272       }
273       entryCounter++;
274     }
275     
276     this.outputString.append(this.getAliasToUse());
277     this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
278   }
279
280   @Override
281   public String getAsJson() {
282     // TODO Auto-generated method stub
283     JSONObject rootNode = new JSONObject();
284
285     JSONArray suggestionsArray = new JSONArray();
286     for (String suggestion : suggestionInputPermutations) {
287       suggestionsArray.put(suggestion);
288     }
289
290     JSONObject entitySuggest = new JSONObject();
291
292     entitySuggest.put("input", suggestionsArray);
293     entitySuggest.put("output", this.outputString);
294     entitySuggest.put("payload", this.payload);
295     rootNode.put("entity_suggest", entitySuggest);
296
297     return rootNode.toString();
298   }
299
300   public String getAliasToUse() {
301     return aliasToUse;
302   }
303
304   public void setAliasToUse(String aliasToUse) {
305     this.aliasToUse = aliasToUse;
306   }
307
308   public Map<String, String> getInputOutputData() {
309     return inputOutputData;
310   }
311
312   public void setInputOutputData(Map<String, String> inputOutputData) {
313     this.inputOutputData = inputOutputData;
314   }
315
316   @Override
317   public String toString() {
318     return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
319         + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
320         + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
321         + suggestionTypeAliases + ", mapper=" + mapper + "]";
322   }
323 }