sal-table:update-table rpc (service) implementation
[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 org.slf4j.Logger;
11
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;
32
33 public class FlowCapableTransactionServiceImpl extends CommonService implements FlowCapableTransactionService {
34
35     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(FlowCapableTransactionServiceImpl.class);
36
37     public FlowCapableTransactionServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
38         super(requestContextStack, deviceContext);
39     }
40
41     @Override
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);
49
50           final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
51           barrierInputOFJavaBuilder.setVersion(version);
52           barrierInputOFJavaBuilder.setXid(xid.getValue());
53
54           LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
55           deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
56
57           final BarrierInput barrierInputOFJava = barrierInputOFJavaBuilder.build();
58
59           final Future<RpcResult<BarrierOutput>> barrierOutputOFJava = provideConnectionAdapter(PRIMARY_CONNECTION)
60                   .barrier(barrierInputOFJava);
61           LOG.debug("Barrier with xid {} was sent from controller.", xid);
62
63           ListenableFuture<RpcResult<BarrierOutput>> listenableBarrierOutputOFJava = JdkFutureAdapters
64                   .listenInPoolThread(barrierOutputOFJava);
65
66           // callback on OF JAVA future
67           SuccessCallback<BarrierOutput, Void> successCallback = new SuccessCallback<BarrierOutput, Void>(
68                   deviceContext, requestContext, listenableBarrierOutputOFJava) {
69
70               @Override
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();
75               }
76           };
77           Futures.addCallback(listenableBarrierOutputOFJava, successCallback);
78       } else {
79           messageSpy.spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_FAILURE);
80       }
81
82       //callback on request context future
83       Futures.addCallback(sendBarrierOutput, new FutureCallback<RpcResult<Void>>() {
84
85           @Override
86           public void onSuccess(RpcResult<Void> result) {
87           }
88
89           @Override
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.")
95                                   .build());
96                   LOG.debug("Barrier reply with xid {} wasn't obtained by controller.",requestContext.getXid());
97
98               }
99           }
100       });
101
102       return sendBarrierOutput;
103
104     }
105
106 }