Fix aggregate-vnf search index population
[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.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.camel.Exchange;
28 import org.onap.aai.datarouter.entity.SpikeAggregationEntity;
29 import org.onap.aai.datarouter.entity.SpikeEventMeta;
30 import org.onap.aai.datarouter.logging.EntityEventPolicyMsgs;
31 import org.onap.aai.util.EntityOxmReferenceHelper;
32 import org.onap.aai.util.Version;
33 import org.onap.aai.util.VersionedOxmEntities;
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   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   
55   @Override
56   public void process(Exchange exchange) throws Exception {
57
58     long startTime = System.currentTimeMillis();
59
60     SpikeEventMeta meta = processSpikeEvent(exchange);
61     
62     if (meta == null) {
63       return;
64     }
65
66     String oxmEntityType = getOxmEntityType(meta.getSpikeEventVertex().getType());
67     
68     VersionedOxmEntities oxmEntities =
69         EntityOxmReferenceHelper.getInstance().getVersionedOxmEntities(Version.valueOf(oxmVersion.toLowerCase()));
70     
71     List<String> suggestibleAttrInPayload = new ArrayList<>();
72     List<String> suggestibleAttrInOxm = extractSuggestableAttr(oxmEntities, meta.getSpikeEventVertex().getType());
73     if (suggestibleAttrInOxm != null) {
74       for (String attr: suggestibleAttrInOxm){
75         if (meta.getVertexProperties().has(attr)) {
76           suggestibleAttrInPayload.add(attr);
77         }
78       }
79     }
80
81     if (suggestibleAttrInPayload.isEmpty()) {
82       return;
83     }
84
85     JsonNode propertiesNode =
86         mapper.readValue(meta.getVertexProperties().toString(), JsonNode.class);
87
88     SpikeAggregationEntity spikeAgregationEntity = new SpikeAggregationEntity();
89     spikeAgregationEntity.setLink(meta.getSpikeEventVertex().getEntityLink());
90     spikeAgregationEntity.deriveFields(propertiesNode);
91
92     handleSearchServiceOperation(spikeAgregationEntity, meta.getBodyOperationType(),
93         searchIndexName);
94
95     long stopTime = System.currentTimeMillis();
96     metricsLogger.info(EntityEventPolicyMsgs.OPERATION_RESULT_NO_ERRORS, PROCESS_SPIKE_EVENT,
97         String.valueOf(stopTime - startTime));
98     setResponse(exchange, ResponseType.SUCCESS, additionalInfo);
99     return;
100   }
101
102 }