tune up barrier management
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / BarrierTaskBuilder.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
9 package org.opendaylight.openflowplugin.impl.device;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.JdkFutureAdapters;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import io.netty.util.Timeout;
17 import io.netty.util.TimerTask;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.TimeUnit;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
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.BarrierOutput;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * openflowplugin-impl
30  * org.opendaylight.openflowplugin.impl.device
31  *
32  * Barrier message self restarting builder.
33  *
34  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
35  *
36  * Created: Apr 3, 2015
37  */
38 public class BarrierTaskBuilder {
39
40     private static final Logger LOG = LoggerFactory.getLogger(BarrierTaskBuilder.class);
41
42     private final DeviceContext deviceCtx;
43
44     public BarrierTaskBuilder (final DeviceContext deviceCtx) {
45         this.deviceCtx = Preconditions.checkNotNull(deviceCtx);
46         Preconditions.checkNotNull(deviceCtx.getTimer());
47     }
48
49     public void buildAndFireBarrierTask() {
50         Timeout timeout = deviceCtx.getTimer().newTimeout(makeTimerTask(), 1000L, TimeUnit.MILLISECONDS);
51         deviceCtx.setCurrentBarrierTimeout(timeout);
52     }
53
54     private TimerTask makeTimerTask() {
55         return new TimerTask() {
56             @Override
57             public void run(final Timeout timeout) throws Exception {
58                 // check outstanding requests first
59                 if (! deviceCtx.getRequests().isEmpty()) {
60                     BarrierInput barrierInput = makeBarrier();
61                     LOG.trace("sending out barrier [{}]", barrierInput.getXid());
62                     final Future<RpcResult<BarrierOutput>> future = deviceCtx.getPrimaryConnectionContext()
63                             .getConnectionAdapter().barrier(barrierInput);
64                     final ListenableFuture<RpcResult<BarrierOutput>> lsFuture = JdkFutureAdapters.listenInPoolThread(future);
65                     Futures.addCallback(lsFuture, makeCallBack());
66                 } else {
67                     // if no requests
68                     buildAndFireBarrierTask();
69                 }
70             }
71         };
72     }
73
74     private FutureCallback<RpcResult<BarrierOutput>> makeCallBack() {
75         return new FutureCallback<RpcResult<BarrierOutput>>() {
76             @Override
77             public void onSuccess(final RpcResult<BarrierOutput> result) {
78                 BarrierProcessor.processOutstandingRequests(result.getResult().getXid(), deviceCtx);
79                 buildAndFireBarrierTask();
80             }
81             @Override
82             public void onFailure(final Throwable t) {
83                 LOG.info("Barrier has failed {} ", t.getMessage());
84                 LOG.trace("Barrier has failed", t);
85             }
86         };
87     }
88
89     /**
90      * @return OF-message, ready to send
91      */
92     private BarrierInput makeBarrier() {
93         final BarrierInputBuilder biBuilder = new BarrierInputBuilder();
94         biBuilder.setVersion(deviceCtx.getDeviceState().getVersion());
95         biBuilder.setXid(deviceCtx.getNextXid().getValue());
96         return biBuilder.build();
97     }
98 }