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.datarouter.policy;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.security.NoSuchAlgorithmException;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Iterator;
31 import java.util.List;
34 import org.apache.camel.Exchange;
35 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
36 import org.json.JSONException;
37 import org.json.JSONObject;
38 import org.onap.aai.datarouter.entity.OxmEntityDescriptor;
39 import org.onap.aai.datarouter.entity.SpikeEventVertex;
40 import org.onap.aai.datarouter.entity.SuggestionSearchEntity;
41 import org.onap.aai.datarouter.logging.EntityEventPolicyMsgs;
42 import org.onap.aai.datarouter.util.EntityOxmReferenceHelper;
43 import org.onap.aai.datarouter.util.SearchSuggestionPermutation;
44 import org.onap.aai.datarouter.util.Version;
45 import org.onap.aai.datarouter.util.VersionedOxmEntities;
47 import com.fasterxml.jackson.databind.JsonNode;
50 public class SpikeAutosuggestIndexProcessor extends AbstractSpikeEntityEventProcessor {
52 public static final String additionalInfo = "Response of SpikeEntityEventPolicy";
54 private final String EVENT_VERTEX = "vertex";
56 private String oxmVersion = null;
58 /** Agent for communicating with the Search Service. */
60 public SpikeAutosuggestIndexProcessor(SpikeEventPolicyConfig config)
61 throws FileNotFoundException {
63 parseLatestOxmVersion();
67 protected void startup() {
68 // Create the indexes in the search service if they do not already exist.
69 searchAgent.createSearchIndex(searchIndexName, searchIndexSchema, createIndexUrl);
70 logger.info(EntityEventPolicyMsgs.ENTITY_EVENT_POLICY_REGISTERED);
74 public void process(Exchange exchange) throws Exception {
76 long startTime = System.currentTimeMillis();
77 String uebPayload = getExchangeBody(exchange);
78 if (uebPayload == null) {
81 JsonNode uebAsJson = null;
83 uebAsJson = mapper.readTree(uebPayload);
84 } catch (IOException e) {
85 returnWithError(exchange, uebPayload, "Invalid Payload");
89 String action = getSpikeEventAction(exchange, uebPayload);
93 JSONObject uebObjEntity = getUebContentAsJson(uebPayload, EVENT_VERTEX);
94 if (uebObjEntity == null) {
95 returnWithError(exchange, uebPayload, "Payload is missing " + EVENT_VERTEX);
99 SpikeEventVertex eventVertex = populateEventVertex(exchange, uebPayload);
100 if (eventVertex == null) {
103 String entityType = getEntityType(exchange, eventVertex, uebPayload);
104 if (entityType == null) {
107 String entityLink = getEntityLink(exchange, eventVertex, uebPayload);
108 if (entityLink == null) {
111 DynamicJAXBContext oxmJaxbContext = readOxm(exchange, uebPayload);
112 if (oxmJaxbContext == null) {
115 String oxmEntityType = getOxmEntityType(entityType);
116 List<String> searchableAttr = getSearchableAttibutes(oxmJaxbContext, oxmEntityType, entityType, uebPayload,
118 if (searchableAttr == null) {
122 // log the fact that all data are in good shape
123 logger.info(EntityEventPolicyMsgs.PROCESS_ENTITY_EVENT_POLICY_NONVERBOSE, action, entityType);
124 logger.debug(EntityEventPolicyMsgs.PROCESS_ENTITY_EVENT_POLICY_VERBOSE, action, entityType,
129 * Use the versioned OXM Entity class to get access to cross-entity reference helper collections
131 VersionedOxmEntities oxmEntities =
132 EntityOxmReferenceHelper.getInstance().getVersionedOxmEntities(Version.valueOf(oxmVersion));
135 * Process for autosuggestable entities
137 if (oxmEntities != null) {
138 Map<String, OxmEntityDescriptor> rootDescriptor =
139 oxmEntities.getSuggestableEntityDescriptors();
140 if (!rootDescriptor.isEmpty()) {
141 List<String> suggestibleAttrInPayload = new ArrayList<>();
142 List<String> suggestibleAttrInOxm = extractSuggestableAttr(oxmEntities, entityType);
143 if (suggestibleAttrInOxm != null) {
144 for (String attr: suggestibleAttrInOxm){
145 if ( uebAsJson.get("vertex").get("properties").has(attr) ){
146 suggestibleAttrInPayload.add(attr);
151 if (suggestibleAttrInPayload.isEmpty()) {
154 List<String> suggestionAliases = extractAliasForSuggestableEntity(oxmEntities, entityType);
157 * It was decided to silently ignore DELETE requests for resources we don't allow to be
158 * deleted. e.g. auto-suggestion deletion is not allowed while aggregation deletion is.
160 if (!ACTION_DELETE.equalsIgnoreCase(action)) {
161 List<ArrayList<String>> listOfValidPowerSetElements =
162 SearchSuggestionPermutation.getNonEmptyUniqueLists(suggestibleAttrInPayload);
164 // Now we have a list containing the power-set (minus empty element) for the status that are
165 // available in the payload. Try inserting a document for every combination.
166 for (ArrayList<String> list : listOfValidPowerSetElements) {
167 SuggestionSearchEntity suggestionSearchEntity = new SuggestionSearchEntity();
168 suggestionSearchEntity.setEntityType(entityType);
169 suggestionSearchEntity.setSuggestableAttr(list);
170 suggestionSearchEntity.setEntityTypeAliases(suggestionAliases);
171 suggestionSearchEntity.setFilterBasedPayloadFromResponse(uebAsJson.get("vertex").get("properties"),
172 suggestibleAttrInOxm, list);
173 suggestionSearchEntity.setSuggestionInputPermutations(
174 suggestionSearchEntity.generateSuggestionInputPermutations());
176 if (suggestionSearchEntity.isSuggestableDoc()) {
178 suggestionSearchEntity.generateSearchSuggestionDisplayStringAndId();
179 } catch (NoSuchAlgorithmException e) {
180 logger.error(EntityEventPolicyMsgs.DISCARD_UPDATING_SEARCH_SUGGESTION_DATA,
181 "Cannot create unique SHA digest for search suggestion data. Exception: "
182 + e.getLocalizedMessage());
185 handleSearchServiceOperation(suggestionSearchEntity, action, searchIndexName);
191 long stopTime = System.currentTimeMillis();
192 metricsLogger.info(EntityEventPolicyMsgs.OPERATION_RESULT_NO_ERRORS, PROCESS_SPIKE_EVENT,
193 String.valueOf(stopTime - startTime));
194 setResponse(exchange, ResponseType.SUCCESS, additionalInfo);
198 public List<String> extractSuggestableAttr(VersionedOxmEntities oxmEntities, String entityType) {
199 // Extract suggestable attributeshandleTopographicalData
200 Map<String, OxmEntityDescriptor> rootDescriptor = oxmEntities.getSuggestableEntityDescriptors();
202 if (rootDescriptor == null) {
203 return Collections.emptyList();
206 OxmEntityDescriptor desc = rootDescriptor.get(entityType);
209 return Collections.emptyList();
212 return desc.getSuggestableAttributes();
216 public List<String> extractAliasForSuggestableEntity(VersionedOxmEntities oxmEntities,
220 Map<String, OxmEntityDescriptor> rootDescriptor = oxmEntities.getEntityAliasDescriptors();
222 if (rootDescriptor == null) {
223 return Collections.emptyList();
226 OxmEntityDescriptor desc = rootDescriptor.get(entityType);
227 return desc.getAlias();
230 private void parseLatestOxmVersion() {
231 int latestVersion = -1;
232 if (oxmVersionContextMap != null) {
233 Iterator it = oxmVersionContextMap.entrySet().iterator();
234 while (it.hasNext()) {
235 Map.Entry pair = (Map.Entry) it.next();
237 String version = pair.getKey().toString();
238 int versionNum = Integer.parseInt(version.substring(1, version.length()));
240 if (versionNum > latestVersion) {
241 latestVersion = versionNum;
242 oxmVersion = pair.getKey().toString();
245 logger.info(EntityEventPolicyMsgs.PROCESS_OXM_MODEL_FOUND, pair.getKey().toString());
248 logger.error(EntityEventPolicyMsgs.PROCESS_OXM_MODEL_MISSING, "");