Fixed a typo in a name
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / policy / SpikeEntitySearchProcessor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.datarouter.policy;
22
23 import java.io.FileNotFoundException;
24 import java.util.List;
25
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;
30
31
32 public class SpikeEntitySearchProcessor extends AbstractSpikeEntityEventProcessor {
33
34   public static final String additionalInfo = "Response of SpikeEntityEventPolicy";
35
36   private static final String PROCESS_SPIKE_EVENT = "Process Spike Event";
37
38   /** Agent for communicating with the Search Service. */
39
40   public SpikeEntitySearchProcessor(SpikeEventPolicyConfig config) throws FileNotFoundException {
41     super(config);
42
43   }
44
45   @Override
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);
50   }
51
52   @Override
53   public void process(Exchange exchange) throws Exception {
54
55     long startTime = System.currentTimeMillis();
56
57     SpikeEventMeta meta = processSpikeEvent(exchange);
58
59     if (meta == null) {
60       return;
61     }
62
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) {
67       return;
68     }
69
70
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);
77
78     if (spikeEventEntity == null) {
79       return;
80     }
81
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);
87     return;
88   }
89
90
91   /*
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)));
97    * 
98    * String entityId = spikeEventEntity.getId(); String jsonPayload;
99    * 
100    * // Run the GET to retrieve the ETAG from the search service OperationResult storedEntity =
101    * searchAgent.getDocument(entitySearchIndex, entityId);
102    * 
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).
106    * 
107    * 
108    * List<String> etag = storedEntity.getHeaders().get(Headers.ETAG);
109    * 
110    * if (etag != null && !etag.isEmpty()) { headers.put(Headers.IF_MATCH, etag); } else {
111    * logger.error(EntityEventPolicyMsgs.NO_ETAG_AVAILABLE_FAILURE, entitySearchIndex, entityId); }
112    * 
113    * ArrayList<JsonNode> sourceObject = new ArrayList<>();
114    * NodeUtils.extractObjectsByKey(NodeUtils.convertJsonStrToJsonNode(storedEntity.getResult()),
115    * "content", sourceObject);
116    * 
117    * if (!sourceObject.isEmpty()) { JsonNode node = sourceObject.get(0); final String sourceCer =
118    * NodeUtils.extractFieldValueFromObject(node, "crossReferenceEntityValues"); 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; } }
122    * 
123    * if (hasNewCer) { // Do the PUT with new CER ((ObjectNode)
124    * node).put("crossReferenceEntityValues", newCer); jsonPayload =
125    * NodeUtils.convertObjectToJson(node, false); searchAgent.putDocument(entitySearchIndex,
126    * entityId, jsonPayload, headers); } } } else {
127    * 
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"); } }
134    */
135
136 }