device flow registry - initial implementation
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceContextImpl.java
index d84a5e8e4930a75cf29b018bba90702682315710..e1992b187652f26b7d0c0e95e9b5830784525323 100644 (file)
@@ -7,7 +7,12 @@
  */
 package org.opendaylight.openflowplugin.impl.device;
 
-import javax.annotation.Nonnull;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.SettableFuture;
+import io.netty.util.HashedWheelTimer;
+import io.netty.util.Timeout;
+import io.netty.util.TimerTask;
 import java.math.BigInteger;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -16,13 +21,7 @@ import java.util.Map;
 import java.util.TreeMap;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
-import com.google.common.util.concurrent.SettableFuture;
-import io.netty.util.HashedWheelTimer;
-import io.netty.util.Timeout;
-import io.netty.util.TimerTask;
+import javax.annotation.Nonnull;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -34,9 +33,11 @@ import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
 import org.opendaylight.openflowplugin.api.openflow.device.exception.DeviceDataException;
-import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor;
+import org.opendaylight.openflowplugin.api.openflow.device.listener.OpenflowMessageListenerFacade;
+import org.opendaylight.openflowplugin.api.openflow.flow.registry.FlowRegistry;
 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
+import org.opendaylight.openflowplugin.impl.flow.registry.DeviceFlowRegistry;
 import org.opendaylight.openflowplugin.impl.translator.PacketReceivedTranslator;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
@@ -56,18 +57,11 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import javax.annotation.Nonnull;
-import java.math.BigInteger;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicLong;
 
 /**
  *
  */
-public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
+public class DeviceContextImpl implements DeviceContext {
 
     private static final Logger LOG = LoggerFactory.getLogger(DeviceContextImpl.class);
 
@@ -81,6 +75,8 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
     private final Map<SwitchConnectionDistinguisher, ConnectionContext> auxiliaryConnectionContexts;
     private final TransactionChainManager txChainManager;
     private TranslatorLibrary translatorLibrary;
+    private OpenflowMessageListenerFacade openflowMessageListenerFacade;
+    private FlowRegistry flowRegistry;
 
     @VisibleForTesting
     DeviceContextImpl(@Nonnull final ConnectionContext primaryConnectionContext,
@@ -94,6 +90,7 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
         txChainManager = new TransactionChainManager(dataBroker, 500L);
         auxiliaryConnectionContexts = new HashMap<>();
         requests = new HashMap<>();
+        flowRegistry = new DeviceFlowRegistry();
     }
 
     /**
@@ -175,49 +172,72 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
         requests.put(xid.getValue(), requestFutureContext);
     }
 
+    @Override
+    public void attachOpenflowMessageListener(final OpenflowMessageListenerFacade openflowMessageListenerFacade) {
+        this.openflowMessageListenerFacade = openflowMessageListenerFacade;
+        primaryConnectionContext.getConnectionAdapter().setMessageListener(openflowMessageListenerFacade);
+    }
+
+    @Override
+    public OpenflowMessageListenerFacade getOpenflowMessageListenerFacade() {
+        return this.openflowMessageListenerFacade;
+    }
+
+    @Override
+    public FlowRegistry getFlowRegistry() {
+        return flowRegistry;
+    }
+
     @Override
     public void processReply(final OfHeader ofHeader) {
         final RequestContext requestContext = getRequests().get(ofHeader.getXid());
-        final SettableFuture replyFuture = requestContext.getFuture();
-        getRequests().remove(ofHeader.getXid());
-        RpcResult<OfHeader> rpcResult;
-
-        if (ofHeader instanceof Error) {
-            final Error error = (Error) ofHeader;
-            final String message = "Operation on device failed";
-            rpcResult = RpcResultBuilder
-                    .<OfHeader>failed()
-                    .withError(RpcError.ErrorType.APPLICATION, message, new DeviceDataException(message, error))
-                    .build();
-        } else {
-            rpcResult = RpcResultBuilder
-                    .<OfHeader>success()
-                    .withResult(ofHeader)
-                    .build();
-        }
+        if (null != requestContext) {
+            final SettableFuture replyFuture = requestContext.getFuture();
+            getRequests().remove(ofHeader.getXid());
+            RpcResult<OfHeader> rpcResult;
+            if (ofHeader instanceof Error) {
+                final Error error = (Error) ofHeader;
+                final String message = "Operation on device failed";
+                rpcResult = RpcResultBuilder
+                        .<OfHeader>failed()
+                        .withError(RpcError.ErrorType.APPLICATION, message, new DeviceDataException(message, error))
+                        .build();
+            } else {
+                rpcResult = RpcResultBuilder
+                        .<OfHeader>success()
+                        .withResult(ofHeader)
+                        .build();
+            }
 
-        replyFuture.set(rpcResult);
-        try {
-            requestContext.close();
-        } catch (final Exception e) {
-            LOG.error("Closing RequestContext failed: ", e);
+            replyFuture.set(rpcResult);
+            try {
+                requestContext.close();
+            } catch (final Exception e) {
+                LOG.error("Closing RequestContext failed: ", e);
+            }
+        } else {
+            LOG.error("Can't find request context registered for xid : {}", ofHeader.getXid());
         }
     }
 
     @Override
     public void processReply(final Xid xid, final List<MultipartReply> ofHeaderList) {
         final RequestContext requestContext = getRequests().get(xid.getValue());
-        final SettableFuture replyFuture = requestContext.getFuture();
-        getRequests().remove(xid.getValue());
-        final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
-                .<List<MultipartReply>>success()
-                .withResult(ofHeaderList)
-                .build();
-        replyFuture.set(rpcResult);
-        try {
-            requestContext.close();
-        } catch (final Exception e) {
-            LOG.error("Closing RequestContext failed: ", e);
+        if (null != requestContext) {
+            final SettableFuture replyFuture = requestContext.getFuture();
+            getRequests().remove(xid.getValue());
+            final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
+                    .<List<MultipartReply>>success()
+                    .withResult(ofHeaderList)
+                    .build();
+            replyFuture.set(rpcResult);
+            try {
+                requestContext.close();
+            } catch (final Exception e) {
+                LOG.error("Closing RequestContext failed: ", e);
+            }
+        } else {
+            LOG.error("Can't find request context registered for xid : {}", xid.getValue());
         }
     }
 
@@ -242,7 +262,7 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
                 LOG.error("Closing RequestContext failed: ", e);
             }
         } else {
-            LOG.error("Can't find request context registered for xid : {}", xid);
+            LOG.error("Can't find request context registered for xid : {}", xid.getValue());
         }
     }
 
@@ -277,6 +297,11 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
         this.translatorLibrary = translatorLibrary;
     }
 
+    @Override
+    public HashedWheelTimer getTimer() {
+        return this.hashedWheelTimer;
+    }
+
 
     private class XidGenerator {