2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.util;
23 import java.io.StringWriter;
24 import java.util.Iterator;
25 import java.util.UUID;
27 import javax.xml.bind.Marshaller;
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.onap.aai.dmaap.AAIDmaapEventJMSProducer;
34 import org.onap.aai.dmaap.MessageProducer;
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 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.core.env.Environment;
45 public class StoreNotificationEvent {
47 private static final Logger LOGGER = LoggerFactory.getLogger(StoreNotificationEvent.class);
49 private MessageProducer messageProducer;
50 private String fromAppId = "";
51 private String transId = "";
52 private final String transactionId;
53 private final String sourceOfTruth;
55 private ApplicationContext context;
56 private Environment env;
59 * Instantiates a new store notification event.
61 public StoreNotificationEvent(String transactionId, String sourceOfTruth) {
62 this.messageProducer = new AAIDmaapEventJMSProducer();
63 this.transactionId = transactionId;
64 this.sourceOfTruth = sourceOfTruth;
67 public StoreNotificationEvent(AAIDmaapEventJMSProducer producer, String transactionId, String sourceOfTruth) {
68 this.messageProducer = producer;
69 this.transactionId = transactionId;
70 this.sourceOfTruth = sourceOfTruth;
80 * @throws AAIException
83 public String storeEventAndSendToJms(NotificationEvent.EventHeader eh, Object obj) throws AAIException {
86 throw new AAIException("AAI_7350");
89 org.onap.aai.domain.notificationEvent.ObjectFactory factory =
90 new org.onap.aai.domain.notificationEvent.ObjectFactory();
92 org.onap.aai.domain.notificationEvent.NotificationEvent ne = factory.createNotificationEvent();
94 if (eh.getId() == null) {
95 eh.setId(genDate2() + "-" + UUID.randomUUID().toString());
97 if (eh.getTimestamp() == null) {
98 eh.setTimestamp(genDate());
101 // there's no default, but i think we want to put this in hbase?
103 if (eh.getEntityLink() == null) {
104 eh.setEntityLink("UNK");
107 if (eh.getAction() == null) {
111 if (eh.getEventType() == null) {
112 eh.setEventType(AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
115 if (eh.getDomain() == null) {
116 eh.setDomain(AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
119 if (eh.getSourceName() == null) {
120 eh.setSourceName(AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
123 if (eh.getSequenceNumber() == null) {
124 eh.setSequenceNumber(AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
127 if (eh.getSeverity() == null) {
128 eh.setSeverity(AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
131 if (eh.getVersion() == null) {
132 eh.setVersion(AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
135 ne.setCambriaPartition(AAIConstants.UEB_PUB_PARTITION_AAI);
136 ne.setEventHeader(eh);
140 PojoUtils pu = new PojoUtils();
141 String entityJson = pu.getJsonFromObject(ne);
142 sendToDmaapJmsQueue(entityJson);
144 } catch (Exception e) {
145 throw new AAIException("AAI_7350", e);
150 * Store dynamic event.
152 * @param notificationJaxbContext
153 * the notification jaxb context
154 * @param notificationVersion
155 * the notification version
160 * @throws AAIException
163 public void storeDynamicEvent(DynamicJAXBContext notificationJaxbContext, String notificationVersion,
164 DynamicEntity eventHeader, DynamicEntity obj) throws AAIException {
167 throw new AAIException("AAI_7350");
170 DynamicEntity notificationEvent = notificationJaxbContext
171 .getDynamicType("inventory.aai.onap.org." + notificationVersion + ".NotificationEvent")
174 if (eventHeader.get("id") == null) {
175 eventHeader.set("id", genDate2() + "-" + UUID.randomUUID().toString());
178 if (eventHeader.get("timestamp") == null) {
179 eventHeader.set("timestamp", genDate());
182 if (eventHeader.get("entityLink") == null) {
183 eventHeader.set("entityLink", "UNK");
186 if (eventHeader.get("action") == null) {
187 eventHeader.set("action", "UNK");
190 if (eventHeader.get("eventType") == null) {
191 eventHeader.set("eventType", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
194 if (eventHeader.get("domain") == null) {
195 eventHeader.set("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
198 if (eventHeader.get("sourceName") == null) {
199 eventHeader.set("sourceName", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
202 if (eventHeader.get("sequenceNumber") == null) {
203 eventHeader.set("sequenceNumber", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
206 if (eventHeader.get("severity") == null) {
207 eventHeader.set("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
210 if (eventHeader.get("version") == null) {
211 eventHeader.set("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
214 if (notificationEvent.get("cambriaPartition") == null) {
215 notificationEvent.set("cambriaPartition", AAIConstants.UEB_PUB_PARTITION_AAI);
218 notificationEvent.set("eventHeader", eventHeader);
219 notificationEvent.set("entity", obj);
222 StringWriter result = new StringWriter();
224 Marshaller marshaller = notificationJaxbContext.createMarshaller();
225 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
226 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, false);
227 marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
228 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
229 marshaller.marshal(notificationEvent, result);
230 this.sendToDmaapJmsQueue(result.toString());
232 } catch (Exception e) {
233 throw new AAIException("AAI_7350", e);
237 public String storeEventOnly(Loader loader, Introspector eventHeader, Introspector obj) throws AAIException {
239 throw new AAIException("AAI_7350");
243 final Introspector notificationEvent = loader.introspectorFromName("notification-event");
245 if (eventHeader.getValue("id") == null) {
246 eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
249 if (eventHeader.getValue("timestamp") == null) {
250 eventHeader.setValue("timestamp", genDate());
253 if (eventHeader.getValue("entity-link") == null) {
254 eventHeader.setValue("entity-link", "UNK");
257 if (eventHeader.getValue("action") == null) {
258 eventHeader.setValue("action", "UNK");
261 if (eventHeader.getValue("event-type") == null) {
262 eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
265 if (eventHeader.getValue("domain") == null) {
266 eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
269 if (eventHeader.getValue("source-name") == null) {
270 eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
273 if (eventHeader.getValue("sequence-number") == null) {
274 eventHeader.setValue("sequence-number",
275 AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
278 if (eventHeader.getValue("severity") == null) {
279 eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
282 if (eventHeader.getValue("version") == null) {
283 eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
286 if (notificationEvent.getValue("cambria-partition") == null) {
287 notificationEvent.setValue("cambria-partition",
288 AAIConfig.get("aai.notificationEvent.default.partition", AAIConstants.UEB_PUB_PARTITION_AAI));
291 notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
292 notificationEvent.setValue("entity", obj.getUnderlyingObject());
294 String entityJson = notificationEvent.marshal(false);
295 JSONObject entityJsonObject = new JSONObject(entityJson);
297 JSONObject entityJsonObjectUpdated = new JSONObject();
299 JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
300 String cambriaPartition = entityJsonObject.getString("cambria.partition");
302 entityJsonObject.remove("event-header");
303 entityJsonObject.remove("cambria.partition");
305 entityJsonObjectUpdated.put("event-header", entityHeader);
306 entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
308 Iterator<String> iter = entityJsonObject.keys();
309 JSONObject entity = new JSONObject();
310 if (iter.hasNext()) {
311 entity = entityJsonObject.getJSONObject(iter.next());
314 entityJsonObjectUpdated.put("entity", entity);
316 return entityJsonObjectUpdated.toString();
317 } catch (JSONException e) {
318 throw new AAIException("AAI_7350", e);
319 } catch (AAIUnknownObjectException e) {
320 throw new AAIException("AAI_7350", e);
324 public String storeEventAndSendToJms(Loader loader, Introspector eventHeader, Introspector obj)
325 throws AAIException {
327 throw new AAIException("AAI_7350");
331 final Introspector notificationEvent = loader.introspectorFromName("notification-event");
333 if (eventHeader.getValue("id") == null) {
334 eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
337 if (eventHeader.getValue("timestamp") == null) {
338 eventHeader.setValue("timestamp", genDate());
341 if (eventHeader.getValue("entity-link") == null) {
342 eventHeader.setValue("entity-link", "UNK");
345 if (eventHeader.getValue("action") == null) {
346 eventHeader.setValue("action", "UNK");
349 if (eventHeader.getValue("event-type") == null) {
350 eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
353 if (eventHeader.getValue("domain") == null) {
354 eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
357 if (eventHeader.getValue("source-name") == null) {
358 eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
361 if (eventHeader.getValue("sequence-number") == null) {
362 eventHeader.setValue("sequence-number",
363 AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
366 if (eventHeader.getValue("severity") == null) {
367 eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
370 if (eventHeader.getValue("version") == null) {
371 eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
374 if (notificationEvent.getValue("cambria-partition") == null) {
375 notificationEvent.setValue("cambria-partition",
376 AAIConfig.get("aai.notificationEvent.default.partition", AAIConstants.UEB_PUB_PARTITION_AAI));
379 notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
380 notificationEvent.setValue("entity", obj.getUnderlyingObject());
382 String entityJson = notificationEvent.marshal(false);
383 sendToDmaapJmsQueue(entityJson);
385 } catch (JSONException e) {
386 throw new AAIException("AAI_7350", e);
387 } catch (AAIUnknownObjectException e) {
388 throw new AAIException("AAI_7350", e);
392 private void sendToDmaapJmsQueue(String entityString) throws JSONException {
394 JSONObject entityJsonObject = new JSONObject(entityString);
396 JSONObject entityJsonObjectUpdated = new JSONObject();
397 JSONObject finalJson = new JSONObject();
399 JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
400 String cambriaPartition = entityJsonObject.getString("cambria.partition");
402 entityJsonObject.remove("event-header");
403 entityJsonObject.remove("cambria.partition");
405 entityJsonObjectUpdated.put("event-header", entityHeader);
406 entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
408 String transId = entityHeader.getString("id");
409 String fromAppId = entityHeader.getString("source-name");
411 Iterator<String> iter = entityJsonObject.keys();
412 JSONObject entity = new JSONObject();
413 if (iter.hasNext()) {
414 entity = entityJsonObject.getJSONObject(iter.next());
417 entityJsonObjectUpdated.put("entity", entity);
419 finalJson.put("event-topic", "AAI-EVENT");
420 finalJson.put("transId", transId);
421 finalJson.put("fromAppId", fromAppId);
422 finalJson.put("fullId", "");
423 finalJson.put("aaiEventPayload", entityJsonObjectUpdated);
425 messageProducer.sendMessageToDefaultDestination(finalJson);
433 public static String genDate() {
434 FormatDate fd = new FormatDate("YYYYMMdd-HH:mm:ss:SSS");
435 return fd.getDateTime();
443 public static String genDate2() {
444 FormatDate fd = new FormatDate("YYYYMMddHHmmss");
445 return fd.getDateTime();