device context provides attached any messege type listener
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImpl.java
index 735fd7685ecabe1cdb5f1d4aacc0acc7b9afe344..642bbe12c56a2a93fdda6374e3d06f84bf77aed6 100644 (file)
@@ -35,17 +35,18 @@ import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
 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.services.OFJResult2RequestCtxFuture;
 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;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
 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;
@@ -74,7 +75,6 @@ public class DeviceManagerImpl implements DeviceManager {
 
     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;
@@ -82,8 +82,7 @@ public class DeviceManagerImpl implements DeviceManager {
     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
 
 
-    public DeviceManagerImpl(@Nonnull final RpcManager rpcManager, @Nonnull final DataBroker dataBroker) {
-        this.rpcManager = Preconditions.checkNotNull(rpcManager);
+    public DeviceManagerImpl(@Nonnull final DataBroker dataBroker) {
         this.dataBroker = Preconditions.checkNotNull(dataBroker);
         hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, 10);
 
@@ -120,34 +119,38 @@ public class DeviceManagerImpl implements DeviceManager {
     @Override
     public void deviceConnected(@CheckForNull final ConnectionContext connectionContext) {
         Preconditions.checkArgument(connectionContext != null);
+
         final DeviceState deviceState = new DeviceStateImpl(connectionContext.getFeatures(), connectionContext.getNodeId());
+
         final DeviceContextImpl deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker, hashedWheelTimer);
+
+        deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceState.getNodeInstanceIdentifier(), new NodeBuilder().setId(deviceState.getNodeId()).build());
+
         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, messageListener,
+        deviceContext.attachAnyMessageTypeListener(messageListener);
+
+        final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(messageListener,
                 MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
 
-        final Xid nodeMeterXid = deviceContext.getNextXid();
-        final ListenableFuture<Collection<MultipartReply>> replyMeterFeature = getNodeStaticInfo(nodeMeterXid, messageListener,
+        final ListenableFuture<RpcResult<List<MultipartReply>>> replyMeterFeature = getNodeStaticInfo(messageListener,
                 MultipartType.OFPMPMETERFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
 
-        final Xid nodeGroupXid = deviceContext.getNextXid();
-        final ListenableFuture<Collection<MultipartReply>> replyGroupFeatures = getNodeStaticInfo(nodeGroupXid, messageListener,
+        final ListenableFuture<RpcResult<List<MultipartReply>>> replyGroupFeatures = getNodeStaticInfo(messageListener,
                 MultipartType.OFPMPGROUPFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
 
-        final Xid nodeTableXid = deviceContext.getNextXid();
-        final ListenableFuture<Collection<MultipartReply>> replyTableFeatures = getNodeStaticInfo(nodeTableXid, messageListener,
+        final ListenableFuture<RpcResult<List<MultipartReply>>> replyTableFeatures = getNodeStaticInfo(messageListener,
                 MultipartType.OFPMPTABLEFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
 
-        final ListenableFuture<List<Collection<MultipartReply>>> deviceFeaturesFuture =
+        final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture =
                 Futures.allAsList(Arrays.asList(replyDesc, replyMeterFeature, replyGroupFeatures, replyTableFeatures));
-        Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<Collection<MultipartReply>>>() {
+
+        Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
             @Override
-            public void onSuccess(final List<Collection<MultipartReply>> result) {
+            public void onSuccess(final List<RpcResult<List<MultipartReply>>> result) {
                 // wake up statistics
                 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
             }
@@ -169,28 +172,30 @@ public class DeviceManagerImpl implements DeviceManager {
         this.translatorLibrary = translatorLibrary;
     }
 
-    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());
+    private ListenableFuture<RpcResult<List<MultipartReply>>> getNodeStaticInfo(final MultiMsgCollector multiMsgCollector, final MultipartType type, final DeviceContext deviceContext,
+                                                                                final InstanceIdentifier<Node> nodeII, final short version) {
 
+        final Xid xid = deviceContext.getNextXid();
         final RequestContext<List<MultipartReply>> requestContext = dummyRequestContextStack.createRequestContext();
-        dContext.hookRequestCtx(xid, requestContext);
+        requestContext.setXid(xid);
+        multiMsgCollector.registerMultipartXid(xid.getValue());
         Futures.addCallback(requestContext.getFuture(), new FutureCallback<RpcResult<List<MultipartReply>>>() {
             @Override
             public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
                 final List<MultipartReply> result = rpcResult.getResult();
                 if (result != null) {
-                    translateAndWriteReply(type, dContext, nodeII, result);
+                    translateAndWriteReply(type, deviceContext, nodeII, result);
                 } else {
                     final Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
                     while (rpcErrorIterator.hasNext()) {
-                        final Throwable t = rpcErrorIterator.next().getCause();
-                        LOG.info("Failed to retrieve static node {} info: {}", type, t.getMessage());
-                        LOG.trace("Detailed error:", t);
+                        RpcError rpcError = rpcErrorIterator.next();
+                        LOG.info("Failed to retrieve static node {} info: {}", type, rpcError.getMessage());
+                        if (null != rpcError.getCause()) {
+                            LOG.trace("Detailed error:", rpcError.getCause());
+                        }
                     }
                     if (MultipartType.OFPMPTABLE.equals(type)) {
-                        makeEmptyTables(dContext, nodeII, dContext.getPrimaryConnectionContext().getFeatures().getTables());
+                        makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
                     }
                 }
             }
@@ -201,20 +206,12 @@ public class DeviceManagerImpl implements DeviceManager {
             }
         });
 
-        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
-            }
+        final ListenableFuture<RpcResult<Void>> rpcFuture = JdkFutureAdapters.listenInPoolThread(deviceContext.getPrimaryConnectionContext().getConnectionAdapter()
+                .multipartRequest(MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type)));
+        OFJResult2RequestCtxFuture OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture(requestContext, deviceContext);
+        OFJResult2RequestCtxFuture.processResultFromOfJava(rpcFuture);
 
-            @Override
-            public void onFailure(final Throwable t) {
-                future.cancel(true);
-            }
-        });
-        return future;
+        return requestContext.getFuture();
     }
 
     // FIXME : remove after ovs tableFeatures fix