Merge "Bug 2945: Fix read failures in StatAbstractListenCommit"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImpl.java
index d42c5bef808d6bab87079503ef77bca9587f4a3b..45bf8d7f3d67b955c686b97c94a07cde9cadfd91 100644 (file)
@@ -12,31 +12,25 @@ import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.JdkFutureAdapters;
 import com.google.common.util.concurrent.ListenableFuture;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.concurrent.Future;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
+import com.google.common.util.concurrent.SettableFuture;
+import io.netty.util.HashedWheelTimer;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
+import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextReadyHandler;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
+import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
 import org.opendaylight.openflowplugin.impl.rpc.RequestContextImpl;
-import org.opendaylight.openflowplugin.impl.rpc.RpcContextImpl;
-import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
@@ -46,16 +40,30 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
-import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 
 /**
  *
@@ -64,34 +72,62 @@ public class DeviceManagerImpl implements DeviceManager {
 
     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
 
-    private final BindingAwareBroker.ProviderContext providerContext;
+    private static final long TICK_DURATION = 500; // 0.5 sec.
+
+    private final RpcManager rpcManager;
+    private final DataBroker dataBroker;
+    private final HashedWheelTimer hashedWheelTimer;
+    private RequestContextStack dummyRequestContextStack;
+    private TranslatorLibrary translatorLibrary;
+
+
+    public DeviceManagerImpl(@Nonnull final RpcManager rpcManager, @Nonnull final DataBroker dataBroker) {
+        this.rpcManager = Preconditions.checkNotNull(rpcManager);
+        this.dataBroker = Preconditions.checkNotNull(dataBroker);
+        hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, 10);
 
+        dummyRequestContextStack = new RequestContextStack() {
+            @Override
+            public <T> void forgetRequestContext(RequestContext<T> requestContext) {
+                //NOOP
+            }
+
+            @Override
+            public <T> SettableFuture<RpcResult<T>> storeOrFail(RequestContext<T> data) {
+                return data.getFuture();
+            }
 
-    public DeviceManagerImpl (@Nonnull final ProviderContext providerContext) {
-        this.providerContext = Preconditions.checkNotNull(providerContext);
+            @Override
+            public <T> RequestContext<T> createRequestContext() {
+                return new RequestContextImpl<>(this);
+            }
+        };
     }
 
     @Override
     public void deviceConnected(@CheckForNull final ConnectionContext connectionContext) {
         Preconditions.checkArgument(connectionContext != null);
-        final DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
         final DeviceState deviceState = new DeviceStateImpl(connectionContext.getFeatures(), connectionContext.getNodeId());
-        final DeviceContext deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker);
+        final DeviceContextImpl deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker, hashedWheelTimer);
+        deviceContext.setTranslatorLibrary(translatorLibrary);
+        final OpenflowProtocolListenerFullImpl messageListener = new OpenflowProtocolListenerFullImpl(
+                connectionContext.getConnectionAdapter(), deviceContext);
+        connectionContext.getConnectionAdapter().setMessageListener(messageListener);
 
         final Xid nodeDescXid = deviceContext.getNextXid();
-        final ListenableFuture<Collection<MultipartReply>> replyDesc = getNodeStaticInfo(nodeDescXid, connectionContext,
+        final ListenableFuture<Collection<MultipartReply>> replyDesc = getNodeStaticInfo(nodeDescXid, messageListener,
                 MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
 
         final Xid nodeMeterXid = deviceContext.getNextXid();
-        final ListenableFuture<Collection<MultipartReply>> replyMeterFeature = getNodeStaticInfo(nodeMeterXid, connectionContext,
+        final ListenableFuture<Collection<MultipartReply>> replyMeterFeature = getNodeStaticInfo(nodeMeterXid, messageListener,
                 MultipartType.OFPMPMETERFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
 
         final Xid nodeGroupXid = deviceContext.getNextXid();
-        final ListenableFuture<Collection<MultipartReply>> replyGroupFeatures = getNodeStaticInfo(nodeGroupXid, connectionContext,
+        final ListenableFuture<Collection<MultipartReply>> replyGroupFeatures = getNodeStaticInfo(nodeGroupXid, messageListener,
                 MultipartType.OFPMPGROUPFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
 
         final Xid nodeTableXid = deviceContext.getNextXid();
-        final ListenableFuture<Collection<MultipartReply>> replyTableFeatures = getNodeStaticInfo(nodeTableXid, connectionContext,
+        final ListenableFuture<Collection<MultipartReply>> replyTableFeatures = getNodeStaticInfo(nodeTableXid, messageListener,
                 MultipartType.OFPMPTABLEFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
 
         final ListenableFuture<List<Collection<MultipartReply>>> deviceFeaturesFuture =
@@ -100,11 +136,8 @@ public class DeviceManagerImpl implements DeviceManager {
             @Override
             public void onSuccess(final List<Collection<MultipartReply>> result) {
                 // FIXME : add statistics
-                final RpcManager rpcManager = new RpcManagerImpl(providerContext);
-                final RequestContextStack rcs = new RpcContextImpl(providerContext, deviceContext);
-                final RequestContext<?> requestContext = new RequestContextImpl<>(rcs);
-                rpcManager.deviceConnected(deviceContext, requestContext);
-                ((DeviceContextImpl) deviceContext).submitTransaction();
+                rpcManager.deviceConnected(deviceContext);
+                deviceContext.submitTransaction();
             }
 
             @Override
@@ -115,44 +148,60 @@ public class DeviceManagerImpl implements DeviceManager {
     }
 
     @Override
-    public void sendMessage(final DataObject dataObject, final RequestContext requestContext) {
+    public void addRequestContextReadyHandler(final DeviceContextReadyHandler deviceContextReadyHandler) {
         // TODO Auto-generated method stub
-
     }
 
     @Override
-    public Xid sendRequest(final DataObject dataObject, final RequestContext requestContext) {
-        // TODO Auto-generated method stub
-        return null;
+    public TranslatorLibrary oook() {
+        return translatorLibrary;
     }
 
     @Override
-    public void addRequestContextReadyHandler(final DeviceContextReadyHandler deviceContextReadyHandler) {
-        // TODO Auto-generated method stub
-
+    public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
+        this.translatorLibrary = translatorLibrary;
     }
 
-    private static ListenableFuture<Collection<MultipartReply>> getNodeStaticInfo(final Xid xid, final ConnectionContext cContext,
-            final MultipartType type, final DeviceContext dContext, final InstanceIdentifier<Node> nodeII, final short version) {
-        final ListenableFuture<Collection<MultipartReply>> future = cContext.registerMultipartMsg(xid.getValue());
-        Futures.addCallback(future, new FutureCallback<Collection<MultipartReply>>() {
+    private ListenableFuture<Collection<MultipartReply>> getNodeStaticInfo(final Xid xid,
+                                                                           final MultiMsgCollector multiMsgCollector, final MultipartType type, final DeviceContext dContext,
+                                                                           final InstanceIdentifier<Node> nodeII, final short version) {
+        final ListenableFuture<Collection<MultipartReply>> future = multiMsgCollector.registerMultipartMsg(xid.getValue());
+
+        RequestContext<List<MultipartReply>> requestContext = dummyRequestContextStack.createRequestContext();
+        dContext.hookRequestCtx(xid, requestContext);
+        Futures.addCallback(requestContext.getFuture(), new FutureCallback<RpcResult<List<MultipartReply>>>() {
             @Override
-            public void onSuccess(final Collection<MultipartReply> result) {
-                Preconditions.checkArgument(result != null);
-                translateAndWriteReply(type, dContext, nodeII, result);
+            public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
+                List<MultipartReply> result = rpcResult.getResult();
+                if (result != null) {
+                    translateAndWriteReply(type, dContext, nodeII, result);
+                } else {
+                    Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
+                    while (rpcErrorIterator.hasNext()) {
+                        Throwable t = rpcErrorIterator.next().getCause();
+                        LOG.info("Failed to retrieve static node {} info: {}", type, t.getMessage());
+                        LOG.trace("Detailed error:", t);
+                    }
+                    if (MultipartType.OFPMPTABLE.equals(type)) {
+                        makeEmptyTables(dContext, nodeII, dContext.getPrimaryConnectionContext().getFeatures().getTables());
+                    }
+                }
             }
+
             @Override
             public void onFailure(final Throwable t) {
                 LOG.info("Failed to retrieve static node {} info: {}", type, t.getMessage());
             }
         });
-        final Future<RpcResult<Void>> rpcFuture = cContext.getConnectionAdapter()
+
+        final Future<RpcResult<Void>> rpcFuture = dContext.getPrimaryConnectionContext().getConnectionAdapter()
                 .multipartRequest(MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type));
         Futures.addCallback(JdkFutureAdapters.listenInPoolThread(rpcFuture), new FutureCallback<RpcResult<Void>>() {
             @Override
             public void onSuccess(final RpcResult<Void> result) {
                 // NOOP
             }
+
             @Override
             public void onFailure(final Throwable t) {
                 future.cancel(true);
@@ -161,45 +210,58 @@ public class DeviceManagerImpl implements DeviceManager {
         return future;
     }
 
+    // FIXME : remove after ovs tableFeatures fix
+    private static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII, final Short nrOfTables) {
+        for (int i = 0; i < nrOfTables; i++) {
+            final short tId = (short) i;
+            final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tId));
+            dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, new TableBuilder().setId(tId).build());
+        }
+    }
+
     private static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
-            final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
+                                               final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
         for (final MultipartReply reply : result) {
+            final MultipartReplyBody body = reply.getMultipartReplyBody();
             switch (type) {
-            case OFPMPDESC:
-                Preconditions.checkArgument(reply instanceof MultipartReplyDesc);
-                final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator((MultipartReplyDesc) reply);
-                final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
-                dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
-                break;
-
-            case OFPMPTABLEFEATURES:
-                Preconditions.checkArgument(reply instanceof MultipartReplyTableFeatures);
-                final List<TableFeatures> tables = NodeStaticReplyTranslatorUtil.nodeTableFeatureTranslator((MultipartReplyTableFeatures) reply);
-                for (final TableFeatures table : tables) {
-                    final Short tableId = table.getTableId();
-                    final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId));
-                    dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, new TableBuilder().setId(tableId).setTableFeatures(Collections.singletonList(table)).build());
-                }
-                break;
-
-            case OFPMPMETERFEATURES:
-                Preconditions.checkArgument(reply instanceof MultipartReplyMeterFeatures);
-                final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil.nodeMeterFeatureTranslator((MultipartReplyMeterFeatures) reply);
-                final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII.augmentation(NodeMeterFeatures.class);
-                dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
-                break;
-
-            case OFPMPGROUPFEATURES:
-                Preconditions.checkArgument(reply instanceof MultipartReplyGroupFeatures);
-                final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil.nodeGroupFeatureTranslator((MultipartReplyGroupFeatures) reply);
-                final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII.augmentation(NodeGroupFeatures.class);
-                dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
-                break;
-
-            default:
-                throw new IllegalArgumentException("Unnexpected MultipartType " + type);
+                case OFPMPDESC:
+                    Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
+                    final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
+                    final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc);
+                    final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
+                    dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
+                    break;
+
+                case OFPMPTABLEFEATURES:
+                    Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase);
+                    final MultipartReplyTableFeatures tableFeatures = ((MultipartReplyTableFeaturesCase) body).getMultipartReplyTableFeatures();
+                    final List<TableFeatures> tables = NodeStaticReplyTranslatorUtil.nodeTableFeatureTranslator(tableFeatures);
+                    for (final TableFeatures table : tables) {
+                        final Short tableId = table.getTableId();
+                        final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId));
+                        dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, new TableBuilder().setId(tableId).setTableFeatures(Collections.singletonList(table)).build());
+                    }
+                    break;
+
+                case OFPMPMETERFEATURES:
+                    Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase);
+                    final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body).getMultipartReplyMeterFeatures();
+                    final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil.nodeMeterFeatureTranslator(meterFeatures);
+                    final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII.augmentation(NodeMeterFeatures.class);
+                    dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
+                    break;
+
+                case OFPMPGROUPFEATURES:
+                    Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase);
+                    final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body).getMultipartReplyGroupFeatures();
+                    final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil.nodeGroupFeatureTranslator(groupFeatures);
+                    final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII.augmentation(NodeGroupFeatures.class);
+                    dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
+                    break;
+
+                default:
+                    throw new IllegalArgumentException("Unnexpected MultipartType " + type);
             }
         }
     }
-
 }