Returning of future from request context - PacketProcessingService.
[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.FutureCallback;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.Future;
14 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PacketOutConvertor;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
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.RpcError;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28
29 public class PacketProcessingServiceImpl extends CommonService implements PacketProcessingService {
30
31     public PacketProcessingServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
32         super(requestContextStack, deviceContext);
33     }
34
35     @Override
36     public Future<RpcResult<Void>> transmitPacket(final TransmitPacketInput input) {
37         getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_ENTERED);
38
39         return handleServiceCall(new Function<RequestContext<Void>, ListenableFuture<RpcResult<Void>>>() {
40             @Override
41             public ListenableFuture<RpcResult<Void>> apply(final RequestContext<Void> requestContext) {
42                 final Xid xid = requestContext.getXid();
43                 final PacketOutInput message = PacketOutConvertor.toPacketOutInput(input, getVersion(), xid.getValue(),
44                         getDatapathId());
45
46                 final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
47
48                 outboundQueue.commitEntry(xid.getValue(), message, new FutureCallback<OfHeader>() {
49
50                     RpcResultBuilder<Void> rpcResultBuilder;
51                     @Override
52                     public void onSuccess(final OfHeader ofHeader) {
53                         rpcResultBuilder = RpcResultBuilder.<Void>success();
54                         requestContext.setResult(rpcResultBuilder.build());
55                         RequestContextUtil.closeRequstContext(requestContext);
56
57                         getMessageSpy().spyMessage(message.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
58                     }
59
60                     @Override
61                     public void onFailure(final Throwable throwable) {
62                         rpcResultBuilder = RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
63                         requestContext.setResult(rpcResultBuilder.build());
64                         RequestContextUtil.closeRequstContext(requestContext);
65
66                         getMessageSpy().spyMessage(message.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
67                     }
68                 });
69                 return requestContext.getFuture();
70             }
71         });
72
73     }
74 }