7f7bdd95194c3aaf69f5c7bfbc4808edc17266b5
[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 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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.datarouter.policy;
24
25 import java.io.FileNotFoundException;
26 import java.util.List;
27
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;
33
34
35 public class SpikeEntitySearchProcessor extends AbstractSpikeEntityEventProcessor {
36
37   public static final String additionalInfo = "Response of SpikeEntityEventPolicy";
38   private static final String searchIndexSchema = "";
39
40
41
42   /** Agent for communicating with the Search Service. */
43
44   public SpikeEntitySearchProcessor(SpikeEventPolicyConfig config)
45       throws FileNotFoundException {
46     super(config);
47
48   }
49
50   @Override
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);
55   }
56
57   @Override
58   public void process(Exchange exchange) throws Exception {
59
60     long startTime = System.currentTimeMillis();
61     String uebPayload = getExchangeBody(exchange);
62     if (uebPayload == null) {
63       return;
64     }
65
66     String action = getSpikeEventAction(exchange, uebPayload);
67     if (action == null) {
68       return;
69     }
70     SpikeEventVertex eventVertex = populateEventVertex(exchange, uebPayload);
71     if (eventVertex == null) {
72       return;
73     }
74     String entityType = getEntityType(exchange, eventVertex, uebPayload);
75     if (entityType == null) {
76       return;
77     }
78     String entityLink = getEntityLink(exchange, eventVertex, uebPayload);
79     if (entityLink == null) {
80       return;
81     }
82     DynamicJAXBContext oxmJaxbContext = readOxm(exchange, uebPayload);
83     if (oxmJaxbContext == null) {
84       return;
85     }
86     String oxmEntityType = getOxmEntityType(entityType);
87     List<String> searchableAttr =  getSearchableAttibutes(oxmJaxbContext, oxmEntityType, entityType, uebPayload,
88         exchange);
89     if (searchableAttr == null) {
90       return;
91     }
92
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,
96         uebPayload);
97
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) {
104       return;
105     }
106
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);
112     return;
113   }
114
115   /*
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)));
121    * 
122    * String entityId = spikeEventEntity.getId(); String jsonPayload;
123    * 
124    * // Run the GET to retrieve the ETAG from the search service OperationResult storedEntity =
125    * searchAgent.getDocument(entitySearchIndex, entityId);
126    * 
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).
130    * 
131    * 
132    * List<String> etag = storedEntity.getHeaders().get(Headers.ETAG);
133    * 
134    * if (etag != null && !etag.isEmpty()) { headers.put(Headers.IF_MATCH, etag); } else {
135    * logger.error(EntityEventPolicyMsgs.NO_ETAG_AVAILABLE_FAILURE, entitySearchIndex, entityId); }
136    * 
137    * ArrayList<JsonNode> sourceObject = new ArrayList<>();
138    * NodeUtils.extractObjectsByKey(NodeUtils.convertJsonStrToJsonNode(storedEntity.getResult()),
139    * "content", sourceObject);
140    * 
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; } }
146    * 
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 {
151    * 
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"); } }
158    */
159
160 }