Returning of future from request context - FlowCapableTransactionService.
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / FlowCapableTransactionServiceImpl.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.util.concurrent.FutureCallback;
11 import java.util.concurrent.Future;
12 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
13 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
16 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
23 import org.opendaylight.yangtools.yang.common.RpcError;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class FlowCapableTransactionServiceImpl extends CommonService implements FlowCapableTransactionService {
30
31     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTransactionServiceImpl.class);
32
33     public FlowCapableTransactionServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
34         super(requestContextStack, deviceContext);
35     }
36
37     @Override
38     public Future<RpcResult<Void>> sendBarrier(final SendBarrierInput input) {
39         final RequestContext<Void> requestContext = getRequestContextStack().createRequestContext();
40         if (requestContext == null) {
41             getMessageSpy().spyMessage(null, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
42             return failedFuture();
43         }
44
45
46         final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
47         final Xid xid = requestContext.getXid();
48         barrierInputOFJavaBuilder.setVersion(getVersion());
49         barrierInputOFJavaBuilder.setXid(xid.getValue());
50
51         LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
52
53         final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
54         final BarrierInput barrierInput = barrierInputOFJavaBuilder.build();
55         outboundQueue.commitEntry(xid.getValue(), barrierInput, new FutureCallback<OfHeader>() {
56
57             RpcResultBuilder<Void> rpcResultBuilder;
58             @Override
59             public void onSuccess(final OfHeader ofHeader) {
60                 rpcResultBuilder = RpcResultBuilder.<Void>success();
61                 requestContext.setResult(rpcResultBuilder.build());
62                 RequestContextUtil.closeRequstContext(requestContext);
63
64                 getMessageSpy().spyMessage(barrierInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
65
66             }
67
68             @Override
69             public void onFailure(final Throwable throwable) {
70                 rpcResultBuilder = RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
71                 requestContext.setResult(rpcResultBuilder.build());
72                 RequestContextUtil.closeRequstContext(requestContext);
73
74                 getMessageSpy().spyMessage(barrierInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
75             }
76         });
77         return requestContext.getFuture();
78     }
79 }