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