fix callback handling
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / PacketProcessingServiceImpl.java
index 1447e261822b7cb83a5a90b6fc166cb15dde5982..db78b0036456374a2a64e4bdc2376b4a1a8cde04 100644 (file)
@@ -1,34 +1,71 @@
 /**
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
- * 
+ *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.openflowplugin.impl.services;
 
-import java.math.BigInteger;
+import com.google.common.base.Function;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
 import java.util.concurrent.Future;
+import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
+import org.opendaylight.openflowplugin.api.openflow.device.Xid;
+import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PacketOutConvertor;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.ConnectionCookie;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
+import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 
 public class PacketProcessingServiceImpl extends CommonService implements PacketProcessingService {
 
+    public PacketProcessingServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
+        super(requestContextStack, deviceContext);
+    }
+
     @Override
     public Future<RpcResult<Void>> transmitPacket(final TransmitPacketInput input) {
-        final PacketOutInput message = PacketOutConvertor.toPacketOutInput(input, version, deviceContext.getNextXid()
-                .getValue(), datapathId);
+        getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_ENTERED);
+
+        return handleServiceCall(new Function<RequestContext<Void>, ListenableFuture<RpcResult<Void>>>() {
+            @Override
+            public ListenableFuture<RpcResult<Void>> apply(final RequestContext<Void> requestContext) {
+                final Xid xid = requestContext.getXid();
+                final PacketOutInput message = PacketOutConvertor.toPacketOutInput(input, getVersion(), xid.getValue(),
+                        getDatapathId());
+
+                final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
+
+                final SettableFuture<RpcResult<Void>> settableFuture = SettableFuture.create();
+
+                outboundQueue.commitEntry(xid.getValue(), message, new FutureCallback<OfHeader>() {
+                    @Override
+                    public void onSuccess(final OfHeader ofHeader) {
+                        getMessageSpy().spyMessage(message.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
+                        final RpcResultBuilder<Void> rpcResultBuilder = RpcResultBuilder.<Void>success();
+                        settableFuture.set(rpcResultBuilder.build());
+                    }
 
-        BigInteger connectionID = PRIMARY_CONNECTION;
-        final ConnectionCookie connectionCookie = input.getConnectionCookie();
-        if (connectionCookie != null && connectionCookie.getValue() != null) {
-            connectionID = BigInteger.valueOf(connectionCookie.getValue());
-        }
+                    @Override
+                    public void onFailure(final Throwable throwable) {
+                        getMessageSpy().spyMessage(message.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
+                        final RpcResultBuilder<Void> rpcResultBuilder = RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
+                        settableFuture.set(rpcResultBuilder.build());
+                    }
+                });
+                return settableFuture;
+            }
+        });
 
-        return provideConnectionAdapter(connectionID).packetOut(message);
     }
 }