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.util.List;
28 import org.apache.camel.Exchange;
29 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
30 import org.onap.aai.datarouter.entity.SpikeEventEntity;
31 import org.onap.aai.datarouter.entity.SpikeEventVertex;
32 import org.onap.aai.datarouter.logging.EntityEventPolicyMsgs;
35 public class SpikeEntitySearchProcessor extends AbstractSpikeEntityEventProcessor {
37 public static final String additionalInfo = "Response of SpikeEntityEventPolicy";
38 private static final String searchIndexSchema = "";
42 /** Agent for communicating with the Search Service. */
44 public SpikeEntitySearchProcessor(SpikeEventPolicyConfig config)
45 throws FileNotFoundException {
51 protected void startup() {
52 // Create the indexes in the search service if they do not already exist.
53 searchAgent.createSearchIndex(searchIndexName, searchIndexSchema);
54 logger.info(EntityEventPolicyMsgs.ENTITY_EVENT_POLICY_REGISTERED);
58 public void process(Exchange exchange) throws Exception {
60 long startTime = System.currentTimeMillis();
61 String uebPayload = getExchangeBody(exchange);
62 if (uebPayload == null) {
66 String action = getSpikeEventAction(exchange, uebPayload);
70 SpikeEventVertex eventVertex = populateEventVertex(exchange, uebPayload);
71 if (eventVertex == null) {
74 String entityType = getEntityType(exchange, eventVertex, uebPayload);
75 if (entityType == null) {
78 String entityLink = getEntityLink(exchange, eventVertex, uebPayload);
79 if (entityLink == null) {
82 DynamicJAXBContext oxmJaxbContext = readOxm(exchange, uebPayload);
83 if (oxmJaxbContext == null) {
86 String oxmEntityType = getOxmEntityType(entityType);
87 List<String> searchableAttr = getSearchableAttibutes(oxmJaxbContext, oxmEntityType, entityType, uebPayload,
89 if (searchableAttr == null) {
93 // log the fact that all data are in good shape
94 logger.info(EntityEventPolicyMsgs.PROCESS_ENTITY_EVENT_POLICY_NONVERBOSE, action, entityType);
95 logger.debug(EntityEventPolicyMsgs.PROCESS_ENTITY_EVENT_POLICY_VERBOSE, action, entityType,
98 SpikeEventEntity spikeEventEntity = new SpikeEventEntity();
99 spikeEventEntity.setEntityType(entityType);
100 spikeEventEntity.setLink(entityLink);
101 spikeEventEntity = populateSpikeEventEntity(exchange, spikeEventEntity, oxmJaxbContext,
102 entityType, action, uebPayload, oxmEntityType,searchableAttr);
103 if (spikeEventEntity == null) {
107 handleSearchServiceOperation(spikeEventEntity, action, searchIndexName);
108 long stopTime = System.currentTimeMillis();
109 metricsLogger.info(EntityEventPolicyMsgs.OPERATION_RESULT_NO_ERRORS, PROCESS_SPIKE_EVENT,
110 String.valueOf(stopTime - startTime));
111 setResponse(exchange, ResponseType.SUCCESS, additionalInfo);
116 * This is not for this Scope. We get back to it later. (updateCerInEntity) private void
117 * updateSearchEntityWithCrossEntityReference(SpikeEventEntity spikeEventEntity) { try {
118 * Map<String, List<String>> headers = new HashMap<>(); headers.put(Headers.FROM_APP_ID,
119 * Arrays.asList("Data Router")); headers.put(Headers.TRANSACTION_ID,
120 * Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
122 * String entityId = spikeEventEntity.getId(); String jsonPayload;
124 * // Run the GET to retrieve the ETAG from the search service OperationResult storedEntity =
125 * searchAgent.getDocument(entitySearchIndex, entityId);
127 * if (HttpUtil.isHttpResponseClassSuccess(storedEntity.getResultCode())) { /* NOTES:
128 * aaiEventEntity (ie the nested entity) may contain a subset of properties of the pre-existing
129 * object, so all we want to do is update the CER on the pre-existing object (if needed).
132 * List<String> etag = storedEntity.getHeaders().get(Headers.ETAG);
134 * if (etag != null && !etag.isEmpty()) { headers.put(Headers.IF_MATCH, etag); } else {
135 * logger.error(EntityEventPolicyMsgs.NO_ETAG_AVAILABLE_FAILURE, entitySearchIndex, entityId); }
137 * ArrayList<JsonNode> sourceObject = new ArrayList<>();
138 * NodeUtils.extractObjectsByKey(NodeUtils.convertJsonStrToJsonNode(storedEntity.getResult()),
139 * "content", sourceObject);
141 * if (!sourceObject.isEmpty()) { JsonNode node = sourceObject.get(0); final String sourceCer =
142 * NodeUtils.extractFieldValueFromObject(node, "crossEntityReferenceValues"); String newCer =
143 * spikeEventEntity.getCrossReferenceEntityValues(); boolean hasNewCer = true; if (sourceCer !=
144 * null && sourceCer.length() > 0) { // already has CER if (!sourceCer.contains(newCer)) {// don't
145 * re-add newCer = sourceCer + ";" + newCer; } else { hasNewCer = false; } }
147 * if (hasNewCer) { // Do the PUT with new CER ((ObjectNode)
148 * node).put("crossEntityReferenceValues", newCer); jsonPayload =
149 * NodeUtils.convertObjectToJson(node, false); searchAgent.putDocument(entitySearchIndex,
150 * entityId, jsonPayload, headers); } } } else {
152 * if (storedEntity.getResultCode() == 404) { // entity not found, so attempt to do a PUT
153 * searchAgent.putDocument(entitySearchIndex, entityId, spikeEventEntity.getAsJson(), headers); }
154 * else { logger.error(EntityEventPolicyMsgs.FAILED_TO_UPDATE_ENTITY_IN_DOCSTORE,
155 * spikeEventEntity.getId(), "SYNC_ENTITY"); } } } catch (IOException e) {
156 * logger.error(EntityEventPolicyMsgs.FAILED_TO_UPDATE_ENTITY_IN_DOCSTORE,
157 * spikeEventEntity.getId(), "SYNC_ENTITY"); } }