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>();
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;
54 public Map<String, String> getPayload() {
58 public void setPayload(Map<String, String> payload) {
59 this.payload = payload;
63 public JSONObject getPayloadJsonNode() {
64 return payloadJsonNode;
67 public void setPayloadJsonNode(JSONObject payloadJsonNode) {
68 this.payloadJsonNode = payloadJsonNode;
72 protected ObjectMapper mapper = new ObjectMapper();
74 public SuggestionSearchEntity() {
78 public void setSuggestableAttr(ArrayList<String> attributes) {
79 for (String attribute : attributes) {
80 this.suggestableAttr.add(attribute);
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());
92 this.setPayload(nodePayload);
97 public SuggestionSearchEntity(OxmModelLoader loader) {
102 public String getEntityType() {
107 public void setEntityType(String entityType) {
108 this.entityType = entityType;
111 public List<String> getSuggestionConnectorWords() {
112 return suggestionConnectorWords;
115 public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
116 this.suggestionConnectorWords = suggestionConnectorWords;
119 public List<String> getSuggestionPropertyTypes() {
120 return this.suggestionAttributeTypes;
123 public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
124 this.suggestionAttributeTypes = suggestionPropertyTypes;
127 public List<String> getSuggestionAttributeValues() {
128 return this.suggestionAttributeValues;
131 public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
132 this.suggestionAttributeValues = suggestionAttributeValues;
135 public List<String> getSuggestionAliases() {
136 return this.suggestionTypeAliases;
139 public void setSuggestionAliases(List<String> suggestionAliases) {
140 this.suggestionTypeAliases = suggestionAliases;
143 public List<String> getSuggestionInputPermutations() {
144 return this.suggestionInputPermutations;
147 public void setSuggestionInputPermutations(List<String> permutations) {
148 this.suggestionInputPermutations = permutations;
151 public void generateSuggestionInputPermutations() {
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);
164 ArrayList<String> listOfSearchSuggestionPermutations = new ArrayList<>();
166 ArrayList<String> listToPermutate = new ArrayList<>(payload.values());
168 for (String entityName : entityNames) {
169 listToPermutate.add(entityName);
170 permutateList(listToPermutate, new ArrayList<String>(), listToPermutate.size(),
171 listOfSearchSuggestionPermutations);
172 listToPermutate.remove(entityName);
174 suggestionInputPermutations = listOfSearchSuggestionPermutations;
178 * Generate all permutations of a list of Strings
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();
189 for (int i = 0; i < permutation.size(); i++) {
190 newPermutation.append(permutation.get(i)).append(" ");
193 listOfSearchSuggestionPermutationList.add(newPermutation.toString().trim());
198 String[] availableItems = list.toArray(new String[0]);
200 for (String i : availableItems) {
203 permutateList(list, permutation, size, listOfSearchSuggestionPermutationList);
205 permutation.remove(i);
209 public boolean isSuggestableDoc() {
210 return this.getPayload().size() != 0;
215 public void deriveFields() {
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 ");
226 this.outputString.append(" ");
229 payloadEntryCounter++;
232 this.outputString.append(this.getAliasToUse());
233 this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
237 public String getIndexDocumentJson() {
238 // TODO Auto-generated method stub
239 JSONObject rootNode = new JSONObject();
241 JSONArray suggestionsArray = new JSONArray();
242 for (String suggestion : suggestionInputPermutations) {
243 suggestionsArray.put(suggestion);
246 JSONObject entitySuggest = new JSONObject();
248 entitySuggest.put("input", suggestionsArray);
249 entitySuggest.put("output", this.outputString);
250 entitySuggest.put("payload", this.payloadJsonNode);
251 rootNode.put("entity_suggest", entitySuggest);
253 return rootNode.toString();
257 public ObjectNode getBulkImportEntity() {
258 // TODO Auto-generated method stub
262 public String getAliasToUse() {
266 public void setAliasToUse(String aliasToUse) {
267 this.aliasToUse = aliasToUse;
271 public String toString() {
272 return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
273 + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
274 + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
275 + suggestionTypeAliases + ", mapper=" + mapper + "]";