Enhancements for the aai-common library
[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 org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.io.StringWriter;
27 import java.util.Iterator;
28 import java.util.UUID;
29
30 import javax.xml.bind.Marshaller;
31
32 import org.eclipse.persistence.dynamic.DynamicEntity;
33 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
34 import org.json.JSONException;
35 import org.json.JSONObject;
36 import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
37 import org.onap.aai.dmaap.MessageProducer;
38 import org.onap.aai.domain.notificationEvent.NotificationEvent;
39 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.introspection.Introspector;
41 import org.onap.aai.introspection.Loader;
42 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.core.env.Environment;
45
46 public class StoreNotificationEvent {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(StoreNotificationEvent.class);
49
50     private MessageProducer messageProducer;
51     private String fromAppId = "";
52     private String transId = "";
53     private final String transactionId;
54     private final String sourceOfTruth;
55
56     private ApplicationContext context;
57     private Environment env;
58
59     /**
60      * Instantiates a new store notification event.
61      */
62     public StoreNotificationEvent(String transactionId, String sourceOfTruth) {
63         this.messageProducer = new AAIDmaapEventJMSProducer();
64         this.transactionId = transactionId;
65         this.sourceOfTruth = sourceOfTruth;
66     }
67
68     public StoreNotificationEvent(AAIDmaapEventJMSProducer producer, String transactionId, String sourceOfTruth) {
69         this.messageProducer = producer;
70         this.transactionId = transactionId;
71         this.sourceOfTruth = sourceOfTruth;
72     }
73
74     /**
75      * Store event.
76      *
77      * @param eh
78      *        the eh
79      * @param obj
80      *        the obj
81      * @throws AAIException
82      *         the AAI exception
83      */
84     public String storeEventAndSendToJms(NotificationEvent.EventHeader eh, Object obj) throws AAIException {
85
86         if (obj == null) {
87             throw new AAIException("AAI_7350");
88         }
89
90         org.onap.aai.domain.notificationEvent.ObjectFactory factory =
91                 new org.onap.aai.domain.notificationEvent.ObjectFactory();
92
93         org.onap.aai.domain.notificationEvent.NotificationEvent ne = factory.createNotificationEvent();
94
95         if (eh.getId() == null) {
96             eh.setId(genDate2() + "-" + UUID.randomUUID().toString());
97         }
98         if (eh.getTimestamp() == null) {
99             eh.setTimestamp(genDate());
100         }
101
102         // there's no default, but i think we want to put this in hbase?
103
104         if (eh.getEntityLink() == null) {
105             eh.setEntityLink("UNK");
106         }
107
108         if (eh.getAction() == null) {
109             eh.setAction("UNK");
110         }
111
112         if (eh.getEventType() == null) {
113             eh.setEventType(AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
114         }
115
116         if (eh.getDomain() == null) {
117             eh.setDomain(AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
118         }
119
120         if (eh.getSourceName() == null) {
121             eh.setSourceName(AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
122         }
123
124         if (eh.getSequenceNumber() == null) {
125             eh.setSequenceNumber(AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
126         }
127
128         if (eh.getSeverity() == null) {
129             eh.setSeverity(AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
130         }
131
132         if (eh.getVersion() == null) {
133             eh.setVersion(AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
134         }
135
136         ne.setCambriaPartition(AAIConstants.UEB_PUB_PARTITION_AAI);
137         ne.setEventHeader(eh);
138         ne.setEntity(obj);
139
140         try {
141             PojoUtils pu = new PojoUtils();
142             String entityJson = pu.getJsonFromObject(ne);
143             sendToDmaapJmsQueue(entityJson);
144             return entityJson;
145         } catch (Exception e) {
146             throw new AAIException("AAI_7350", e);
147         }
148     }
149
150     /**
151      * Store dynamic event.
152      *
153      * @param notificationJaxbContext
154      *        the notification jaxb context
155      * @param notificationVersion
156      *        the notification version
157      * @param eventHeader
158      *        the event header
159      * @param obj
160      *        the obj
161      * @throws AAIException
162      *         the AAI exception
163      */
164     public void storeDynamicEvent(DynamicJAXBContext notificationJaxbContext, String notificationVersion,
165             DynamicEntity eventHeader, DynamicEntity obj) throws AAIException {
166
167         if (obj == null) {
168             throw new AAIException("AAI_7350");
169         }
170
171         DynamicEntity notificationEvent = notificationJaxbContext
172                 .getDynamicType("inventory.aai.onap.org." + notificationVersion + ".NotificationEvent")
173                 .newDynamicEntity();
174
175         if (eventHeader.get("id") == null) {
176             eventHeader.set("id", genDate2() + "-" + UUID.randomUUID().toString());
177         }
178
179         if (eventHeader.get("timestamp") == null) {
180             eventHeader.set("timestamp", genDate());
181         }
182
183         if (eventHeader.get("entityLink") == null) {
184             eventHeader.set("entityLink", "UNK");
185         }
186
187         if (eventHeader.get("action") == null) {
188             eventHeader.set("action", "UNK");
189         }
190
191         if (eventHeader.get("eventType") == null) {
192             eventHeader.set("eventType", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
193         }
194
195         if (eventHeader.get("domain") == null) {
196             eventHeader.set("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
197         }
198
199         if (eventHeader.get("sourceName") == null) {
200             eventHeader.set("sourceName", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
201         }
202
203         if (eventHeader.get("sequenceNumber") == null) {
204             eventHeader.set("sequenceNumber", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
205         }
206
207         if (eventHeader.get("severity") == null) {
208             eventHeader.set("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
209         }
210
211         if (eventHeader.get("version") == null) {
212             eventHeader.set("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
213         }
214
215         if (notificationEvent.get("cambriaPartition") == null) {
216             notificationEvent.set("cambriaPartition", AAIConstants.UEB_PUB_PARTITION_AAI);
217         }
218
219         notificationEvent.set("eventHeader", eventHeader);
220         notificationEvent.set("entity", obj);
221
222         try {
223             StringWriter result = new StringWriter();
224
225             Marshaller marshaller = notificationJaxbContext.createMarshaller();
226             marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
227             marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, false);
228             marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
229             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
230             marshaller.marshal(notificationEvent, result);
231             this.sendToDmaapJmsQueue(result.toString());
232
233         } catch (Exception e) {
234             throw new AAIException("AAI_7350", e);
235         }
236     }
237
238     public String storeEventOnly(Loader loader, Introspector eventHeader, Introspector obj) throws AAIException {
239         if (obj == null) {
240             throw new AAIException("AAI_7350");
241         }
242
243         try {
244             final Introspector notificationEvent = loader.introspectorFromName("notification-event");
245
246             if (eventHeader.getValue("id") == null) {
247                 eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
248             }
249
250             if (eventHeader.getValue("timestamp") == null) {
251                 eventHeader.setValue("timestamp", genDate());
252             }
253
254             if (eventHeader.getValue("entity-link") == null) {
255                 eventHeader.setValue("entity-link", "UNK");
256             }
257
258             if (eventHeader.getValue("action") == null) {
259                 eventHeader.setValue("action", "UNK");
260             }
261
262             if (eventHeader.getValue("event-type") == null) {
263                 eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
264             }
265
266             if (eventHeader.getValue("domain") == null) {
267                 eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
268             }
269
270             if (eventHeader.getValue("source-name") == null) {
271                 eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
272             }
273
274             if (eventHeader.getValue("sequence-number") == null) {
275                 eventHeader.setValue("sequence-number",
276                     AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
277             }
278
279             if (eventHeader.getValue("severity") == null) {
280                 eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
281             }
282
283             if (eventHeader.getValue("version") == null) {
284                 eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
285             }
286
287             if (notificationEvent.getValue("cambria-partition") == null) {
288                 notificationEvent.setValue("cambria-partition",
289                     AAIConfig.get("aai.notificationEvent.default.partition", AAIConstants.UEB_PUB_PARTITION_AAI));
290             }
291
292             notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
293             notificationEvent.setValue("entity", obj.getUnderlyingObject());
294
295             String entityJson = notificationEvent.marshal(false);
296             JSONObject entityJsonObject = new JSONObject(entityJson);
297
298             JSONObject entityJsonObjectUpdated = new JSONObject();
299
300             JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
301             String cambriaPartition = entityJsonObject.getString("cambria.partition");
302
303             entityJsonObject.remove("event-header");
304             entityJsonObject.remove("cambria.partition");
305
306             entityJsonObjectUpdated.put("event-header", entityHeader);
307             entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
308
309             Iterator<String> iter = entityJsonObject.keys();
310             JSONObject entity = new JSONObject();
311             if (iter.hasNext()) {
312                 entity = entityJsonObject.getJSONObject(iter.next());
313             }
314
315             entityJsonObjectUpdated.put("entity", entity);
316
317             return entityJsonObjectUpdated.toString();
318         } catch (JSONException e) {
319             throw new AAIException("AAI_7350", e);
320         } catch (AAIUnknownObjectException e) {
321             throw new AAIException("AAI_7350", e);
322         }
323     }
324
325     public String storeEventAndSendToJms(Loader loader, Introspector eventHeader, Introspector obj) 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             sendToDmaapJmsQueue(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 sendToDmaapJmsQueue(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 }