76fce7b8fd429c689cbaf655f963c73a90b39e2a
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / policy / SpikeAggregateGenericVnfProcessor.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.io.IOException;
27 import java.util.List;
28
29 import org.apache.camel.Exchange;
30 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
31 import org.onap.aai.datarouter.entity.SpikeAggregationEntity;
32 import org.onap.aai.datarouter.entity.SpikeEventVertex;
33 import org.onap.aai.datarouter.logging.EntityEventPolicyMsgs;
34
35 import com.fasterxml.jackson.databind.JsonNode;
36
37
38 public class SpikeAggregateGenericVnfProcessor extends AbstractSpikeEntityEventProcessor {
39
40   public static final String additionalInfo = "Response of SpikeEntityEventPolicy";
41
42   /** Agent for communicating with the Search Service. */
43
44   public SpikeAggregateGenericVnfProcessor(SpikeEventPolicyConfig config)
45       throws FileNotFoundException {
46     super(config);
47   }
48   
49   @Override
50   protected void startup() {
51     // Create the indexes in the search service if they do not already exist.
52     searchAgent.createSearchIndex(searchIndexName, searchIndexSchema, createIndexUrl);
53     logger.info(EntityEventPolicyMsgs.ENTITY_EVENT_POLICY_REGISTERED);
54   }
55
56   @Override
57   public void process(Exchange exchange) throws Exception {
58
59     long startTime = System.currentTimeMillis();
60     String uebPayload = getExchangeBody(exchange);
61     if (uebPayload == null) {
62       return;
63     }
64     JsonNode uebAsJson = null;
65     try {
66       uebAsJson = mapper.readTree(uebPayload);
67     } catch (IOException e) {
68       returnWithError(exchange, uebPayload, "Invalid Payload");
69       return;
70     }
71     
72     String action = getSpikeEventAction(exchange, uebPayload);
73     if (action == null) {
74       return;
75     }
76     SpikeEventVertex eventVertex = populateEventVertex(exchange, uebPayload);
77     if (eventVertex == null) {
78       return;
79     }
80     String entityType = getEntityType(exchange, eventVertex, uebPayload);
81     if (entityType == null) {
82       return;
83     }
84     String entityLink = getEntityLink(exchange, eventVertex, uebPayload);
85     if (entityLink == null) {
86       return;
87     }
88     DynamicJAXBContext oxmJaxbContext = readOxm(exchange, uebPayload);
89     if (oxmJaxbContext == null) {
90       return;
91     }
92     String oxmEntityType = getOxmEntityType(entityType);
93     List<String> searchableAttr =  getSearchableAttibutes(oxmJaxbContext, oxmEntityType, entityType, uebPayload,
94         exchange);
95     if (searchableAttr == null) {
96       return;
97     }
98     
99
100     // log the fact that all data are in good shape
101     logger.info(EntityEventPolicyMsgs.PROCESS_ENTITY_EVENT_POLICY_NONVERBOSE, action, entityType);
102     logger.debug(EntityEventPolicyMsgs.PROCESS_ENTITY_EVENT_POLICY_VERBOSE, action, entityType,
103         uebPayload);
104
105     SpikeAggregationEntity spikeAgregationEntity = new SpikeAggregationEntity();
106     spikeAgregationEntity.setLink(entityLink);
107     spikeAgregationEntity.deriveFields(uebAsJson);
108     handleSearchServiceOperation(spikeAgregationEntity, action, searchIndexName);
109
110     long stopTime = System.currentTimeMillis();
111     metricsLogger.info(EntityEventPolicyMsgs.OPERATION_RESULT_NO_ERRORS, PROCESS_SPIKE_EVENT,
112         String.valueOf(stopTime - startTime));
113     setResponse(exchange, ResponseType.SUCCESS, additionalInfo);
114     return;
115   }
116
117 }