DataCrate removed and its usage replaced by RequestContext
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / PacketProcessingServiceImpl.java
index 03f1e0c0f03e7ec68ee40a74a55b4d1714b0f105..c176a2a282374278bdb17725b8b2e831f3674314 100644 (file)
@@ -1,36 +1,72 @@
 /**
  * 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 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.openflowplugin.api.openflow.rpc.RpcContext;
+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.PacketProcessingService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 
-/**
- * @author joe
- * 
- */
-// TODO: implement this
 public class PacketProcessingServiceImpl extends CommonService implements PacketProcessingService {
 
+    public PacketProcessingServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
+        super(requestContextStack, deviceContext);
+    }
 
-    /*
-         * (non-Javadoc)
-         *
-         * @see
-         * org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService#transmitPacket
-         * (org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput)
-         */
     @Override
     public Future<RpcResult<Void>> transmitPacket(final TransmitPacketInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        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) {
+                        if (ofHeader instanceof RpcResult) {
+                            RpcResult rpcResult = (RpcResult) ofHeader;
+                            if (!rpcResult.isSuccessful()) {
+                                settableFuture.set(rpcResult);
+                            } else {
+                                settableFuture.cancel(true);
+                            }
+                        }
+                    }
+
+                    @Override
+                    public void onFailure(final Throwable throwable) {
+                        settableFuture.cancel(true);
+                    }
+                });
+                return settableFuture;
+            }
+        });
+
     }
 }