Merge "Do not override Karaf version"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / PacketProcessingServiceImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.services;
9
10 import com.google.common.base.Function;
11 import com.google.common.util.concurrent.AsyncFunction;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.math.BigInteger;
16 import java.util.concurrent.Future;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PacketOutConvertor;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.ConnectionCookie;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26
27 public class PacketProcessingServiceImpl extends CommonService implements PacketProcessingService {
28
29     public PacketProcessingServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
30         super(requestContextStack, deviceContext);
31     }
32
33     @Override
34     public Future<RpcResult<Void>> transmitPacket(final TransmitPacketInput input) {
35
36         return handleServiceCall(PRIMARY_CONNECTION, new Function<DataCrate<Void>, ListenableFuture<RpcResult<Void>>>() {
37
38             @Override
39             public ListenableFuture<RpcResult<Void>> apply(DataCrate<Void> data) {
40                 final Xid xid = data.getRequestContext().getXid();
41                 final PacketOutInput message = PacketOutConvertor.toPacketOutInput(input, version, xid.getValue(),
42                         datapathId);
43
44                 BigInteger connectionID = PRIMARY_CONNECTION;
45                 final ConnectionCookie connectionCookie = input.getConnectionCookie();
46                 if (connectionCookie != null && connectionCookie.getValue() != null) {
47                     connectionID = BigInteger.valueOf(connectionCookie.getValue());
48                 }
49
50                 ListenableFuture<RpcResult<Void>> rpcResultListenableFuture = JdkFutureAdapters.listenInPoolThread(provideConnectionAdapter(connectionID).packetOut(message));
51                 return Futures.transform(rpcResultListenableFuture, new AsyncFunction<RpcResult<Void>, RpcResult<Void>>() {
52                     @Override
53                     public ListenableFuture<RpcResult<Void>> apply(RpcResult<Void> rpcResult) throws Exception {
54                         if (! rpcResult.isSuccessful()) {
55                             return Futures.immediateFuture(rpcResult);
56                         } else {
57                             return Futures.immediateCancelledFuture();
58                         }
59                     }
60                 });
61             }
62         });
63
64     }
65 }