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 org.slf4j.Logger;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
17 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
18 import com.google.common.util.concurrent.FutureCallback;
19 import com.google.common.util.concurrent.Futures;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
21 import com.google.common.util.concurrent.SettableFuture;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23 import org.opendaylight.openflowplugin.impl.callback.SuccessCallback;
24 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
26 import com.google.common.util.concurrent.JdkFutureAdapters;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
30 import java.util.concurrent.Future;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
33 public class FlowCapableTransactionServiceImpl extends CommonService implements FlowCapableTransactionService {
35 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(FlowCapableTransactionServiceImpl.class);
37 public FlowCapableTransactionServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
38 super(requestContextStack, deviceContext);
42 public Future<RpcResult<Void>> sendBarrier(SendBarrierInput input) {
43 final RequestContext<Void> requestContext = requestContextStack.createRequestContext();
44 final SettableFuture<RpcResult<Void>> sendBarrierOutput = requestContextStack
45 .storeOrFail(requestContext);
46 if (!sendBarrierOutput.isDone()) {
47 final Xid xid = deviceContext.getNextXid();
48 requestContext.setXid(xid);
50 final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
51 barrierInputOFJavaBuilder.setVersion(version);
52 barrierInputOFJavaBuilder.setXid(xid.getValue());
54 LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
55 deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
57 final BarrierInput barrierInputOFJava = barrierInputOFJavaBuilder.build();
59 final Future<RpcResult<BarrierOutput>> barrierOutputOFJava = provideConnectionAdapter(PRIMARY_CONNECTION)
60 .barrier(barrierInputOFJava);
61 LOG.debug("Barrier with xid {} was sent from controller.", xid);
63 ListenableFuture<RpcResult<BarrierOutput>> listenableBarrierOutputOFJava = JdkFutureAdapters
64 .listenInPoolThread(barrierOutputOFJava);
66 // callback on OF JAVA future
67 SuccessCallback<BarrierOutput, Void> successCallback = new SuccessCallback<BarrierOutput, Void>(
68 deviceContext, requestContext, listenableBarrierOutputOFJava) {
71 public RpcResult<Void> transform(RpcResult<BarrierOutput> rpcResult) {
72 //no transformation, because output for request context is Void
73 LOG.debug("Barrier reply with xid {} was obtained by controller.",rpcResult.getResult().getXid());
74 return RpcResultBuilder.<Void>success().build();
77 Futures.addCallback(listenableBarrierOutputOFJava, successCallback);
79 messageSpy.spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_FAILURE);
82 //callback on request context future
83 Futures.addCallback(sendBarrierOutput, new FutureCallback<RpcResult<Void>>() {
86 public void onSuccess(RpcResult<Void> result) {
90 public void onFailure(Throwable t) {
91 if (sendBarrierOutput.isCancelled()) {
92 requestContext.getFuture().set(
93 RpcResultBuilder.<Void> failed()
94 .withError(ErrorType.APPLICATION, "Barrier response wasn't obtained until barrier.")
96 LOG.debug("Barrier reply with xid {} wasn't obtained by controller.",requestContext.getXid());
102 return sendBarrierOutput;