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