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