Make sure RequestContext has a constant XID
[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.Futures;
11 import com.google.common.util.concurrent.JdkFutureAdapters;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.Future;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
18 import org.opendaylight.openflowplugin.impl.callback.SuccessCallback;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.slf4j.Logger;
27
28 public class FlowCapableTransactionServiceImpl extends CommonService implements FlowCapableTransactionService {
29
30     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(FlowCapableTransactionServiceImpl.class);
31
32     public FlowCapableTransactionServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
33         super(requestContextStack, deviceContext);
34     }
35
36     @Override
37     public Future<RpcResult<Void>> sendBarrier(final SendBarrierInput input) {
38         final RequestContext<Void> requestContext = getRequestContextStack().createRequestContext();
39         if (requestContext == null) {
40             getMessageSpy().spyMessage(null, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
41             return failedFuture();
42         }
43
44         final DeviceContext deviceContext = getDeviceContext();
45
46         final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
47         barrierInputOFJavaBuilder.setVersion(getVersion());
48         barrierInputOFJavaBuilder.setXid(requestContext.getXid().getValue());
49
50         LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
51         deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
52
53         final BarrierInput barrierInputOFJava = barrierInputOFJavaBuilder.build();
54
55         // FIXME: should be submitted through OutboundQueue
56         final Future<RpcResult<BarrierOutput>> barrierOutputOFJava = getPrimaryConnectionAdapter()
57                 .barrier(barrierInputOFJava);
58         LOG.debug("Barrier with xid {} was sent from controller.", requestContext.getXid());
59
60         ListenableFuture<RpcResult<BarrierOutput>> listenableBarrierOutputOFJava = JdkFutureAdapters
61                 .listenInPoolThread(barrierOutputOFJava);
62
63         // callback on OF JAVA future
64         SuccessCallback<BarrierOutput, Void> successCallback = new SuccessCallback<BarrierOutput, Void>(
65                 deviceContext, requestContext, listenableBarrierOutputOFJava) {
66
67             @Override
68             public RpcResult<Void> transform(final RpcResult<BarrierOutput> rpcResult) {
69                 //no transformation, because output for request context is Void
70                 LOG.debug("Barrier reply with xid {} was obtained by controller.", rpcResult.getResult().getXid());
71                 return RpcResultBuilder.<Void>success().build();
72             }
73         };
74         Futures.addCallback(listenableBarrierOutputOFJava, successCallback);
75
76         return requestContext.getFuture();
77     }
78 }