856c3ad597495a309935fa21fc0412ece3445a69
[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.device.Xid;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
19 import org.opendaylight.openflowplugin.impl.callback.SuccessCallback;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
27 import org.slf4j.Logger;
28
29 public class FlowCapableTransactionServiceImpl extends CommonService implements FlowCapableTransactionService {
30
31     private static final Logger LOG = org.slf4j.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         final DeviceContext deviceContext = getDeviceContext();
46         final Long reservedXid = deviceContext.getReservedXid();
47         if (null == reservedXid) {
48             return RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
49         }
50         final Xid xid = new Xid(reservedXid);
51         requestContext.setXid(xid);
52
53         final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
54         barrierInputOFJavaBuilder.setVersion(getVersion());
55         barrierInputOFJavaBuilder.setXid(xid.getValue());
56
57         LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
58         deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
59
60         final BarrierInput barrierInputOFJava = barrierInputOFJavaBuilder.build();
61
62         // FIXME: should be submitted through OutboundQueue
63         final Future<RpcResult<BarrierOutput>> barrierOutputOFJava = getPrimaryConnectionAdapter()
64                 .barrier(barrierInputOFJava);
65         LOG.debug("Barrier with xid {} was sent from controller.", xid);
66
67         ListenableFuture<RpcResult<BarrierOutput>> listenableBarrierOutputOFJava = JdkFutureAdapters
68                 .listenInPoolThread(barrierOutputOFJava);
69
70         // callback on OF JAVA future
71         SuccessCallback<BarrierOutput, Void> successCallback = new SuccessCallback<BarrierOutput, Void>(
72                 deviceContext, requestContext, listenableBarrierOutputOFJava) {
73
74             @Override
75             public RpcResult<Void> transform(final RpcResult<BarrierOutput> rpcResult) {
76                 //no transformation, because output for request context is Void
77                 LOG.debug("Barrier reply with xid {} was obtained by controller.", rpcResult.getResult().getXid());
78                 return RpcResultBuilder.<Void>success().build();
79             }
80         };
81         Futures.addCallback(listenableBarrierOutputOFJava, successCallback);
82
83         return requestContext.getFuture();
84     }
85 }