Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / synchronizer / 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.openecomp.sparky.synchronizer.entity;
26
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.node.ObjectNode;
30
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 import org.json.JSONArray;
38 import org.json.JSONObject;
39 import org.openecomp.sparky.config.oxm.OxmModelLoader;
40 import org.openecomp.sparky.util.NodeUtils;
41
42 public class SuggestionSearchEntity extends IndexableEntity implements IndexDocument {
43
44   private String entityType;
45   private List<String> suggestionConnectorWords = new ArrayList<String>();
46   private List<String> suggestionAttributeTypes = new ArrayList<String>();
47   private List<String> suggestionAttributeValues = new ArrayList<String>();
48   private List<String> suggestionTypeAliases = new ArrayList<String>();
49   private List<String> suggestionInputPermutations = new ArrayList<String>();
50   private List<String> suggestableAttr = new ArrayList<String>();
51   private Map<String, String> payload = new HashMap<String, String>();
52   private JSONObject payloadJsonNode = new JSONObject();
53   private StringBuffer outputString = new StringBuffer();
54   private String aliasToUse;
55
56   public Map<String, String> getPayload() {
57     return payload;
58   }
59
60   public void setPayload(Map<String, String> payload) {
61     this.payload = payload;
62   }
63   
64   
65   public JSONObject getPayloadJsonNode() {
66     return payloadJsonNode;
67   }
68
69   public void setPayloadJsonNode(JSONObject payloadJsonNode) {
70     this.payloadJsonNode = payloadJsonNode;
71   }
72
73
74   protected ObjectMapper mapper = new ObjectMapper();
75
76   public SuggestionSearchEntity() {
77     super();
78   }
79
80   public void setSuggestableAttr(ArrayList<String> attributes) {
81     for (String attribute : attributes) {
82       this.suggestableAttr.add(attribute);
83     }
84   }
85
86   public void setPayloadFromResponse(JsonNode node) {
87     Map<String, String> nodePayload = new HashMap<String, String>();
88     if (suggestableAttr != null) {
89       for (String attribute : suggestableAttr) {
90         if (node.get(attribute) != null) {
91           nodePayload.put(attribute, node.get(attribute).asText());
92         }
93       }
94       this.setPayload(nodePayload);
95     }
96   }
97
98
99   public SuggestionSearchEntity(OxmModelLoader loader) {
100     super(loader);
101   }
102
103   @Override
104   public String getEntityType() {
105     return entityType;
106   }
107
108   @Override
109   public void setEntityType(String entityType) {
110     this.entityType = entityType;
111   }
112
113   public List<String> getSuggestionConnectorWords() {
114     return suggestionConnectorWords;
115   }
116
117   public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
118     this.suggestionConnectorWords = suggestionConnectorWords;
119   }
120
121   public List<String> getSuggestionPropertyTypes() {
122     return this.suggestionAttributeTypes;
123   }
124
125   public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
126     this.suggestionAttributeTypes = suggestionPropertyTypes;
127   }
128
129   public List<String> getSuggestionAttributeValues() {
130     return this.suggestionAttributeValues;
131   }
132
133   public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
134     this.suggestionAttributeValues = suggestionAttributeValues;
135   }
136
137   public List<String> getSuggestionAliases() {
138     return this.suggestionTypeAliases;
139   }
140
141   public void setSuggestionAliases(List<String> suggestionAliases) {
142     this.suggestionTypeAliases = suggestionAliases;
143   }
144
145   public List<String> getSuggestionInputPermutations() {
146     return this.suggestionInputPermutations;
147   }
148
149   public void setSuggestionInputPermutations(List<String> permutations) {
150     this.suggestionInputPermutations = permutations;
151   }
152
153   public void generateSuggestionInputPermutations() {
154
155
156     List<String> entityNames = new ArrayList<>();
157     entityNames.add(entityType);
158     HashMap<String, String> desc = loader.getOxmModel().get(this.entityType);
159     String attr = desc.get("suggestionAliases");
160     String[] suggestionAliasesArray = attr.split(",");
161     suggestionTypeAliases = Arrays.asList(suggestionAliasesArray);
162     this.setAliasToUse(suggestionAliasesArray[suggestionAliasesArray.length - 1]);
163     for (String alias : suggestionTypeAliases) {
164       entityNames.add(alias);
165     }
166     ArrayList<String> listOfSearchSuggestionPermutations = new ArrayList<>();
167
168     ArrayList<String> listToPermutate = new ArrayList<>(payload.values());
169
170     for (String entityName : entityNames) {
171       listToPermutate.add(entityName);
172       permutateList(listToPermutate, new ArrayList<String>(), listToPermutate.size(),
173           listOfSearchSuggestionPermutations);
174       listToPermutate.remove(entityName);
175     }
176     suggestionInputPermutations = listOfSearchSuggestionPermutations;
177   }
178
179   /**
180    * Generate all permutations of a list of Strings
181    * 
182    * @param list
183    * @param permutation
184    * @param size
185    */
186   private void permutateList(List<String> list, List<String> permutation, int size,
187       List<String> listOfSearchSuggestionPermutationList) {
188     if (permutation.size() == size) {
189       StringBuilder newPermutation = new StringBuilder();
190
191       for (int i = 0; i < permutation.size(); i++) {
192         newPermutation.append(permutation.get(i)).append(" ");
193       }
194
195       listOfSearchSuggestionPermutationList.add(newPermutation.toString().trim());
196
197       return;
198     }
199
200     String[] availableItems = list.toArray(new String[0]);
201
202     for (String i : availableItems) {
203       permutation.add(i);
204       list.remove(i);
205       permutateList(list, permutation, size, listOfSearchSuggestionPermutationList);
206       list.add(i);
207       permutation.remove(i);
208     }
209   }
210
211   public boolean isSuggestableDoc() {
212     return this.getPayload().size() != 0;
213   }
214
215
216   @Override
217   public void deriveFields() {
218     
219     int payloadEntryCounter = 1;
220     for (Map.Entry<String, String> payload : getPayload().entrySet()) {
221       // Add the payload(status) only if a valid value is present
222       if (payload.getValue() != null &&payload.getValue().length() > 0) {
223         this.getPayloadJsonNode().put(payload.getKey(), payload.getValue());
224         this.outputString.append(payload.getValue());
225         if (payloadEntryCounter < getPayload().entrySet().size()) {
226           this.outputString.append(" and ");
227         } else{
228           this.outputString.append(" ");
229         }
230       }
231       payloadEntryCounter++;
232     }
233     
234     this.outputString.append(this.getAliasToUse());
235     this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
236   }
237
238   @Override
239   public String getIndexDocumentJson() {
240     // TODO Auto-generated method stub
241     JSONObject rootNode = new JSONObject();
242
243     JSONArray suggestionsArray = new JSONArray();
244     for (String suggestion : suggestionInputPermutations) {
245       suggestionsArray.put(suggestion);
246     }
247
248     JSONObject entitySuggest = new JSONObject();
249
250     entitySuggest.put("input", suggestionsArray);
251     entitySuggest.put("output", this.outputString);
252     entitySuggest.put("payload", this.payloadJsonNode);
253     rootNode.put("entity_suggest", entitySuggest);
254
255     return rootNode.toString();
256   }
257
258   @Override
259   public ObjectNode getBulkImportEntity() {
260     // TODO Auto-generated method stub
261     return null;
262   }
263
264   public String getAliasToUse() {
265     return aliasToUse;
266   }
267
268   public void setAliasToUse(String aliasToUse) {
269     this.aliasToUse = aliasToUse;
270   }
271
272   @Override
273   public String toString() {
274     return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
275         + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
276         + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
277         + suggestionTypeAliases + ", mapper=" + mapper + "]";
278   }
279 }