Simplify CommonService interface
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / FlowCapableTransactionServiceImpl.java
index 4fc6821f182b04cef874d50bb3a78f9bb980ff51..bbba541d3d630be96aa732f918c7f36fce8a9b4b 100644 (file)
  */
 package org.opendaylight.openflowplugin.impl.services;
 
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.JdkFutureAdapters;
-import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.SettableFuture;
 import java.util.concurrent.Future;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
-import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
-import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
-import org.opendaylight.openflowplugin.impl.callback.SuccessCallback;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
-import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
-import org.slf4j.Logger;
-
-public class FlowCapableTransactionServiceImpl extends CommonService implements FlowCapableTransactionService {
-
-    private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(FlowCapableTransactionServiceImpl.class);
 
+public class FlowCapableTransactionServiceImpl extends AbstractVoidService<SendBarrierInput> implements FlowCapableTransactionService {
     public FlowCapableTransactionServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
         super(requestContextStack, deviceContext);
     }
 
     @Override
-    public Future<RpcResult<Void>> sendBarrier(SendBarrierInput input) {
-        final RequestContext<Void> requestContext = getRequestContextStack().createRequestContext();
-        final SettableFuture<RpcResult<Void>> sendBarrierOutput = getRequestContextStack()
-                .storeOrFail(requestContext);
-        if (!sendBarrierOutput.isDone()) {
-            final DeviceContext deviceContext = getDeviceContext();
-            final Long reservedXid = deviceContext.getReservedXid();
-            if (null == reservedXid){
-                RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
-                return sendBarrierOutput;
-            }
-            final Xid xid = new Xid(reservedXid);
-            requestContext.setXid(xid);
-
-            final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
-            barrierInputOFJavaBuilder.setVersion(getVersion());
-            barrierInputOFJavaBuilder.setXid(xid.getValue());
-
-            LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
-            deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
-
-            final BarrierInput barrierInputOFJava = barrierInputOFJavaBuilder.build();
-
-            final Future<RpcResult<BarrierOutput>> barrierOutputOFJava = getPrimaryConnectionAdapter()
-                    .barrier(barrierInputOFJava);
-            LOG.debug("Barrier with xid {} was sent from controller.", xid);
-
-            ListenableFuture<RpcResult<BarrierOutput>> listenableBarrierOutputOFJava = JdkFutureAdapters
-                    .listenInPoolThread(barrierOutputOFJava);
-
-            // callback on OF JAVA future
-            SuccessCallback<BarrierOutput, Void> successCallback = new SuccessCallback<BarrierOutput, Void>(
-                    deviceContext, requestContext, listenableBarrierOutputOFJava) {
-
-                @Override
-                public RpcResult<Void> transform(RpcResult<BarrierOutput> rpcResult) {
-                    //no transformation, because output for request context is Void
-                    LOG.debug("Barrier reply with xid {} was obtained by controller.", rpcResult.getResult().getXid());
-                    return RpcResultBuilder.<Void>success().build();
-                }
-            };
-            Futures.addCallback(listenableBarrierOutputOFJava, successCallback);
-        } else {
-            getMessageSpy().spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
-        }
-
-        //callback on request context future
-        Futures.addCallback(sendBarrierOutput, new FutureCallback<RpcResult<Void>>() {
-
-            @Override
-            public void onSuccess(RpcResult<Void> result) {
-            }
-
-            @Override
-            public void onFailure(Throwable t) {
-                if (sendBarrierOutput.isCancelled()) {
-                    requestContext.getFuture().set(
-                            RpcResultBuilder.<Void>failed()
-                                    .withError(ErrorType.APPLICATION, "Barrier response wasn't obtained until barrier.")
-                                    .build());
-                    LOG.debug("Barrier reply with xid {} wasn't obtained by controller.", requestContext.getXid());
-
-                }
-            }
-        });
-
-        return sendBarrierOutput;
-
+    public Future<RpcResult<Void>> sendBarrier(final SendBarrierInput input) {
+        return handleServiceCall(input);
     }
 
+    @Override
+    protected OfHeader buildRequest(final Xid xid, final SendBarrierInput input) {
+        final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
+        barrierInputOFJavaBuilder.setVersion(getVersion());
+        barrierInputOFJavaBuilder.setXid(xid.getValue());
+        return barrierInputOFJavaBuilder.build();
+    }
 }