2 * ============LICENSE_START=======================================================
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.openecomp.datarouter.entity;
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;
35 import org.json.JSONArray;
36 import org.json.JSONObject;
37 import org.openecomp.datarouter.util.NodeUtils;
39 import com.fasterxml.jackson.databind.JsonNode;
41 public class SuggestionSearchEntity implements DocumentStoreDataEntity, Serializable {
42 private static final long serialVersionUID = -3636393943669310760L;
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();
54 public void deriveFields() throws NoSuchAlgorithmException {
55 int payloadEntryCounter = 1;
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());
62 if (payloadEntryCounter < getPayload().entrySet().size()) {
63 this.outputString.append(" and ");
65 this.outputString.append(" ");
69 payloadEntryCounter++;
72 this.outputString.append(getEntityTypeAliases().get(0));
73 this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
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.
81 public List<String> generateSuggestionInputPermutations() {
82 List<String> entityNames = new ArrayList<>();
83 entityNames.add(entityType);
85 if ((entityTypeAliases != null) && !(entityTypeAliases.isEmpty())) {
86 for (String alias : entityTypeAliases) {
87 entityNames.add(alias);
91 ArrayList<String> listToPermutate = new ArrayList<>(statusPermutations);
92 ArrayList<String> listOfSearchSuggestionPermutations = new ArrayList<>();
94 for (String entityName : entityNames) {
95 listToPermutate.add(entityName);
96 permutateList(listToPermutate, new ArrayList<String>(), listToPermutate.size(), listOfSearchSuggestionPermutations);
97 listToPermutate.remove(entityName);
100 return listOfSearchSuggestionPermutations;
103 public boolean isSuggestableDoc() {
104 return this.getPayload().size() != 0;
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
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();
119 for (int i = 0; i < permutation.size(); i++) {
120 newPermutation.append(permutation.get(i)).append(" ");
123 listOfSearchSuggestionPermutationList.add(newPermutation.toString().trim());
128 String[] availableItems = list.toArray(new String[0]);
130 for (String i : availableItems) {
133 permutateList(list, permutation, size, listOfSearchSuggestionPermutationList);
135 permutation.remove(i);
140 * return Custom-built JSON representation of this class
143 public String getAsJson() throws IOException {
144 if (entityType == null || suggestionInputPermutations == null) {
148 JSONObject rootNode = new JSONObject();
149 JSONArray inputArray = new JSONArray();
150 JSONObject payloadNode = new JSONObject();
151 StringBuffer outputString = new StringBuffer();
153 int payloadEntryCounter = 1;
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());
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++;
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));
170 for (String permutation : suggestionInputPermutations) {
171 inputArray.put(permutation);
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);
181 return rootNode.toString();
184 public String getEntityType() {
188 public void setEntityType(String entityType) {
189 this.entityType = entityType;
192 public List<String> getEntityTypeAliases() {
193 return entityTypeAliases;
196 public void setEntityTypeAliases(List<String> entityTypeAliases) {
197 this.entityTypeAliases = entityTypeAliases;
201 public String getId() {
205 public void setId(String id) {
209 public StringBuffer getOutputString() {
213 public void setOutputString(StringBuffer outputString) {
214 this.outputString = outputString;
217 public Map<String, String> getPayload() {
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());
231 this.setPayload(nodePayload);
235 public void setPayload(Map<String, String> payload) {
236 this.payload = payload;
239 public JSONObject getPayloadJsonNode() {
240 return payloadJsonNode;
243 public void setPayloadJsonNode(JSONObject payloadJsonNode) {
244 this.payloadJsonNode = payloadJsonNode;
247 public List<String> getStatusPermutations() {
248 return statusPermutations;
251 public List<String> getSuggestableAttr() {
252 return suggestableAttr;
255 public List<String> getSuggestionInputPermutations() {
256 return this.suggestionInputPermutations;
259 public void setStatusPermutations(List<String> statusPermutations) {
260 this.statusPermutations = statusPermutations;
263 public void setSuggestableAttr(ArrayList<String> attributes) {
264 for (String attribute : attributes) {
265 this.suggestableAttr.add(attribute);
269 public void setSuggestionInputPermutations(List<String> permutations) {
270 this.suggestionInputPermutations = permutations;
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 + "]";