Merge of new rebased code
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / openecomp / appc / adapter / messaging / dmaap / impl / EventSenderDmaapImpl.java
  * ============LICENSE_END=========================================================
  */
 
-package org.openecomp.appc.adapter.dmaap.impl;
+package org.openecomp.appc.adapter.messaging.dmaap.impl;
+
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+import org.openecomp.sdnc.sli.SvcLogicContext;
 
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 
-import org.openecomp.appc.adapter.dmaap.EventSender;
-import org.openecomp.appc.adapter.dmaap.Producer;
-import org.openecomp.appc.adapter.dmaap.DmaapDestination;
-import org.openecomp.appc.adapter.dmaap.event.EventHeader;
-import org.openecomp.appc.adapter.dmaap.event.EventMessage;
-import org.openecomp.appc.adapter.dmaap.event.EventStatus;
-import org.openecomp.appc.adapter.dmaap.DmaapProducer;
+import org.openecomp.appc.adapter.message.EventSender;
+import org.openecomp.appc.adapter.message.MessageDestination;
+import org.openecomp.appc.adapter.message.Producer;
+import org.openecomp.appc.adapter.message.event.EventHeader;
+import org.openecomp.appc.adapter.message.event.EventMessage;
+import org.openecomp.appc.adapter.message.event.EventStatus;
+import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
 import org.openecomp.appc.configuration.Configuration;
 import org.openecomp.appc.configuration.ConfigurationFactory;
 import org.openecomp.appc.exceptions.APPCException;
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-import org.openecomp.sdnc.sli.SvcLogicContext;
 
-
-public class EventSenderImpl implements EventSender
+public class EventSenderDmaapImpl implements EventSender
 {
-    private static final EELFLogger LOG = EELFManager.getInstance().getLogger(EventSenderImpl.class);
-    public static final String EVENT_TOPIC_WRITE = "event.topic.write";
-    public static final String EVENT_CLIENT_KEY = "event.client.key";
-    public static final String EVENT_CLIENT_SECRET = "event.client.secret";
-    public static final String EVENT_POOL_MEMBERS = "event.pool.members";
+    private static final EELFLogger LOG = EELFManager.getInstance().getLogger(EventSenderDmaapImpl.class);
+    public static final String EVENT_TOPIC_WRITE = "dmaap.event.topic.write";
+    public static final String DMAAP_USERNAME = "dmaap.appc.username";
+    public static final String DMAAP_PASSWORD = "dmaap.appc.password";
+    public static final String EVENT_POOL_MEMBERS = "dmaap.event.pool.members";
 
     private static Configuration configuration = ConfigurationFactory.getConfiguration();
 
@@ -59,21 +59,21 @@ public class EventSenderImpl implements EventSender
         this.producerMap = producerMap;
     }
 
-    public EventSenderImpl(){
+    public EventSenderDmaapImpl(){
 
     }
 
     public void initialize(){
         Properties properties = configuration.getProperties();
         String writeTopic;
-        String apiKey;
-        String apiSecret;
+        String username;
+        String password;
         final List<String> pool = new ArrayList<>();
 
-        for(DmaapDestination destination:DmaapDestination.values()){
+        for(MessageDestination destination: MessageDestination.values()){
             writeTopic = properties.getProperty(destination + "." +  EVENT_TOPIC_WRITE);
-            apiKey = properties.getProperty(destination + "." + EVENT_CLIENT_KEY);
-            apiSecret = properties.getProperty(destination + "." + EVENT_CLIENT_SECRET);
+            username = properties.getProperty(destination + "." + DMAAP_USERNAME);
+            password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
             String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
 
             if (hostNames != null && !hostNames.isEmpty()) {
@@ -83,12 +83,8 @@ public class EventSenderImpl implements EventSender
 
             LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
             LOG.debug(String.format("writeTopic = %s, taken from property: %s", writeTopic, destination + "." + EVENT_TOPIC_WRITE));
-            LOG.debug(String.format("apiKey = %s, taken from property: %s", apiKey, destination + "." + EVENT_CLIENT_KEY));
-            Producer producer = new DmaapProducer(pool, writeTopic);
-
-            if (apiKey != null && apiSecret != null) {
-                producer.updateCredentials(apiKey, apiSecret);
-            }
+            LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
+            Producer producer = new DmaapProducerImpl(pool, writeTopic,username, password);
 
             for (String url : pool) {
                 if (url.contains("3905") || url.contains("https")) {
@@ -103,7 +99,7 @@ public class EventSenderImpl implements EventSender
     }
 
     @Override
-    public boolean sendEvent(DmaapDestination destination,EventMessage msg) {
+    public boolean sendEvent(MessageDestination destination, EventMessage msg) {
         String jsonStr = msg.toJson();
         String id = msg.getEventHeader().getEventId();
         LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
@@ -112,7 +108,43 @@ public class EventSenderImpl implements EventSender
     }
 
     @Override
-    public boolean sendEvent(DmaapDestination destination,Map<String, String> params, SvcLogicContext ctx) throws APPCException {
+    public boolean sendEvent(MessageDestination destination, EventMessage msg, String eventTopicName) {
+        String jsonStr = msg.toJson();
+        String id = msg.getEventHeader().getEventId();
+        LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
+        Producer producer = createProducer(destination, eventTopicName);
+        return producer.post(id, jsonStr);
+    }
+    
+    private Producer createProducer(MessageDestination destination, String eventTopicName) {
+        Properties properties = configuration.getProperties();
+        final List<String> pool = new ArrayList<>();
+        String username = properties.getProperty(destination + "." + DMAAP_USERNAME);
+        String password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
+        String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
+
+        if (hostNames != null && !hostNames.isEmpty()) {
+            LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
+            Collections.addAll(pool, hostNames.split(","));
+        }
+
+        LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
+        LOG.debug(String.format("writeTopic = %s, taken from property: %s", eventTopicName, destination + "." + EVENT_TOPIC_WRITE));
+        LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
+        Producer producer = new DmaapProducerImpl(pool, eventTopicName,username, password);
+
+        for (String url : pool) {
+            if (url.contains("3905") || url.contains("https")) {
+                LOG.debug("Producer should use HTTPS");
+                producer.useHttps(true);
+                break;
+            }
+        }
+        return producer;
+    }
+
+    @Override
+    public boolean sendEvent(MessageDestination destination, Map<String, String> params, SvcLogicContext ctx) throws APPCException {
 
         if (params == null) {
             String message = "Parameters map is empty (null)";
@@ -134,10 +166,10 @@ public class EventSenderImpl implements EventSender
             LOG.error(message);
             throw new APPCException(message);
         }
-        EventMessage dmaapEventMessage = new EventMessage(
+        EventMessage eventMessage = new EventMessage(
                         new EventHeader(eventTime, apiVer, eventId),
                         new EventStatus(code, reason));
 
-        return sendEvent(destination,dmaapEventMessage);
+        return sendEvent(destination,eventMessage);
     }
 }