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