2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.datarouter.policy;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.List;
32 import org.apache.camel.Exchange;
33 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
34 import org.json.JSONException;
35 import org.json.JSONObject;
36 import org.onap.aai.datarouter.entity.OxmEntityDescriptor;
37 import org.onap.aai.datarouter.entity.SpikeEventVertex;
38 import org.onap.aai.datarouter.entity.SuggestionSearchEntity;
39 import org.onap.aai.datarouter.logging.EntityEventPolicyMsgs;
40 import org.onap.aai.datarouter.util.EntityOxmReferenceHelper;
41 import org.onap.aai.datarouter.util.SearchSuggestionPermutation;
42 import org.onap.aai.datarouter.util.Version;
43 import org.onap.aai.datarouter.util.VersionedOxmEntities;
45 import com.fasterxml.jackson.databind.JsonNode;
48 public class SpikeAutosuggestIndexProcessor extends AbstractSpikeEntityEventProcessor {
50 public static final String additionalInfo = "Response of SpikeEntityEventPolicy";
52 private final String EVENT_VERTEX = "vertex";
54 private String oxmVersion = null;
56 /** Agent for communicating with the Search Service. */
58 public SpikeAutosuggestIndexProcessor(SpikeEventPolicyConfig config)
59 throws FileNotFoundException {
61 parseLatestOxmVersion();
65 protected void startup() {
66 // Create the indexes in the search service if they do not already exist.
67 searchAgent.createSearchIndex(searchIndexName, searchIndexSchema, createIndexUrl);
68 logger.info(EntityEventPolicyMsgs.ENTITY_EVENT_POLICY_REGISTERED);
72 public void process(Exchange exchange) throws Exception {
74 long startTime = System.currentTimeMillis();
75 String uebPayload = getExchangeBody(exchange);
76 if (uebPayload == null) {
79 JsonNode uebAsJson = null;
81 uebAsJson = mapper.readTree(uebPayload);
82 } catch (IOException e) {
83 returnWithError(exchange, uebPayload, "Invalid Payload");
87 String action = getSpikeEventAction(exchange, uebPayload);
91 JSONObject uebObjEntity = getUebContentAsJson(uebPayload, EVENT_VERTEX);
92 if (uebObjEntity == null) {
93 returnWithError(exchange, uebPayload, "Payload is missing " + EVENT_VERTEX);
97 SpikeEventVertex eventVertex = populateEventVertex(exchange, uebPayload);
98 if (eventVertex == null) {
101 String entityType = getEntityType(exchange, eventVertex, uebPayload);
102 if (entityType == null) {
105 String entityLink = getEntityLink(exchange, eventVertex, uebPayload);
106 if (entityLink == null) {
109 DynamicJAXBContext oxmJaxbContext = readOxm(exchange, uebPayload);
110 if (oxmJaxbContext == null) {
113 String oxmEntityType = getOxmEntityType(entityType);
114 List<String> searchableAttr = getSearchableAttibutes(oxmJaxbContext, oxmEntityType, entityType, uebPayload,
116 if (searchableAttr == null) {
120 // log the fact that all data are in good shape
121 logger.info(EntityEventPolicyMsgs.PROCESS_ENTITY_EVENT_POLICY_NONVERBOSE, action, entityType);
122 logger.debug(EntityEventPolicyMsgs.PROCESS_ENTITY_EVENT_POLICY_VERBOSE, action, entityType,
127 * Use the versioned OXM Entity class to get access to cross-entity reference helper collections
129 VersionedOxmEntities oxmEntities =
130 EntityOxmReferenceHelper.getInstance().getVersionedOxmEntities(Version.valueOf(oxmVersion));
133 * Process for autosuggestable entities
135 if (oxmEntities != null) {
136 Map<String, OxmEntityDescriptor> rootDescriptor =
137 oxmEntities.getSuggestableEntityDescriptors();
138 if (!rootDescriptor.isEmpty()) {
139 List<String> suggestibleAttrInPayload = new ArrayList<>();
140 List<String> suggestibleAttrInOxm = extractSuggestableAttr(oxmEntities, entityType);
141 if (suggestibleAttrInOxm != null) {
142 for (String attr: suggestibleAttrInOxm){
143 if ( uebAsJson.get("vertex").get("properties").has(attr) ){
144 suggestibleAttrInPayload.add(attr);
149 if (suggestibleAttrInPayload.isEmpty()) {
152 List<String> suggestionAliases = extractAliasForSuggestableEntity(oxmEntities, entityType);
155 * It was decided to silently ignore DELETE requests for resources we don't allow to be
156 * deleted. e.g. auto-suggestion deletion is not allowed while aggregation deletion is.
158 if (!ACTION_DELETE.equalsIgnoreCase(action)) {
159 List<ArrayList<String>> listOfValidPowerSetElements =
160 SearchSuggestionPermutation.getNonEmptyUniqueLists(suggestibleAttrInPayload);
162 // Now we have a list containing the power-set (minus empty element) for the status that are
163 // available in the payload. Try inserting a document for every combination.
164 for (ArrayList<String> list : listOfValidPowerSetElements) {
165 SuggestionSearchEntity suggestionSearchEntity = new SuggestionSearchEntity();
166 suggestionSearchEntity.setEntityType(entityType);
167 suggestionSearchEntity.setSuggestableAttr(list);
168 suggestionSearchEntity.setEntityTypeAliases(suggestionAliases);
169 suggestionSearchEntity.setFilterBasedPayloadFromResponse(uebAsJson.get("vertex").get("properties"),
170 suggestibleAttrInOxm, list);
171 suggestionSearchEntity.setSuggestionInputPermutations(
172 suggestionSearchEntity.generateSuggestionInputPermutations());
174 if (suggestionSearchEntity.isSuggestableDoc()) {
176 suggestionSearchEntity.generateSearchSuggestionDisplayStringAndId();
177 } catch (NoSuchAlgorithmException e) {
178 logger.error(EntityEventPolicyMsgs.DISCARD_UPDATING_SEARCH_SUGGESTION_DATA,
179 "Cannot create unique SHA digest for search suggestion data. Exception: "
180 + e.getLocalizedMessage());
183 handleSearchServiceOperation(suggestionSearchEntity, action, searchIndexName);
189 long stopTime = System.currentTimeMillis();
190 metricsLogger.info(EntityEventPolicyMsgs.OPERATION_RESULT_NO_ERRORS, PROCESS_SPIKE_EVENT,
191 String.valueOf(stopTime - startTime));
192 setResponse(exchange, ResponseType.SUCCESS, additionalInfo);
196 public List<String> extractSuggestableAttr(VersionedOxmEntities oxmEntities, String entityType) {
197 // Extract suggestable attributeshandleTopographicalData
198 Map<String, OxmEntityDescriptor> rootDescriptor = oxmEntities.getSuggestableEntityDescriptors();
200 if (rootDescriptor == null) {
201 return Collections.emptyList();
204 OxmEntityDescriptor desc = rootDescriptor.get(entityType);
207 return Collections.emptyList();
210 return desc.getSuggestableAttributes();
214 public List<String> extractAliasForSuggestableEntity(VersionedOxmEntities oxmEntities,
218 Map<String, OxmEntityDescriptor> rootDescriptor = oxmEntities.getEntityAliasDescriptors();
220 if (rootDescriptor == null) {
221 return Collections.emptyList();
224 OxmEntityDescriptor desc = rootDescriptor.get(entityType);
225 return desc.getAlias();
228 private void parseLatestOxmVersion() {
229 int latestVersion = -1;
230 if (oxmVersionContextMap != null) {
231 Iterator it = oxmVersionContextMap.entrySet().iterator();
232 while (it.hasNext()) {
233 Map.Entry pair = (Map.Entry) it.next();
235 String version = pair.getKey().toString();
236 int versionNum = Integer.parseInt(version.substring(1, version.length()));
238 if (versionNum > latestVersion) {
239 latestVersion = versionNum;
240 oxmVersion = pair.getKey().toString();
243 logger.info(EntityEventPolicyMsgs.PROCESS_OXM_MODEL_FOUND, pair.getKey().toString());
246 logger.error(EntityEventPolicyMsgs.PROCESS_OXM_MODEL_MISSING, "");