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.sync.entity;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
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;
40 import com.fasterxml.jackson.databind.JsonNode;
41 import com.fasterxml.jackson.databind.ObjectMapper;
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";
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>();
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;
63 private SuggestionEntityLookup entityLookup;
65 public JSONObject getPayload() {
69 public void setPayload(JSONObject payload) {
70 this.payload = payload;
73 protected ObjectMapper mapper = new ObjectMapper();
75 public SuggestionSearchEntity(FiltersConfig filtersConfig) {
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);
87 public SuggestionSearchEntity(FiltersConfig filtersConfig, SuggestionEntityLookup entityLookup) {
89 this.entityLookup = entityLookup;
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);
100 public SuggestionSearchEntity(SuggestionEntityLookup entityLookup, FiltersConfig config) {
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);
111 public void setSuggestableAttr(ArrayList<String> attributes) {
112 for (String attribute : attributes) {
113 this.suggestableAttr.add(attribute);
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());
129 public void setFilterBasedPayloadFromResponse(JsonNode node, String entityName, ArrayList<String> uniqueList) {
131 HashMap<String, String> desc = entityLookup.getSuggestionSearchEntityOxmModel().get(entityName);
133 if ( desc == null ) {
137 String attr = desc.get("suggestibleAttributes");
139 if ( attr == null ) {
143 List<String> suggestableAttrOxm = Arrays.asList(attr.split(","));
147 * (1) 'uniqueList' is one item within the power set of the suggestable attributes.
148 * (2) 'inputeOutputData' is used to generate permutations of strings
150 for (String selectiveAttr: uniqueList) {
151 if (node.get(selectiveAttr) != null) {
152 inputOutputData.put(selectiveAttr, node.get(selectiveAttr).asText());
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);
166 this.payload.put(attribute, node.get(attribute).asText());
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);
177 this.payload.put(FILTER_LIST, this.payloadFilters);
182 public String getEntityType() {
187 public void setEntityType(String entityType) {
188 this.entityType = entityType;
191 public List<String> getSuggestionConnectorWords() {
192 return suggestionConnectorWords;
195 public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
196 this.suggestionConnectorWords = suggestionConnectorWords;
199 public List<String> getSuggestionPropertyTypes() {
200 return this.suggestionAttributeTypes;
203 public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
204 this.suggestionAttributeTypes = suggestionPropertyTypes;
207 public List<String> getSuggestionAttributeValues() {
208 return this.suggestionAttributeValues;
211 public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
212 this.suggestionAttributeValues = suggestionAttributeValues;
215 public List<String> getSuggestionAliases() {
216 return this.suggestionTypeAliases;
219 public void setSuggestionAliases(List<String> suggestionAliases) {
220 this.suggestionTypeAliases = suggestionAliases;
223 public List<String> getSuggestionInputPermutations() {
224 return this.suggestionInputPermutations;
227 public void setSuggestionInputPermutations(List<String> permutations) {
228 this.suggestionInputPermutations = permutations;
231 public void generateSuggestionInputPermutations() {
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);
244 ArrayList<String> listToPermutate = new ArrayList<>(inputOutputData.values());
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));
252 // prepare for the next pass: remove the entity-name or alias from the list
253 listToPermutate.remove(entity);
257 public boolean isSuggestableDoc() {
258 return this.getPayload().length() != 0;
263 public void deriveFields() {
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 ");
272 this.outputString.append(" ");
278 this.outputString.append(this.getAliasToUse());
279 this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
283 public String getAsJson() {
284 // TODO Auto-generated method stub
285 JSONObject rootNode = new JSONObject();
287 JSONArray suggestionsArray = new JSONArray();
288 for (String suggestion : suggestionInputPermutations) {
289 suggestionsArray.put(suggestion);
292 JSONObject entitySuggest = new JSONObject();
294 entitySuggest.put("input", suggestionsArray);
295 entitySuggest.put("output", this.outputString);
296 entitySuggest.put("payload", this.payload);
297 rootNode.put("entity_suggest", entitySuggest);
299 return rootNode.toString();
302 public String getAliasToUse() {
306 public void setAliasToUse(String aliasToUse) {
307 this.aliasToUse = aliasToUse;
310 public Map<String, String> getInputOutputData() {
311 return inputOutputData;
314 public void setInputOutputData(Map<String, String> inputOutputData) {
315 this.inputOutputData = inputOutputData;
319 public String toString() {
320 return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
321 + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
322 + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
323 + suggestionTypeAliases + ", mapper=" + mapper + "]";