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