2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.services;
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;
29 public class FlowCapableTransactionServiceImpl extends CommonService implements FlowCapableTransactionService {
31 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(FlowCapableTransactionServiceImpl.class);
33 public FlowCapableTransactionServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
34 super(requestContextStack, deviceContext);
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();
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.");
50 final Xid xid = new Xid(reservedXid);
51 requestContext.setXid(xid);
53 final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
54 barrierInputOFJavaBuilder.setVersion(getVersion());
55 barrierInputOFJavaBuilder.setXid(xid.getValue());
57 LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
58 deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
60 final BarrierInput barrierInputOFJava = barrierInputOFJavaBuilder.build();
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);
67 ListenableFuture<RpcResult<BarrierOutput>> listenableBarrierOutputOFJava = JdkFutureAdapters
68 .listenInPoolThread(barrierOutputOFJava);
70 // callback on OF JAVA future
71 SuccessCallback<BarrierOutput, Void> successCallback = new SuccessCallback<BarrierOutput, Void>(
72 deviceContext, requestContext, listenableBarrierOutputOFJava) {
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();
81 Futures.addCallback(listenableBarrierOutputOFJava, successCallback);
83 return requestContext.getFuture();