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