Adding subscription api from separate repo
[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(FiltersConfig filtersConfig) {
76     super();
77     
78     FiltersDetailsConfig filterConfigList = filtersConfig.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(FiltersConfig filtersConfig, SuggestionEntityLookup entityLookup) {
88     
89     this.entityLookup = entityLookup;
90     
91     FiltersDetailsConfig filterConfigList = filtersConfig.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, ArrayList<String> uniqueList) {
130     
131     HashMap<String, String> desc = entityLookup.getSuggestionSearchEntityOxmModel().get(entityName);
132     
133     if ( desc == null ) {
134       return;
135     }
136     
137     String attr = desc.get("suggestibleAttributes");
138     
139     if ( attr == null ) {
140       return;
141     }
142     
143     List<String> suggestableAttrOxm = Arrays.asList(attr.split(","));
144     
145     /*
146      * Note: 
147      * (1) 'uniqueList' is one item within the power set of the suggestable attributes.
148      * (2) '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 = entityLookup.getSuggestionSearchEntityOxmModel().get(this.entityType);
236     String attr = desc.get("suggestionAliases");
237     String[] suggestionAliasesArray = attr.split(",");
238     suggestionTypeAliases = Arrays.asList(suggestionAliasesArray);
239     this.setAliasToUse(suggestionAliasesArray[suggestionAliasesArray.length - 1]);
240     for (String alias : suggestionTypeAliases) {
241       entityNames.add(alias);
242     }
243
244     ArrayList<String> listToPermutate = new ArrayList<>(inputOutputData.values());
245
246     for (String entity : entityNames){
247       listToPermutate.add(entity); // add entity-name or alias in list to permutate
248       List<List<String>> lists = SuggestionsPermutation.getListPermutations(listToPermutate);
249       for (List<String> li : lists){
250         suggestionInputPermutations.add(String.join(" ", li));
251       }
252       // prepare for the next pass: remove the entity-name or alias from the list
253       listToPermutate.remove(entity); 
254     }
255   }
256
257   public boolean isSuggestableDoc() {
258     return this.getPayload().length() != 0;
259   }
260
261
262   @Override
263   public void deriveFields() {
264     
265     int entryCounter = 1;
266     for (Map.Entry<String, String> outputValue : inputOutputData.entrySet()) {
267       if (outputValue.getValue() != null && outputValue.getValue().length() > 0) {
268         this.outputString.append(outputValue.getValue());
269         if (entryCounter < inputOutputData.entrySet().size()) {
270           this.outputString.append(" and ");
271         } else{
272           this.outputString.append(" ");
273         }
274       }
275       entryCounter++;
276     }
277     
278     this.outputString.append(this.getAliasToUse());
279     this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
280   }
281
282   @Override
283   public String getAsJson() {
284     // TODO Auto-generated method stub
285     JSONObject rootNode = new JSONObject();
286
287     JSONArray suggestionsArray = new JSONArray();
288     for (String suggestion : suggestionInputPermutations) {
289       suggestionsArray.put(suggestion);
290     }
291
292     JSONObject entitySuggest = new JSONObject();
293
294     entitySuggest.put("input", suggestionsArray);
295     entitySuggest.put("output", this.outputString);
296     entitySuggest.put("payload", this.payload);
297     rootNode.put("entity_suggest", entitySuggest);
298
299     return rootNode.toString();
300   }
301
302   public String getAliasToUse() {
303     return aliasToUse;
304   }
305
306   public void setAliasToUse(String aliasToUse) {
307     this.aliasToUse = aliasToUse;
308   }
309
310   public Map<String, String> getInputOutputData() {
311     return inputOutputData;
312   }
313
314   public void setInputOutputData(Map<String, String> inputOutputData) {
315     this.inputOutputData = inputOutputData;
316   }
317
318   @Override
319   public String toString() {
320     return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
321         + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
322         + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
323         + suggestionTypeAliases + ", mapper=" + mapper + "]";
324   }
325 }