AAI-1523 Batch reformat aai-core
[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 com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
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 EELFLogger logger = EELFManager.getInstance().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 storeEvent(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 storeEvent(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             sendToDmaapJmsQueue(entityJson);
297             return entityJson;
298         } catch (JSONException e) {
299             throw new AAIException("AAI_7350", e);
300         } catch (AAIUnknownObjectException e) {
301             throw new AAIException("AAI_7350", e);
302         }
303     }
304
305     private void sendToDmaapJmsQueue(String entityString) throws JSONException {
306
307         JSONObject entityJsonObject = new JSONObject(entityString);
308
309         JSONObject entityJsonObjectUpdated = new JSONObject();
310         JSONObject finalJson = new JSONObject();
311
312         JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
313         String cambriaPartition = entityJsonObject.getString("cambria.partition");
314
315         entityJsonObject.remove("event-header");
316         entityJsonObject.remove("cambria.partition");
317
318         entityJsonObjectUpdated.put("event-header", entityHeader);
319         entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
320
321         String transId = entityHeader.getString("id");
322         String fromAppId = entityHeader.getString("source-name");
323
324         Iterator<String> iter = entityJsonObject.keys();
325         JSONObject entity = new JSONObject();
326         if (iter.hasNext()) {
327             entity = entityJsonObject.getJSONObject(iter.next());
328         }
329
330         entityJsonObjectUpdated.put("entity", entity);
331
332         finalJson.put("event-topic", "AAI-EVENT");
333         finalJson.put("transId", transId);
334         finalJson.put("fromAppId", fromAppId);
335         finalJson.put("fullId", "");
336         finalJson.put("aaiEventPayload", entityJsonObjectUpdated);
337
338         messageProducer.sendMessageToDefaultDestination(finalJson);
339     }
340
341     /**
342      * Gen date.
343      *
344      * @return the string
345      */
346     public static String genDate() {
347         FormatDate fd = new FormatDate("YYYYMMdd-HH:mm:ss:SSS");
348         return fd.getDateTime();
349     }
350
351     /**
352      * Gen date 2.
353      *
354      * @return the string
355      */
356     public static String genDate2() {
357         FormatDate fd = new FormatDate("YYYYMMddHHmmss");
358         return fd.getDateTime();
359     }
360
361 }