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.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.JdkFutureAdapters;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.concurrent.Future;
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.impl.callback.SuccessCallback;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
27 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
30 import org.slf4j.Logger;
32 public class FlowCapableTransactionServiceImpl extends CommonService implements FlowCapableTransactionService {
34 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(FlowCapableTransactionServiceImpl.class);
36 public FlowCapableTransactionServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
37 super(requestContextStack, deviceContext);
41 public Future<RpcResult<Void>> sendBarrier(SendBarrierInput input) {
42 final RequestContext<Void> requestContext = getRequestContextStack().createRequestContext();
43 final SettableFuture<RpcResult<Void>> sendBarrierOutput = getRequestContextStack()
44 .storeOrFail(requestContext);
45 if (!sendBarrierOutput.isDone()) {
46 final DeviceContext deviceContext = getDeviceContext();
47 final Long reservedXid = deviceContext.getReservedXid();
48 if (null == reservedXid){
49 RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
50 return sendBarrierOutput;
52 final Xid xid = new Xid(reservedXid);
53 requestContext.setXid(xid);
55 final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
56 barrierInputOFJavaBuilder.setVersion(getVersion());
57 barrierInputOFJavaBuilder.setXid(xid.getValue());
59 LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
60 deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
62 final BarrierInput barrierInputOFJava = barrierInputOFJavaBuilder.build();
64 final Future<RpcResult<BarrierOutput>> barrierOutputOFJava = getPrimaryConnectionAdapter()
65 .barrier(barrierInputOFJava);
66 LOG.debug("Barrier with xid {} was sent from controller.", xid);
68 ListenableFuture<RpcResult<BarrierOutput>> listenableBarrierOutputOFJava = JdkFutureAdapters
69 .listenInPoolThread(barrierOutputOFJava);
71 // callback on OF JAVA future
72 SuccessCallback<BarrierOutput, Void> successCallback = new SuccessCallback<BarrierOutput, Void>(
73 deviceContext, requestContext, listenableBarrierOutputOFJava) {
76 public RpcResult<Void> transform(RpcResult<BarrierOutput> rpcResult) {
77 //no transformation, because output for request context is Void
78 LOG.debug("Barrier reply with xid {} was obtained by controller.", rpcResult.getResult().getXid());
79 return RpcResultBuilder.<Void>success().build();
82 Futures.addCallback(listenableBarrierOutputOFJava, successCallback);
84 getMessageSpy().spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
87 //callback on request context future
88 Futures.addCallback(sendBarrierOutput, new FutureCallback<RpcResult<Void>>() {
91 public void onSuccess(RpcResult<Void> result) {
95 public void onFailure(Throwable t) {
96 if (sendBarrierOutput.isCancelled()) {
97 requestContext.getFuture().set(
98 RpcResultBuilder.<Void>failed()
99 .withError(ErrorType.APPLICATION, "Barrier response wasn't obtained until barrier.")
101 LOG.debug("Barrier reply with xid {} wasn't obtained by controller.", requestContext.getXid());
107 return sendBarrierOutput;