Release 1.13.6 Docker Artifact
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / StoreNotificationEvent.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.util;
22
23 import java.io.StringWriter;
24 import java.util.Iterator;
25 import java.util.UUID;
26
27 import javax.xml.bind.Marshaller;
28
29 import org.eclipse.persistence.dynamic.DynamicEntity;
30 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.onap.aai.domain.notificationEvent.NotificationEvent;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Introspector;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
38 import org.onap.aai.kafka.AAIKafkaEventJMSProducer;
39 import org.onap.aai.kafka.MessageProducer;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.core.env.Environment;
44
45 public class StoreNotificationEvent {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(StoreNotificationEvent.class);
48
49     private MessageProducer messageProducer;
50     private String fromAppId = "";
51     private String transId = "";
52     private final String transactionId;
53     private final String sourceOfTruth;
54
55     private ApplicationContext context;
56     private Environment env;
57
58     /**
59      * Instantiates a new store notification event.
60      */
61     public StoreNotificationEvent(String transactionId, String sourceOfTruth) {
62         this.messageProducer = new AAIKafkaEventJMSProducer();
63         this.transactionId = transactionId;
64         this.sourceOfTruth = sourceOfTruth;
65     }
66
67     public StoreNotificationEvent(AAIKafkaEventJMSProducer producer, String transactionId, String sourceOfTruth) {
68         this.messageProducer = producer;
69         this.transactionId = transactionId;
70         this.sourceOfTruth = sourceOfTruth;
71     }
72
73     /**
74      * Store event.
75      *
76      * @param eh
77      *        the eh
78      * @param obj
79      *        the obj
80      * @throws AAIException
81      *         the AAI exception
82      */
83     public String storeEventAndSendToJms(NotificationEvent.EventHeader eh, Object obj) throws AAIException {
84
85         if (obj == null) {
86             throw new AAIException("AAI_7350");
87         }
88
89         org.onap.aai.domain.notificationEvent.ObjectFactory factory =
90                 new org.onap.aai.domain.notificationEvent.ObjectFactory();
91
92         org.onap.aai.domain.notificationEvent.NotificationEvent ne = factory.createNotificationEvent();
93
94         if (eh.getId() == null) {
95             eh.setId(genDate2() + "-" + UUID.randomUUID().toString());
96         }
97         if (eh.getTimestamp() == null) {
98             eh.setTimestamp(genDate());
99         }
100
101         // there's no default, but i think we want to put this in hbase?
102
103         if (eh.getEntityLink() == null) {
104             eh.setEntityLink("UNK");
105         }
106
107         if (eh.getAction() == null) {
108             eh.setAction("UNK");
109         }
110
111         if (eh.getEventType() == null) {
112             eh.setEventType(AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
113         }
114
115         if (eh.getDomain() == null) {
116             eh.setDomain(AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
117         }
118
119         if (eh.getSourceName() == null) {
120             eh.setSourceName(AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
121         }
122
123         if (eh.getSequenceNumber() == null) {
124             eh.setSequenceNumber(AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
125         }
126
127         if (eh.getSeverity() == null) {
128             eh.setSeverity(AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
129         }
130
131         if (eh.getVersion() == null) {
132             eh.setVersion(AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
133         }
134
135         ne.setCambriaPartition(AAIConstants.UEB_PUB_PARTITION_AAI);
136         ne.setEventHeader(eh);
137         ne.setEntity(obj);
138
139         try {
140             PojoUtils pu = new PojoUtils();
141             String entityJson = pu.getJsonFromObject(ne);
142             sendToKafkaJmsQueue(entityJson);
143             return entityJson;
144         } catch (Exception e) {
145             throw new AAIException("AAI_7350", e);
146         }
147     }
148
149     /**
150      * Store dynamic event.
151      *
152      * @param notificationJaxbContext
153      *        the notification jaxb context
154      * @param notificationVersion
155      *        the notification version
156      * @param eventHeader
157      *        the event header
158      * @param obj
159      *        the obj
160      * @throws AAIException
161      *         the AAI exception
162      */
163     public void storeDynamicEvent(DynamicJAXBContext notificationJaxbContext, String notificationVersion,
164             DynamicEntity eventHeader, DynamicEntity obj) throws AAIException {
165
166         if (obj == null) {
167             throw new AAIException("AAI_7350");
168         }
169
170         DynamicEntity notificationEvent = notificationJaxbContext
171                 .getDynamicType("inventory.aai.onap.org." + notificationVersion + ".NotificationEvent")
172                 .newDynamicEntity();
173
174         if (eventHeader.get("id") == null) {
175             eventHeader.set("id", genDate2() + "-" + UUID.randomUUID().toString());
176         }
177
178         if (eventHeader.get("timestamp") == null) {
179             eventHeader.set("timestamp", genDate());
180         }
181
182         if (eventHeader.get("entityLink") == null) {
183             eventHeader.set("entityLink", "UNK");
184         }
185
186         if (eventHeader.get("action") == null) {
187             eventHeader.set("action", "UNK");
188         }
189
190         if (eventHeader.get("eventType") == null) {
191             eventHeader.set("eventType", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
192         }
193
194         if (eventHeader.get("domain") == null) {
195             eventHeader.set("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
196         }
197
198         if (eventHeader.get("sourceName") == null) {
199             eventHeader.set("sourceName", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
200         }
201
202         if (eventHeader.get("sequenceNumber") == null) {
203             eventHeader.set("sequenceNumber", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
204         }
205
206         if (eventHeader.get("severity") == null) {
207             eventHeader.set("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
208         }
209
210         if (eventHeader.get("version") == null) {
211             eventHeader.set("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
212         }
213
214         if (notificationEvent.get("cambriaPartition") == null) {
215             notificationEvent.set("cambriaPartition", AAIConstants.UEB_PUB_PARTITION_AAI);
216         }
217
218         notificationEvent.set("eventHeader", eventHeader);
219         notificationEvent.set("entity", obj);
220
221         try {
222             StringWriter result = new StringWriter();
223
224             Marshaller marshaller = notificationJaxbContext.createMarshaller();
225             marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
226             marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, false);
227             marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
228             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
229             marshaller.marshal(notificationEvent, result);
230             this.sendToKafkaJmsQueue(result.toString());
231
232         } catch (Exception e) {
233             throw new AAIException("AAI_7350", e);
234         }
235     }
236
237     public String storeEventOnly(Loader loader, Introspector eventHeader, Introspector obj) throws AAIException {
238         if (obj == null) {
239             throw new AAIException("AAI_7350");
240         }
241
242         try {
243             final Introspector notificationEvent = loader.introspectorFromName("notification-event");
244
245             if (eventHeader.getValue("id") == null) {
246                 eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
247             }
248
249             if (eventHeader.getValue("timestamp") == null) {
250                 eventHeader.setValue("timestamp", genDate());
251             }
252
253             if (eventHeader.getValue("entity-link") == null) {
254                 eventHeader.setValue("entity-link", "UNK");
255             }
256
257             if (eventHeader.getValue("action") == null) {
258                 eventHeader.setValue("action", "UNK");
259             }
260
261             if (eventHeader.getValue("event-type") == null) {
262                 eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
263             }
264
265             if (eventHeader.getValue("domain") == null) {
266                 eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
267             }
268
269             if (eventHeader.getValue("source-name") == null) {
270                 eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
271             }
272
273             if (eventHeader.getValue("sequence-number") == null) {
274                 eventHeader.setValue("sequence-number",
275                         AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
276             }
277
278             if (eventHeader.getValue("severity") == null) {
279                 eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
280             }
281
282             if (eventHeader.getValue("version") == null) {
283                 eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
284             }
285
286             if (notificationEvent.getValue("cambria-partition") == null) {
287                 notificationEvent.setValue("cambria-partition",
288                         AAIConfig.get("aai.notificationEvent.default.partition", AAIConstants.UEB_PUB_PARTITION_AAI));
289             }
290
291             notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
292             notificationEvent.setValue("entity", obj.getUnderlyingObject());
293
294             String entityJson = notificationEvent.marshal(false);
295             JSONObject entityJsonObject = new JSONObject(entityJson);
296
297             JSONObject entityJsonObjectUpdated = new JSONObject();
298
299             JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
300             String cambriaPartition = entityJsonObject.getString("cambria.partition");
301
302             entityJsonObject.remove("event-header");
303             entityJsonObject.remove("cambria.partition");
304
305             entityJsonObjectUpdated.put("event-header", entityHeader);
306             entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
307
308             Iterator<String> iter = entityJsonObject.keys();
309             JSONObject entity = new JSONObject();
310             if (iter.hasNext()) {
311                 entity = entityJsonObject.getJSONObject(iter.next());
312             }
313
314             entityJsonObjectUpdated.put("entity", entity);
315
316             return entityJsonObjectUpdated.toString();
317         } catch (JSONException e) {
318             throw new AAIException("AAI_7350", e);
319         } catch (AAIUnknownObjectException e) {
320             throw new AAIException("AAI_7350", e);
321         }
322     }
323
324     public String storeEventAndSendToJms(Loader loader, Introspector eventHeader, Introspector obj)
325             throws AAIException {
326         if (obj == null) {
327             throw new AAIException("AAI_7350");
328         }
329
330         try {
331             final Introspector notificationEvent = loader.introspectorFromName("notification-event");
332
333             if (eventHeader.getValue("id") == null) {
334                 eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
335             }
336
337             if (eventHeader.getValue("timestamp") == null) {
338                 eventHeader.setValue("timestamp", genDate());
339             }
340
341             if (eventHeader.getValue("entity-link") == null) {
342                 eventHeader.setValue("entity-link", "UNK");
343             }
344
345             if (eventHeader.getValue("action") == null) {
346                 eventHeader.setValue("action", "UNK");
347             }
348
349             if (eventHeader.getValue("event-type") == null) {
350                 eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
351             }
352
353             if (eventHeader.getValue("domain") == null) {
354                 eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
355             }
356
357             if (eventHeader.getValue("source-name") == null) {
358                 eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
359             }
360
361             if (eventHeader.getValue("sequence-number") == null) {
362                 eventHeader.setValue("sequence-number",
363                         AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
364             }
365
366             if (eventHeader.getValue("severity") == null) {
367                 eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
368             }
369
370             if (eventHeader.getValue("version") == null) {
371                 eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
372             }
373
374             if (notificationEvent.getValue("cambria-partition") == null) {
375                 notificationEvent.setValue("cambria-partition",
376                         AAIConfig.get("aai.notificationEvent.default.partition", AAIConstants.UEB_PUB_PARTITION_AAI));
377             }
378
379             notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
380             notificationEvent.setValue("entity", obj.getUnderlyingObject());
381
382             String entityJson = notificationEvent.marshal(false);
383             sendToKafkaJmsQueue(entityJson);
384             return entityJson;
385         } catch (JSONException e) {
386             throw new AAIException("AAI_7350", e);
387         } catch (AAIUnknownObjectException e) {
388             throw new AAIException("AAI_7350", e);
389         }
390     }
391
392     private void sendToKafkaJmsQueue(String entityString) throws JSONException {
393
394         JSONObject entityJsonObject = new JSONObject(entityString);
395
396         JSONObject entityJsonObjectUpdated = new JSONObject();
397         JSONObject finalJson = new JSONObject();
398
399         JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
400         String cambriaPartition = entityJsonObject.getString("cambria.partition");
401
402         entityJsonObject.remove("event-header");
403         entityJsonObject.remove("cambria.partition");
404
405         entityJsonObjectUpdated.put("event-header", entityHeader);
406         entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
407
408         String transId = entityHeader.getString("id");
409         String fromAppId = entityHeader.getString("source-name");
410
411         Iterator<String> iter = entityJsonObject.keys();
412         JSONObject entity = new JSONObject();
413         if (iter.hasNext()) {
414             entity = entityJsonObject.getJSONObject(iter.next());
415         }
416
417         entityJsonObjectUpdated.put("entity", entity);
418
419         finalJson.put("event-topic", "AAI-EVENT");
420         finalJson.put("transId", transId);
421         finalJson.put("fromAppId", fromAppId);
422         finalJson.put("fullId", "");
423         finalJson.put("aaiEventPayload", entityJsonObjectUpdated);
424
425         messageProducer.sendMessageToDefaultDestination(finalJson);
426     }
427
428     /**
429      * Gen date.
430      *
431      * @return the string
432      */
433     public static String genDate() {
434         FormatDate fd = new FormatDate("YYYYMMdd-HH:mm:ss:SSS");
435         return fd.getDateTime();
436     }
437
438     /**
439      * Gen date 2.
440      *
441      * @return the string
442      */
443     public static String genDate2() {
444         FormatDate fd = new FormatDate("YYYYMMddHHmmss");
445         return fd.getDateTime();
446     }
447
448 }