2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.device;
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;
30 * org.opendaylight.openflowplugin.impl.device
32 * Barrier message self restarting builder.
34 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
36 * Created: Apr 3, 2015
38 public class BarrierTaskBuilder {
40 private static final Logger LOG = LoggerFactory.getLogger(BarrierTaskBuilder.class);
42 private final DeviceContext deviceCtx;
44 public BarrierTaskBuilder (final DeviceContext deviceCtx) {
45 this.deviceCtx = Preconditions.checkNotNull(deviceCtx);
46 Preconditions.checkNotNull(deviceCtx.getTimer());
49 public void buildAndFireBarrierTask() {
50 Timeout timeout = deviceCtx.getTimer().newTimeout(makeTimerTask(), 1000L, TimeUnit.MILLISECONDS);
51 deviceCtx.setCurrentBarrierTimeout(timeout);
54 private TimerTask makeTimerTask() {
55 return new TimerTask() {
57 public void run(final Timeout timeout) throws Exception {
58 // check outstanding requests first
59 if (deviceCtx.getNumberOfOutstandingRequests() > 0) {
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());
68 buildAndFireBarrierTask();
74 private FutureCallback<RpcResult<BarrierOutput>> makeCallBack() {
75 return new FutureCallback<RpcResult<BarrierOutput>>() {
77 public void onSuccess(final RpcResult<BarrierOutput> result) {
78 BarrierProcessor.processOutstandingRequests(result.getResult().getXid(), deviceCtx);
79 buildAndFireBarrierTask();
82 public void onFailure(final Throwable t) {
83 LOG.info("Barrier has failed {} ", t.getMessage());
84 LOG.trace("Barrier has failed", t);
90 * @return OF-message, ready to send
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();