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.util.List;
26 import org.apache.camel.Exchange;
27 import org.onap.aai.datarouter.entity.SpikeEventEntity;
28 import org.onap.aai.datarouter.entity.SpikeEventMeta;
29 import org.onap.aai.datarouter.logging.EntityEventPolicyMsgs;
32 public class SpikeEntitySearchProcessor extends AbstractSpikeEntityEventProcessor {
34 public static final String additionalInfo = "Response of SpikeEntityEventPolicy";
36 private static final String PROCESS_SPIKE_EVENT = "Process Spike Event";
38 /** Agent for communicating with the Search Service. */
40 public SpikeEntitySearchProcessor(SpikeEventPolicyConfig config) throws FileNotFoundException {
46 protected void startup() {
47 // Create the indexes in the search service if they do not already exist.
48 searchAgent.createSearchIndex(searchIndexName, searchIndexSchema);
49 logger.info(EntityEventPolicyMsgs.ENTITY_EVENT_POLICY_REGISTERED);
53 public void process(Exchange exchange) throws Exception {
55 long startTime = System.currentTimeMillis();
57 SpikeEventMeta meta = processSpikeEvent(exchange);
63 String oxmEntityType = getOxmEntityType(meta.getSpikeEventVertex().getType());
64 List<String> searchableAttr = getSearchableAttibutes(meta.getOxmJaxbContext(), oxmEntityType,
65 meta.getSpikeEventVertex().getType(), meta.getEventEntity().toString(), exchange);
66 if (searchableAttr == null) {
71 SpikeEventEntity spikeEventEntity = new SpikeEventEntity();
72 spikeEventEntity.setEntityType(meta.getSpikeEventVertex().getType());
73 spikeEventEntity.setLink(meta.getSpikeEventVertex().getEntityLink());
74 spikeEventEntity = populateSpikeEventEntity(exchange, spikeEventEntity,
75 meta.getOxmJaxbContext(), meta.getSpikeEventVertex().getType(), meta.getBodyOperationType(),
76 meta.getVertexProperties().toString(), oxmEntityType, searchableAttr);
78 if (spikeEventEntity == null) {
82 handleSearchServiceOperation(spikeEventEntity, meta.getBodyOperationType(), searchIndexName);
83 long stopTime = System.currentTimeMillis();
84 metricsLogger.info(EntityEventPolicyMsgs.OPERATION_RESULT_NO_ERRORS, PROCESS_SPIKE_EVENT,
85 String.valueOf(stopTime - startTime));
86 setResponse(exchange, ResponseType.SUCCESS, additionalInfo);
92 * This is not for this Scope. We get back to it later. (updateCerInEntity) private void
93 * updateSearchEntityWithCrossEntityReference(SpikeEventEntity spikeEventEntity) { try {
94 * Map<String, List<String>> headers = new HashMap<>(); headers.put(Headers.FROM_APP_ID,
95 * Arrays.asList("Data Router")); headers.put(Headers.TRANSACTION_ID,
96 * Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
98 * String entityId = spikeEventEntity.getId(); String jsonPayload;
100 * // Run the GET to retrieve the ETAG from the search service OperationResult storedEntity =
101 * searchAgent.getDocument(entitySearchIndex, entityId);
103 * if (HttpUtil.isHttpResponseClassSuccess(storedEntity.getResultCode())) { /* NOTES:
104 * aaiEventEntity (ie the nested entity) may contain a subset of properties of the pre-existing
105 * object, so all we want to do is update the CER on the pre-existing object (if needed).
108 * List<String> etag = storedEntity.getHeaders().get(Headers.ETAG);
110 * if (etag != null && !etag.isEmpty()) { headers.put(Headers.IF_MATCH, etag); } else {
111 * logger.error(EntityEventPolicyMsgs.NO_ETAG_AVAILABLE_FAILURE, entitySearchIndex, entityId); }
113 * ArrayList<JsonNode> sourceObject = new ArrayList<>();
114 * NodeUtils.extractObjectsByKey(NodeUtils.convertJsonStrToJsonNode(storedEntity.getResult()),
115 * "content", sourceObject);
117 * if (!sourceObject.isEmpty()) { JsonNode node = sourceObject.get(0); final String sourceCer =
118 * NodeUtils.extractFieldValueFromObject(node, "crossEntityReferenceValues"); String newCer =
119 * spikeEventEntity.getCrossReferenceEntityValues(); boolean hasNewCer = true; if (sourceCer !=
120 * null && sourceCer.length() > 0) { // already has CER if (!sourceCer.contains(newCer)) {// don't
121 * re-add newCer = sourceCer + ";" + newCer; } else { hasNewCer = false; } }
123 * if (hasNewCer) { // Do the PUT with new CER ((ObjectNode)
124 * node).put("crossEntityReferenceValues", newCer); jsonPayload =
125 * NodeUtils.convertObjectToJson(node, false); searchAgent.putDocument(entitySearchIndex,
126 * entityId, jsonPayload, headers); } } } else {
128 * if (storedEntity.getResultCode() == 404) { // entity not found, so attempt to do a PUT
129 * searchAgent.putDocument(entitySearchIndex, entityId, spikeEventEntity.getAsJson(), headers); }
130 * else { logger.error(EntityEventPolicyMsgs.FAILED_TO_UPDATE_ENTITY_IN_DOCSTORE,
131 * spikeEventEntity.getId(), "SYNC_ENTITY"); } } } catch (IOException e) {
132 * logger.error(EntityEventPolicyMsgs.FAILED_TO_UPDATE_ENTITY_IN_DOCSTORE,
133 * spikeEventEntity.getId(), "SYNC_ENTITY"); } }