Remove DMaaP dependency from AAI-Common
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / StoreNotificationEvent.java
index 8b2bf50..6f3e888 100644 (file)
@@ -20,9 +20,6 @@
 
 package org.onap.aai.util;
 
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-
 import java.io.StringWriter;
 import java.util.Iterator;
 import java.util.UUID;
@@ -33,19 +30,21 @@ import org.eclipse.persistence.dynamic.DynamicEntity;
 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
 import org.json.JSONException;
 import org.json.JSONObject;
-import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
-import org.onap.aai.dmaap.MessageProducer;
 import org.onap.aai.domain.notificationEvent.NotificationEvent;
 import org.onap.aai.exceptions.AAIException;
 import org.onap.aai.introspection.Introspector;
 import org.onap.aai.introspection.Loader;
 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
+import org.onap.aai.kafka.AAIKafkaEventJMSProducer;
+import org.onap.aai.kafka.MessageProducer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.context.ApplicationContext;
 import org.springframework.core.env.Environment;
 
 public class StoreNotificationEvent {
 
-    private static final EELFLogger logger = EELFManager.getInstance().getLogger(StoreNotificationEvent.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(StoreNotificationEvent.class);
 
     private MessageProducer messageProducer;
     private String fromAppId = "";
@@ -60,12 +59,12 @@ public class StoreNotificationEvent {
      * Instantiates a new store notification event.
      */
     public StoreNotificationEvent(String transactionId, String sourceOfTruth) {
-        this.messageProducer = new AAIDmaapEventJMSProducer();
+        this.messageProducer = new AAIKafkaEventJMSProducer();
         this.transactionId = transactionId;
         this.sourceOfTruth = sourceOfTruth;
     }
 
-    public StoreNotificationEvent(AAIDmaapEventJMSProducer producer, String transactionId, String sourceOfTruth) {
+    public StoreNotificationEvent(AAIKafkaEventJMSProducer producer, String transactionId, String sourceOfTruth) {
         this.messageProducer = producer;
         this.transactionId = transactionId;
         this.sourceOfTruth = sourceOfTruth;
@@ -81,7 +80,7 @@ public class StoreNotificationEvent {
      * @throws AAIException
      *         the AAI exception
      */
-    public String storeEvent(NotificationEvent.EventHeader eh, Object obj) throws AAIException {
+    public String storeEventAndSendToJms(NotificationEvent.EventHeader eh, Object obj) throws AAIException {
 
         if (obj == null) {
             throw new AAIException("AAI_7350");
@@ -140,7 +139,7 @@ public class StoreNotificationEvent {
         try {
             PojoUtils pu = new PojoUtils();
             String entityJson = pu.getJsonFromObject(ne);
-            sendToDmaapJmsQueue(entityJson);
+            sendToKafkaJmsQueue(entityJson);
             return entityJson;
         } catch (Exception e) {
             throw new AAIException("AAI_7350", e);
@@ -228,14 +227,102 @@ public class StoreNotificationEvent {
             marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
             marshaller.marshal(notificationEvent, result);
-            this.sendToDmaapJmsQueue(result.toString());
+            this.sendToKafkaJmsQueue(result.toString());
 
         } catch (Exception e) {
             throw new AAIException("AAI_7350", e);
         }
     }
 
-    public String storeEvent(Loader loader, Introspector eventHeader, Introspector obj) throws AAIException {
+    public String storeEventOnly(Loader loader, Introspector eventHeader, Introspector obj) throws AAIException {
+        if (obj == null) {
+            throw new AAIException("AAI_7350");
+        }
+
+        try {
+            final Introspector notificationEvent = loader.introspectorFromName("notification-event");
+
+            if (eventHeader.getValue("id") == null) {
+                eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
+            }
+
+            if (eventHeader.getValue("timestamp") == null) {
+                eventHeader.setValue("timestamp", genDate());
+            }
+
+            if (eventHeader.getValue("entity-link") == null) {
+                eventHeader.setValue("entity-link", "UNK");
+            }
+
+            if (eventHeader.getValue("action") == null) {
+                eventHeader.setValue("action", "UNK");
+            }
+
+            if (eventHeader.getValue("event-type") == null) {
+                eventHeader.setValue("event-type", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
+            }
+
+            if (eventHeader.getValue("domain") == null) {
+                eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
+            }
+
+            if (eventHeader.getValue("source-name") == null) {
+                eventHeader.setValue("source-name", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
+            }
+
+            if (eventHeader.getValue("sequence-number") == null) {
+                eventHeader.setValue("sequence-number",
+                        AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
+            }
+
+            if (eventHeader.getValue("severity") == null) {
+                eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
+            }
+
+            if (eventHeader.getValue("version") == null) {
+                eventHeader.setValue("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
+            }
+
+            if (notificationEvent.getValue("cambria-partition") == null) {
+                notificationEvent.setValue("cambria-partition",
+                        AAIConfig.get("aai.notificationEvent.default.partition", AAIConstants.UEB_PUB_PARTITION_AAI));
+            }
+
+            notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
+            notificationEvent.setValue("entity", obj.getUnderlyingObject());
+
+            String entityJson = notificationEvent.marshal(false);
+            JSONObject entityJsonObject = new JSONObject(entityJson);
+
+            JSONObject entityJsonObjectUpdated = new JSONObject();
+
+            JSONObject entityHeader = entityJsonObject.getJSONObject("event-header");
+            String cambriaPartition = entityJsonObject.getString("cambria.partition");
+
+            entityJsonObject.remove("event-header");
+            entityJsonObject.remove("cambria.partition");
+
+            entityJsonObjectUpdated.put("event-header", entityHeader);
+            entityJsonObjectUpdated.put("cambria.partition", cambriaPartition);
+
+            Iterator<String> iter = entityJsonObject.keys();
+            JSONObject entity = new JSONObject();
+            if (iter.hasNext()) {
+                entity = entityJsonObject.getJSONObject(iter.next());
+            }
+
+            entityJsonObjectUpdated.put("entity", entity);
+
+            return entityJsonObjectUpdated.toString();
+        } catch (JSONException e) {
+            throw new AAIException("AAI_7350", e);
+        } catch (AAIUnknownObjectException e) {
+            throw new AAIException("AAI_7350", e);
+        }
+    }
+
+    public String storeEventAndSendToJms(Loader loader, Introspector eventHeader, Introspector obj)
+            throws AAIException {
         if (obj == null) {
             throw new AAIException("AAI_7350");
         }
@@ -293,7 +380,7 @@ public class StoreNotificationEvent {
             notificationEvent.setValue("entity", obj.getUnderlyingObject());
 
             String entityJson = notificationEvent.marshal(false);
-            sendToDmaapJmsQueue(entityJson);
+            sendToKafkaJmsQueue(entityJson);
             return entityJson;
         } catch (JSONException e) {
             throw new AAIException("AAI_7350", e);
@@ -302,7 +389,7 @@ public class StoreNotificationEvent {
         }
     }
 
-    private void sendToDmaapJmsQueue(String entityString) throws JSONException {
+    private void sendToKafkaJmsQueue(String entityString) throws JSONException {
 
         JSONObject entityJsonObject = new JSONObject(entityString);