2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.synchronizer.entity;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.fasterxml.jackson.databind.node.ObjectNode;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.List;
35 import org.json.JSONArray;
36 import org.json.JSONObject;
37 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
38 import org.onap.aai.sparky.util.NodeUtils;
40 public class SuggestionSearchEntity extends IndexableEntity implements IndexDocument {
42 private String entityType;
43 private List<String> suggestionConnectorWords = new ArrayList<String>();
44 private List<String> suggestionAttributeTypes = new ArrayList<String>();
47 * @return the suggestionAttributeTypes
49 public List<String> getSuggestionAttributeTypes() {
50 return suggestionAttributeTypes;
54 * @param suggestionAttributeTypes the suggestionAttributeTypes to set
56 public void setSuggestionAttributeTypes(List<String> suggestionAttributeTypes) {
57 this.suggestionAttributeTypes = suggestionAttributeTypes;
61 * @return the suggestionTypeAliases
63 public List<String> getSuggestionTypeAliases() {
64 return suggestionTypeAliases;
68 * @param suggestionTypeAliases the suggestionTypeAliases to set
70 public void setSuggestionTypeAliases(List<String> suggestionTypeAliases) {
71 this.suggestionTypeAliases = suggestionTypeAliases;
75 * @return the suggestableAttr
77 public List<String> getSuggestableAttr() {
78 return suggestableAttr;
82 * @param suggestableAttr the suggestableAttr to set
84 public void setSuggestableAttr(List<String> suggestableAttr) {
85 this.suggestableAttr = suggestableAttr;
89 * @return the outputString
91 public StringBuffer getOutputString() {
96 * @param outputString the outputString to set
98 public void setOutputString(StringBuffer outputString) {
99 this.outputString = outputString;
105 public ObjectMapper getMapper() {
110 * @param mapper the mapper to set
112 public void setMapper(ObjectMapper mapper) {
113 this.mapper = mapper;
117 private List<String> suggestionAttributeValues = new ArrayList<String>();
118 private List<String> suggestionTypeAliases = new ArrayList<String>();
119 private List<String> suggestionInputPermutations = new ArrayList<String>();
120 private List<String> suggestableAttr = new ArrayList<String>();
121 private Map<String, String> payload = new HashMap<String, String>();
122 private JSONObject payloadJsonNode = new JSONObject();
123 private StringBuffer outputString = new StringBuffer();
124 private String aliasToUse;
126 public Map<String, String> getPayload() {
130 public void setPayload(Map<String, String> payload) {
131 this.payload = payload;
135 public JSONObject getPayloadJsonNode() {
136 return payloadJsonNode;
139 public void setPayloadJsonNode(JSONObject payloadJsonNode) {
140 this.payloadJsonNode = payloadJsonNode;
144 protected ObjectMapper mapper = new ObjectMapper();
146 public SuggestionSearchEntity() {
150 public void setSuggestableAttr(ArrayList<String> attributes) {
151 for (String attribute : attributes) {
152 this.suggestableAttr.add(attribute);
156 public void setPayloadFromResponse(JsonNode node) {
157 Map<String, String> nodePayload = new HashMap<String, String>();
158 if (suggestableAttr != null) {
159 for (String attribute : suggestableAttr) {
160 if (node.get(attribute) != null) {
161 nodePayload.put(attribute, node.get(attribute).asText());
164 this.setPayload(nodePayload);
169 public SuggestionSearchEntity(OxmModelLoader loader) {
174 public String getEntityType() {
179 public void setEntityType(String entityType) {
180 this.entityType = entityType;
183 public List<String> getSuggestionConnectorWords() {
184 return suggestionConnectorWords;
187 public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
188 this.suggestionConnectorWords = suggestionConnectorWords;
191 public List<String> getSuggestionPropertyTypes() {
192 return this.suggestionAttributeTypes;
195 public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
196 this.suggestionAttributeTypes = suggestionPropertyTypes;
199 public List<String> getSuggestionAttributeValues() {
200 return this.suggestionAttributeValues;
203 public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
204 this.suggestionAttributeValues = suggestionAttributeValues;
207 public List<String> getSuggestionAliases() {
208 return this.suggestionTypeAliases;
211 public void setSuggestionAliases(List<String> suggestionAliases) {
212 this.suggestionTypeAliases = suggestionAliases;
215 public List<String> getSuggestionInputPermutations() {
216 return this.suggestionInputPermutations;
219 public void setSuggestionInputPermutations(List<String> permutations) {
220 this.suggestionInputPermutations = permutations;
223 public void generateSuggestionInputPermutations() {
226 List<String> entityNames = new ArrayList<>();
227 entityNames.add(entityType);
228 HashMap<String, String> desc = loader.getOxmModel().get(this.entityType);
229 String attr = desc.get("suggestionAliases");
230 String[] suggestionAliasesArray = attr.split(",");
231 suggestionTypeAliases = Arrays.asList(suggestionAliasesArray);
232 this.setAliasToUse(suggestionAliasesArray[suggestionAliasesArray.length - 1]);
233 for (String alias : suggestionTypeAliases) {
234 entityNames.add(alias);
236 ArrayList<String> listOfSearchSuggestionPermutations = new ArrayList<>();
238 ArrayList<String> listToPermutate = new ArrayList<>(payload.values());
240 for (String entityName : entityNames) {
241 listToPermutate.add(entityName);
242 permutateList(listToPermutate, new ArrayList<String>(), listToPermutate.size(),
243 listOfSearchSuggestionPermutations);
244 listToPermutate.remove(entityName);
246 suggestionInputPermutations = listOfSearchSuggestionPermutations;
250 * Generate all permutations of a list of Strings
256 private void permutateList(List<String> list, List<String> permutation, int size,
257 List<String> listOfSearchSuggestionPermutationList) {
258 if (permutation.size() == size) {
259 StringBuilder newPermutation = new StringBuilder();
261 for (int i = 0; i < permutation.size(); i++) {
262 newPermutation.append(permutation.get(i)).append(" ");
265 listOfSearchSuggestionPermutationList.add(newPermutation.toString().trim());
270 String[] availableItems = list.toArray(new String[0]);
272 for (String i : availableItems) {
275 permutateList(list, permutation, size, listOfSearchSuggestionPermutationList);
277 permutation.remove(i);
281 public boolean isSuggestableDoc() {
282 return this.getPayload().size() != 0;
287 public void deriveFields() {
289 int payloadEntryCounter = 1;
290 for (Map.Entry<String, String> payload : getPayload().entrySet()) {
291 // Add the payload(status) only if a valid value is present
292 if (payload.getValue() != null && payload.getValue().length() > 0) {
293 this.getPayloadJsonNode().put(payload.getKey(), payload.getValue());
294 this.outputString.append(payload.getValue());
295 if (payloadEntryCounter < getPayload().entrySet().size()) {
296 this.outputString.append(" and ");
298 this.outputString.append(" ");
301 payloadEntryCounter++;
304 this.outputString.append(this.getAliasToUse());
305 this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
309 public String getIndexDocumentJson() {
310 // TODO Auto-generated method stub
311 JSONObject rootNode = new JSONObject();
313 JSONArray suggestionsArray = new JSONArray();
314 for (String suggestion : suggestionInputPermutations) {
315 suggestionsArray.put(suggestion);
318 JSONObject entitySuggest = new JSONObject();
320 entitySuggest.put("input", suggestionsArray);
321 entitySuggest.put("output", this.outputString);
322 entitySuggest.put("payload", this.payloadJsonNode);
323 rootNode.put("entity_suggest", entitySuggest);
325 return rootNode.toString();
329 public ObjectNode getBulkImportEntity() {
330 // TODO Auto-generated method stub
334 public String getAliasToUse() {
338 public void setAliasToUse(String aliasToUse) {
339 this.aliasToUse = aliasToUse;
343 public String toString() {
344 return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
345 + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
346 + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
347 + suggestionTypeAliases + ", mapper=" + mapper + "]";