2b0096035d53f13d8bc5eb22a1d21f7e6887844a
[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.SettableFuture;
12 import java.util.concurrent.Future;
13 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
25 import org.opendaylight.yangtools.yang.common.RpcError;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28 import org.slf4j.Logger;
29
30 public class FlowCapableTransactionServiceImpl extends CommonService implements FlowCapableTransactionService {
31
32     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(FlowCapableTransactionServiceImpl.class);
33
34     public FlowCapableTransactionServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
35         super(requestContextStack, deviceContext);
36     }
37
38     @Override
39     public Future<RpcResult<Void>> sendBarrier(final SendBarrierInput input) {
40         final RequestContext<Void> requestContext = getRequestContextStack().createRequestContext();
41         if (requestContext == null) {
42             getMessageSpy().spyMessage(null, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
43             return failedFuture();
44         }
45
46         final DeviceContext deviceContext = getDeviceContext();
47
48         final BarrierInputBuilder barrierInputOFJavaBuilder = new BarrierInputBuilder();
49         final Xid xid = requestContext.getXid();
50         barrierInputOFJavaBuilder.setVersion(getVersion());
51         barrierInputOFJavaBuilder.setXid(xid.getValue());
52
53         LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
54         deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
55
56         final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
57         final SettableFuture<RpcResult<Void>> settableFuture = SettableFuture.create();
58         final BarrierInput barrierInput = barrierInputOFJavaBuilder.build();
59         outboundQueue.commitEntry(xid.getValue(), barrierInput, new FutureCallback<OfHeader>() {
60             @Override
61             public void onSuccess(final OfHeader ofHeader) {
62                 RequestContextUtil.closeRequstContext(requestContext);
63                 getDeviceContext().unhookRequestCtx(requestContext.getXid());
64                 getMessageSpy().spyMessage(barrierInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
65
66                 settableFuture.set(RpcResultBuilder.<Void>success().build());
67             }
68
69             @Override
70             public void onFailure(final Throwable throwable) {
71                 RpcResultBuilder rpcResultBuilder = RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
72                 RequestContextUtil.closeRequstContext(requestContext);
73                 getDeviceContext().unhookRequestCtx(requestContext.getXid());
74                 getMessageSpy().spyMessage(barrierInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
75                 settableFuture.set(rpcResultBuilder.build());
76             }
77         });
78         return settableFuture;
79     }
80 }