Fix RpcResultBuilder/RpcContext raw references
[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 com.google.common.util.concurrent.SettableFuture;
14 import java.util.concurrent.Future;
15 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PacketOutConvertor;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27
28 public class PacketProcessingServiceImpl extends CommonService implements PacketProcessingService {
29
30     public PacketProcessingServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
31         super(requestContextStack, deviceContext);
32     }
33
34     @Override
35     public Future<RpcResult<Void>> transmitPacket(final TransmitPacketInput input) {
36         getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_ENTERED);
37
38         return handleServiceCall(new Function<RequestContext<Void>, ListenableFuture<RpcResult<Void>>>() {
39             @Override
40             public ListenableFuture<RpcResult<Void>> apply(final RequestContext<Void> requestContext) {
41                 final Xid xid = requestContext.getXid();
42                 final PacketOutInput message = PacketOutConvertor.toPacketOutInput(input, getVersion(), xid.getValue(),
43                         getDatapathId());
44
45                 final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
46
47                 final SettableFuture<RpcResult<Void>> settableFuture = SettableFuture.create();
48
49                 outboundQueue.commitEntry(xid.getValue(), message, new FutureCallback<OfHeader>() {
50                     @Override
51                     public void onSuccess(final OfHeader ofHeader) {
52                         if (ofHeader instanceof RpcResult) {
53                             @SuppressWarnings("unchecked")
54                             RpcResult<Void> rpcResult = (RpcResult<Void>) ofHeader;
55                             if (!rpcResult.isSuccessful()) {
56                                 getMessageSpy().spyMessage(message.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
57                                 settableFuture.set(rpcResult);
58                             } else {
59                                 getMessageSpy().spyMessage(message.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
60                                 settableFuture.cancel(true);
61                             }
62                         }
63                     }
64
65                     @Override
66                     public void onFailure(final Throwable throwable) {
67                         getMessageSpy().spyMessage(message.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
68
69                         settableFuture.cancel(true);
70                     }
71                 });
72                 return settableFuture;
73             }
74         });
75
76     }
77 }