4fc6821f182b04cef874d50bb3a78f9bb980ff51
[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.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;
31
32 public class FlowCapableTransactionServiceImpl extends CommonService implements FlowCapableTransactionService {
33
34     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(FlowCapableTransactionServiceImpl.class);
35
36     public FlowCapableTransactionServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
37         super(requestContextStack, deviceContext);
38     }
39
40     @Override
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;
51             }
52             final Xid xid = new Xid(reservedXid);
53             requestContext.setXid(xid);
54
55             final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
56             barrierInputOFJavaBuilder.setVersion(getVersion());
57             barrierInputOFJavaBuilder.setXid(xid.getValue());
58
59             LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
60             deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
61
62             final BarrierInput barrierInputOFJava = barrierInputOFJavaBuilder.build();
63
64             final Future<RpcResult<BarrierOutput>> barrierOutputOFJava = getPrimaryConnectionAdapter()
65                     .barrier(barrierInputOFJava);
66             LOG.debug("Barrier with xid {} was sent from controller.", xid);
67
68             ListenableFuture<RpcResult<BarrierOutput>> listenableBarrierOutputOFJava = JdkFutureAdapters
69                     .listenInPoolThread(barrierOutputOFJava);
70
71             // callback on OF JAVA future
72             SuccessCallback<BarrierOutput, Void> successCallback = new SuccessCallback<BarrierOutput, Void>(
73                     deviceContext, requestContext, listenableBarrierOutputOFJava) {
74
75                 @Override
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();
80                 }
81             };
82             Futures.addCallback(listenableBarrierOutputOFJava, successCallback);
83         } else {
84             getMessageSpy().spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
85         }
86
87         //callback on request context future
88         Futures.addCallback(sendBarrierOutput, new FutureCallback<RpcResult<Void>>() {
89
90             @Override
91             public void onSuccess(RpcResult<Void> result) {
92             }
93
94             @Override
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.")
100                                     .build());
101                     LOG.debug("Barrier reply with xid {} wasn't obtained by controller.", requestContext.getXid());
102
103                 }
104             }
105         });
106
107         return sendBarrierOutput;
108
109     }
110
111 }