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