device flow registry - initial implementation
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceContextImpl.java
index 9aa9b1d37b59662f585bea6393d36252cceefdaf..e1992b187652f26b7d0c0e95e9b5830784525323 100644 (file)
@@ -15,8 +15,10 @@ import io.netty.util.Timeout;
 import io.netty.util.TimerTask;
 import java.math.BigInteger;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.TreeMap;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 import javax.annotation.Nonnull;
@@ -25,21 +27,26 @@ import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
-import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
 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.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.impl.translator.PortUpdateTranslator;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.*;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableFeatures;
 import org.opendaylight.yangtools.yang.binding.ChildOf;
@@ -54,7 +61,7 @@ import org.slf4j.LoggerFactory;
 /**
  *
  */
-public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
+public class DeviceContextImpl implements DeviceContext {
 
     private static final Logger LOG = LoggerFactory.getLogger(DeviceContextImpl.class);
 
@@ -63,11 +70,13 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
     private final DataBroker dataBroker;
     private final XidGenerator xidGenerator;
     private final HashedWheelTimer hashedWheelTimer;
-    private Map<Long, RequestContext> requests = new HashMap<Long, RequestContext>();
+    private Map<Long, RequestContext> requests = new TreeMap<>();
 
     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,
@@ -81,6 +90,7 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
         txChainManager = new TransactionChainManager(dataBroker, 500L);
         auxiliaryConnectionContexts = new HashMap<>();
         requests = new HashMap<>();
+        flowRegistry = new DeviceFlowRegistry();
     }
 
     /**
@@ -89,6 +99,7 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
      */
     void submitTransaction() {
         txChainManager.submitTransaction();
+        txChainManager.enableCounter();
         hashedWheelTimer.newTimeout(new TimerTask() {
             @Override
             public void run(final Timeout timeout) throws Exception {
@@ -126,7 +137,7 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
 
     @Override
     public <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store,
-            final InstanceIdentifier<T> path, final T data) {
+                                                          final InstanceIdentifier<T> path, final T data) {
         txChainManager.writeToTransaction(store, path, data);
     }
 
@@ -161,67 +172,97 @@ 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());
         }
     }
 
     @Override
     public void processException(final Xid xid, final DeviceDataException deviceDataException) {
+
+        LOG.trace("Processing exception for xid : {}", xid.getValue());
+
         final RequestContext requestContext = getRequests().get(xid.getValue());
 
-        final SettableFuture replyFuture = requestContext.getFuture();
-        getRequests().remove(xid.getValue());
-        final RpcResult<List<OfHeader>> rpcResult= RpcResultBuilder
-                .<List<OfHeader>>failed()
-                .withError(RpcError.ErrorType.APPLICATION, "Message processing failed", deviceDataException)
-                .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<OfHeader>> rpcResult = RpcResultBuilder
+                    .<List<OfHeader>>failed()
+                    .withError(RpcError.ErrorType.APPLICATION, String.format("Message processing failed : %s", deviceDataException.getError()), deviceDataException)
+                    .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());
         }
     }
 
@@ -232,7 +273,7 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
 
     @Override
     public void processPortStatusMessage(final PortStatusMessage portStatus) {
-        final TranslatorKey translatorKey = new TranslatorKey(portStatus.getVersion(), PortUpdateTranslator.class.getName());
+        final TranslatorKey translatorKey = new TranslatorKey(portStatus.getVersion(), PortStatusMessage.class.getName());
         final MessageTranslator<PortStatusMessage, FlowCapableNodeConnector> messageTranslator = translatorLibrary.lookupTranslator(translatorKey);
         final FlowCapableNodeConnector nodeConnector = messageTranslator.translate(portStatus, this, null);
         //TODO write into datastore
@@ -246,10 +287,21 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
         //TODO publish to MD-SAL
     }
 
+    @Override
+    public TranslatorLibrary oook() {
+        return translatorLibrary;
+    }
+
+    @Override
     public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
         this.translatorLibrary = translatorLibrary;
     }
 
+    @Override
+    public HashedWheelTimer getTimer() {
+        return this.hashedWheelTimer;
+    }
+
 
     private class XidGenerator {
 
@@ -259,4 +311,17 @@ public class DeviceContextImpl implements DeviceContext, DeviceReplyProcessor {
             return new Xid(xid.incrementAndGet());
         }
     }
+
+    @Override
+    public RequestContext extractNextOutstandingMessage(long barrierXid) {
+        RequestContext nextMessage = null;
+        Iterator<Long> keyIterator = requests.keySet().iterator();
+        if (keyIterator.hasNext()) {
+            Long oldestXid = keyIterator.next();
+            if (oldestXid < barrierXid) {
+                nextMessage = requests.remove(oldestXid);
+            }
+        }
+        return nextMessage;
+    }
 }