Initial ONAP Synapse commit
[aai/data-router.git] / src / main / java / org / openecomp / datarouter / entity / SuggestionSearchEntity.java
1 /**
2  * ============LICENSE_START=======================================================
3  * DataRouter
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.datarouter.entity;
26
27 import java.io.IOException;
28 import java.io.Serializable;
29 import java.security.NoSuchAlgorithmException;
30 import java.util.ArrayList;
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.datarouter.util.NodeUtils;
38
39 import com.fasterxml.jackson.databind.JsonNode;
40
41 public class SuggestionSearchEntity implements DocumentStoreDataEntity, Serializable {
42   private static final long serialVersionUID = -3636393943669310760L;
43
44   protected String id; // generated SHA-256 digest
45   private String entityType;
46   private List<String> entityTypeAliases;
47   private List<String> suggestionInputPermutations = new ArrayList<>();
48   private List<String> statusPermutations = new ArrayList<>();
49   private List<String> suggestableAttr = new ArrayList<>();
50   private Map<String, String> payload = new HashMap<>();
51   private JSONObject payloadJsonNode = new JSONObject();
52   private StringBuffer outputString = new StringBuffer();
53
54   public void deriveFields() throws NoSuchAlgorithmException {
55     int payloadEntryCounter = 1;
56
57     for (Map.Entry<String, String> payload : getPayload().entrySet()) {
58       if (payload.getValue() != null && payload.getValue().length() > 0) {
59         this.getPayloadJsonNode().put(payload.getKey(), payload.getValue());
60         this.outputString.append(payload.getValue());
61
62         if (payloadEntryCounter < getPayload().entrySet().size()) {
63           this.outputString.append(" and ");
64         } else {
65           this.outputString.append(" ");
66         }
67       }
68
69       payloadEntryCounter++;
70     }
71
72     this.outputString.append(getEntityTypeAliases().get(0));
73     this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
74   }
75
76   /**
77    * Launch pad for performing permutations of the entity type, aliases, prov status and orchestration status.
78    * SHA-256 will result in an ID with a guaranteed uniqueness compared to just a java hashcode value.
79    * @return
80    */
81   public List<String> generateSuggestionInputPermutations() {
82     List<String> entityNames = new ArrayList<>();
83     entityNames.add(entityType);
84
85     if ((entityTypeAliases != null) && !(entityTypeAliases.isEmpty())) {
86       for (String alias : entityTypeAliases) {
87         entityNames.add(alias);
88       }
89     }
90
91     ArrayList<String> listToPermutate = new ArrayList<>(statusPermutations);
92     ArrayList<String> listOfSearchSuggestionPermutations = new ArrayList<>();
93
94     for (String entityName : entityNames) {
95       listToPermutate.add(entityName);
96       permutateList(listToPermutate, new ArrayList<String>(), listToPermutate.size(), listOfSearchSuggestionPermutations);
97       listToPermutate.remove(entityName);
98     }
99
100     return listOfSearchSuggestionPermutations;
101   }
102
103   public boolean isSuggestableDoc() {
104     return this.getPayload().size() != 0;
105   }
106   
107   /**
108    * Generate all permutations of Entity Type and (Prov Status and/or Orchestration Status)
109    * @param list The list of unique elements to create permutations of
110    * @param permutation A list to hold the current permutation used during
111    * @param size To keep track of the original size of the number of unique elements
112    * @param listOfSearchSuggestionPermutationList The list to hold all of the different permutations
113    */
114   private void permutateList(List<String> list, List<String> permutation, int size,
115       List<String> listOfSearchSuggestionPermutationList) {
116     if (permutation.size() == size) {
117       StringBuilder newPermutation = new StringBuilder();
118
119       for (int i = 0; i < permutation.size(); i++) {
120         newPermutation.append(permutation.get(i)).append(" ");
121       }
122
123       listOfSearchSuggestionPermutationList.add(newPermutation.toString().trim());
124
125       return;
126     }
127
128     String[] availableItems = list.toArray(new String[0]);
129
130     for (String i : availableItems) {
131       permutation.add(i);
132       list.remove(i);
133       permutateList(list, permutation, size, listOfSearchSuggestionPermutationList);
134       list.add(i);
135       permutation.remove(i);
136     }
137   }
138
139   /**
140    * return Custom-built JSON representation of this class
141    */
142   @Override
143   public String getAsJson() throws IOException {
144     if (entityType == null || suggestionInputPermutations == null) {
145       return null;
146     }
147
148     JSONObject rootNode = new JSONObject();
149     JSONArray inputArray = new JSONArray();
150     JSONObject payloadNode = new JSONObject();
151     StringBuffer outputString = new StringBuffer();
152
153     int payloadEntryCounter = 1;
154
155     // Add prov and orchestration status to search suggestion string
156     for (Map.Entry<String, String> payload : getPayload().entrySet()) {
157       payloadNode.put(payload.getKey(), payload.getValue());
158       outputString.append(payload.getValue());
159
160       if (payloadEntryCounter < getPayload().entrySet().size()) {
161         // Add the word "and" between prov and orchestration statuses, if both are present
162         outputString.append(" and ");
163         payloadEntryCounter++;
164       }
165     }
166
167     // Add entity type to search suggestion string. We've decided to use the first entity type alias from the OXM
168     outputString.append(" ").append(getEntityTypeAliases().get(0));
169
170     for (String permutation : suggestionInputPermutations) {
171       inputArray.put(permutation);
172     }
173
174     // Build up the search suggestion as JSON
175     JSONObject entitySuggest = new JSONObject();
176     entitySuggest.put("input", inputArray);
177     entitySuggest.put("output", outputString);
178     entitySuggest.put("payload", payloadNode);
179     rootNode.put("entity_suggest", entitySuggest);
180
181     return rootNode.toString();
182   }
183
184   public String getEntityType() {
185     return entityType;
186   }
187
188   public void setEntityType(String entityType) {
189     this.entityType = entityType;
190   }
191
192   public List<String> getEntityTypeAliases() {
193     return entityTypeAliases;
194   }
195
196   public void setEntityTypeAliases(List<String> entityTypeAliases) {
197     this.entityTypeAliases = entityTypeAliases;
198   }
199
200   @Override
201   public String getId() {
202     return id;
203   }
204
205   public void setId(String id) {
206     this.id = id;
207   }
208
209   public StringBuffer getOutputString() {
210     return outputString;
211   }
212
213   public void setOutputString(StringBuffer outputString) {
214     this.outputString = outputString;
215   }
216
217   public Map<String, String> getPayload() {
218     return payload;
219   }
220
221   public void setPayloadFromResponse(JsonNode node) {
222     Map<String, String> nodePayload = new HashMap<>();
223     JsonNode entityNode = node.get("entity");
224     if (suggestableAttr != null) {
225       for (String attribute : suggestableAttr) {
226         if (entityNode.get(attribute) != null && !entityNode.get(attribute).asText().trim().isEmpty()) {
227           nodePayload.put(attribute, entityNode.get(attribute).asText());
228           this.statusPermutations.add(entityNode.get(attribute).asText());
229         }
230       }
231       this.setPayload(nodePayload);
232     }
233   }
234
235   public void setPayload(Map<String, String> payload) {
236     this.payload = payload;
237   }
238
239   public JSONObject getPayloadJsonNode() {
240     return payloadJsonNode;
241   }
242
243   public void setPayloadJsonNode(JSONObject payloadJsonNode) {
244     this.payloadJsonNode = payloadJsonNode;
245   }
246
247   public List<String> getStatusPermutations() {
248     return statusPermutations;
249   }
250
251   public List<String> getSuggestableAttr() {
252     return suggestableAttr;
253   }
254
255   public List<String> getSuggestionInputPermutations() {
256     return this.suggestionInputPermutations;
257   }
258
259   public void setStatusPermutations(List<String> statusPermutations) {
260     this.statusPermutations = statusPermutations;
261   }
262
263   public void setSuggestableAttr(ArrayList<String> attributes) {
264     for (String attribute : attributes) {
265       this.suggestableAttr.add(attribute);
266     }
267   }
268
269   public void setSuggestionInputPermutations(List<String> permutations) {
270     this.suggestionInputPermutations = permutations;
271   }
272
273   @Override
274   public String toString() {
275     return "SuggestionSearchEntity [id=" + id + ", entityType=" + entityType
276         + ", entityTypeAliases=" + entityTypeAliases + ", suggestionInputPermutations="
277         + suggestionInputPermutations + ", statusPermutations=" + statusPermutations
278         + ", suggestableAttr=" + suggestableAttr + ", payload=" + payload + ", payloadJsonNode="
279         + payloadJsonNode + ", outputString=" + outputString + "]";
280   }
281 }