BUG-1075: ingress back pressure
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / SwitchConnectionHandlerImpl.java
index 647626643f23010b4335836e2cd1359f51a094d3..ecb9757f85c007e1e5cd092878a2be0d051f3822 100644 (file)
@@ -9,15 +9,50 @@
 package org.opendaylight.openflowplugin.openflow.md.core;
 
 import java.net.InetAddress;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
+import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
+import org.opendaylight.openflowplugin.openflow.md.queue.MessageSpy;
+import org.opendaylight.openflowplugin.openflow.md.queue.QueueProcessorLightImpl;
+import org.opendaylight.yangtools.yang.binding.DataContainer;
 
 /**
- * @author mirehak
- *
+ * basic interconnecting piece between plugin and library 
  */
 public class SwitchConnectionHandlerImpl implements SwitchConnectionHandler {
+    
+    private ScheduledThreadPoolExecutor spyPool; 
+
+    private QueueProcessorLightImpl queueProcessor;
+    private ErrorHandler errorHandler;
+    private MessageSpy<DataContainer> messageSpy;
+    private int spyRate = 10;
+
+    /**
+     *
+     */
+    public SwitchConnectionHandlerImpl() {
+        queueProcessor = new QueueProcessorLightImpl();
+        
+        //TODO: implement shutdown invocation upon service stop event
+        spyPool = new ScheduledThreadPoolExecutor(1);
+    }
+
+    /**
+     * wire all up
+     */
+    public void init() {
+        queueProcessor.setTranslatorMapping(OFSessionUtil.getTranslatorMap());
+        queueProcessor.setPopListenersMapping(OFSessionUtil.getPopListenerMapping());
+        queueProcessor.setMessageSpy(messageSpy);
+        
+        queueProcessor.init();
+        
+        spyPool.scheduleAtFixedRate(messageSpy, spyRate, spyRate, TimeUnit.SECONDS);
+    }
 
     @Override
     public boolean accept(InetAddress address) {
@@ -27,7 +62,23 @@ public class SwitchConnectionHandlerImpl implements SwitchConnectionHandler {
 
     @Override
     public void onSwitchConnected(ConnectionAdapter connectionAdapter) {
-        ConnectionConductor conductor = ConnectionConductorFactory.createConductor(connectionAdapter);
+        ConnectionConductor conductor = ConnectionConductorFactory.createConductor(
+                connectionAdapter, queueProcessor);
+        conductor.setErrorHandler(errorHandler);
+    }
+    
+    /**
+     * @param messageSpy the messageSpy to set
+     */
+    public void setMessageSpy(MessageSpy<DataContainer> messageSpy) {
+        this.messageSpy = messageSpy;
+    }
+    
+    /**
+     * @param errorHandler the errorHandler to set
+     */
+    public void setErrorHandler(ErrorHandler errorHandler) {
+        this.errorHandler = errorHandler;
     }
 
 }