2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.sync.entity;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.List;
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.onap.aai.sparky.config.oxm.SuggestionEntityLookup;
36 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
37 import org.onap.aai.sparky.search.filters.config.FiltersDetailsConfig;
38 import org.onap.aai.sparky.search.filters.config.UiFilterConfig;
39 import org.onap.aai.sparky.util.NodeUtils;
40 import org.onap.aai.sparky.util.SuggestionsPermutation;
42 import com.fasterxml.jackson.databind.JsonNode;
43 import com.fasterxml.jackson.databind.ObjectMapper;
45 public class SuggestionSearchEntity extends IndexableEntity implements IndexDocument {
46 private static final String FILTER_ID = "filterId";
47 private static final String FILTER_VALUE = "filterValue";
48 private static final String FILTER_LIST = "filterList";
50 private String entityType;
51 private List<String> suggestionConnectorWords = new ArrayList<String>();
52 private List<String> suggestionAttributeTypes = new ArrayList<String>();
53 private List<String> suggestionAttributeValues = new ArrayList<String>();
54 private List<String> suggestionTypeAliases = new ArrayList<String>();
55 private List<String> suggestionInputPermutations = new ArrayList<String>();
56 private List<String> suggestableAttr = new ArrayList<String>();
58 private Map<String, String> inputOutputData = new HashMap<String, String>();
59 Map<String, UiFilterConfig> filters = new HashMap<String, UiFilterConfig>();
60 private JSONObject payload = new JSONObject();
61 private JSONArray payloadFilters = new JSONArray();
62 private StringBuffer outputString = new StringBuffer();
63 private String aliasToUse;
65 private SuggestionEntityLookup entityLookup;
67 public JSONObject getPayload() {
71 public void setPayload(JSONObject payload) {
72 this.payload = payload;
75 protected ObjectMapper mapper = new ObjectMapper();
77 public SuggestionSearchEntity(FiltersConfig filtersConfig) {
80 FiltersDetailsConfig filterConfigList = filtersConfig.getFiltersConfig();
81 // Populate the map with keys that will match the suggestableAttr values
82 for(UiFilterConfig filter : filterConfigList.getFilters()) {
83 if(filter.getDataSource() != null) {
84 filters.put(filter.getDataSource().getFieldName(), filter);
89 public SuggestionSearchEntity(FiltersConfig filtersConfig, SuggestionEntityLookup entityLookup) {
91 this.entityLookup = entityLookup;
93 FiltersDetailsConfig filterConfigList = filtersConfig.getFiltersConfig();
94 // Populate the map with keys that will match the suggestableAttr values
95 for(UiFilterConfig filter : filterConfigList.getFilters()) {
96 if(filter.getDataSource() != null) {
97 filters.put(filter.getDataSource().getFieldName(), filter);
102 public SuggestionSearchEntity(SuggestionEntityLookup entityLookup, FiltersConfig config) {
104 FiltersDetailsConfig filterConfigList = config.getFiltersConfig();
105 // Populate the map with keys that will match the suggestableAttr values
106 for(UiFilterConfig filter : filterConfigList.getFilters()) {
107 if(filter.getDataSource() != null) {
108 filters.put(filter.getDataSource().getFieldName(), filter);
113 public void setSuggestableAttr(ArrayList<String> attributes) {
114 for (String attribute : attributes) {
115 this.suggestableAttr.add(attribute);
119 public void setPayloadFromResponse(JsonNode node) {
120 if (suggestableAttr != null) {
121 JSONObject nodePayload = new JSONObject();
122 for (String attribute : suggestableAttr) {
123 if (node.get(attribute) != null) {
124 inputOutputData.put(attribute, node.get(attribute).asText());
125 this.payload.put(attribute, node.get(attribute).asText());
131 public void setFilterBasedPayloadFromResponse(JsonNode node, String entityName, ArrayList<String> uniqueList) {
133 HashMap<String, String> desc = entityLookup.getSuggestionSearchEntityOxmModel().get(entityName);
135 if ( desc == null ) {
139 String attr = desc.get("suggestibleAttributes");
141 if ( attr == null ) {
145 List<String> suggestableAttrOxm = Arrays.asList(attr.split(","));
149 * (1) 'uniqueList' is one item within the power set of the suggestable attributes.
150 * (2) 'inputeOutputData' is used to generate permutations of strings
152 for (String selectiveAttr: uniqueList) {
153 if (node.get(selectiveAttr) != null) {
154 inputOutputData.put(selectiveAttr, node.get(selectiveAttr).asText());
158 if (suggestableAttrOxm != null) {
159 for (String attribute : suggestableAttrOxm) {
160 if (node.get(attribute) != null && uniqueList.contains(attribute)) {
161 UiFilterConfig filterConfig = filters.get(attribute);
162 if(filterConfig != null) {
163 JSONObject filterPayload = new JSONObject();
164 filterPayload.put(FILTER_ID, filterConfig.getFilterId());
165 filterPayload.put(FILTER_VALUE, node.get(attribute).asText());
166 this.payloadFilters.put(filterPayload);
168 this.payload.put(attribute, node.get(attribute).asText());
171 UiFilterConfig emptyValueFilterConfig = filters.get(attribute);
172 if(emptyValueFilterConfig != null) {
173 JSONObject emptyValueFilterPayload = new JSONObject();
174 emptyValueFilterPayload.put(FILTER_ID, emptyValueFilterConfig.getFilterId());
175 this.payloadFilters.put(emptyValueFilterPayload);
179 this.payload.put(FILTER_LIST, this.payloadFilters);
184 public String getEntityType() {
189 public void setEntityType(String entityType) {
190 this.entityType = entityType;
193 public List<String> getSuggestionConnectorWords() {
194 return suggestionConnectorWords;
197 public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
198 this.suggestionConnectorWords = suggestionConnectorWords;
201 public List<String> getSuggestionPropertyTypes() {
202 return this.suggestionAttributeTypes;
205 public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
206 this.suggestionAttributeTypes = suggestionPropertyTypes;
209 public List<String> getSuggestionAttributeValues() {
210 return this.suggestionAttributeValues;
213 public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
214 this.suggestionAttributeValues = suggestionAttributeValues;
217 public List<String> getSuggestionAliases() {
218 return this.suggestionTypeAliases;
221 public void setSuggestionAliases(List<String> suggestionAliases) {
222 this.suggestionTypeAliases = suggestionAliases;
225 public List<String> getSuggestionInputPermutations() {
226 return this.suggestionInputPermutations;
229 public void setSuggestionInputPermutations(List<String> permutations) {
230 this.suggestionInputPermutations = permutations;
233 public void generateSuggestionInputPermutations() {
235 List<String> entityNames = new ArrayList<>();
236 entityNames.add(entityType);
237 HashMap<String, String> desc = entityLookup.getSuggestionSearchEntityOxmModel().get(this.entityType);
238 String attr = desc.get("suggestionAliases");
239 String[] suggestionAliasesArray = attr.split(",");
240 suggestionTypeAliases = Arrays.asList(suggestionAliasesArray);
241 this.setAliasToUse(suggestionAliasesArray[suggestionAliasesArray.length - 1]);
242 for (String alias : suggestionTypeAliases) {
243 entityNames.add(alias);
246 ArrayList<String> listToPermutate = new ArrayList<>(inputOutputData.values());
248 for (String entity : entityNames){
249 listToPermutate.add(entity); // add entity-name or alias in list to permutate
250 List<List<String>> lists = SuggestionsPermutation.getListPermutations(listToPermutate);
251 for (List<String> li : lists){
252 suggestionInputPermutations.add(String.join(" ", li));
254 // prepare for the next pass: remove the entity-name or alias from the list
255 listToPermutate.remove(entity);
259 public boolean isSuggestableDoc() {
260 return this.getPayload().length() != 0;
265 public void deriveFields() {
267 int entryCounter = 1;
268 for (Map.Entry<String, String> outputValue : inputOutputData.entrySet()) {
269 if (outputValue.getValue() != null && outputValue.getValue().length() > 0) {
270 this.outputString.append(outputValue.getValue());
271 if (entryCounter < inputOutputData.entrySet().size()) {
272 this.outputString.append(" and ");
274 this.outputString.append(" ");
280 this.outputString.append(this.getAliasToUse());
281 this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
285 public String getAsJson() {
286 // TODO Auto-generated method stub
287 JSONObject rootNode = new JSONObject();
289 JSONArray suggestionsArray = new JSONArray();
290 for (String suggestion : suggestionInputPermutations) {
291 suggestionsArray.put(suggestion);
294 JSONObject entitySuggest = new JSONObject();
296 entitySuggest.put("input", suggestionsArray);
297 entitySuggest.put("output", this.outputString);
298 entitySuggest.put("payload", this.payload);
299 rootNode.put("entity_suggest", entitySuggest);
301 return rootNode.toString();
304 public String getAliasToUse() {
308 public void setAliasToUse(String aliasToUse) {
309 this.aliasToUse = aliasToUse;
312 public Map<String, String> getInputOutputData() {
313 return inputOutputData;
316 public void setInputOutputData(Map<String, String> inputOutputData) {
317 this.inputOutputData = inputOutputData;
321 public String toString() {
322 return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
323 + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
324 + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
325 + suggestionTypeAliases + ", mapper=" + mapper + "]";