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