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.RpcError;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
31 * org.opendaylight.openflowplugin.impl.device
32 * Barrier message self restarting builder.
34 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
35 * Created: Apr 3, 2015
37 public class BarrierTaskBuilder {
39 private static final Logger LOG = LoggerFactory.getLogger(BarrierTaskBuilder.class);
40 public static final long DELAY = 500L;
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(new BarrierTask(deviceCtx), DELAY, TimeUnit.MILLISECONDS);
51 deviceCtx.setCurrentBarrierTimeout(timeout);
55 private final class BarrierTask implements TimerTask {
57 private final DeviceContext deviceCtx;
59 public BarrierTask(final DeviceContext deviceCtx) {
60 this.deviceCtx = deviceCtx;
64 * @return OF-message, ready to send
66 private BarrierInput makeBarrier() {
67 final BarrierInputBuilder biBuilder = new BarrierInputBuilder();
68 biBuilder.setVersion(deviceCtx.getDeviceState().getVersion());
69 biBuilder.setXid(deviceCtx.getNextXid().getValue());
70 return biBuilder.build();
74 public void run(final Timeout timeout) throws Exception {
75 // check outstanding requests first
76 if (deviceCtx.getDeviceState().isValid()) {
77 if (deviceCtx.getNumberOfOutstandingRequests() > 0) {
78 BarrierInput barrierInput = makeBarrier();
79 LOG.trace("sending out barrier [{}]", barrierInput.getXid());
80 synchronized (deviceCtx) {
81 final Future<RpcResult<BarrierOutput>> future = deviceCtx.getPrimaryConnectionContext()
82 .getConnectionAdapter().barrier(barrierInput);
83 final ListenableFuture<RpcResult<BarrierOutput>> lsFuture = JdkFutureAdapters.listenInPoolThread(future);
84 Futures.addCallback(lsFuture, makeCallBack());
88 buildAndFireBarrierTask();
91 LOG.trace("DeviceContext is not valid, will not create next barrier task.");
95 private FutureCallback<RpcResult<BarrierOutput>> makeCallBack() {
96 return new FutureCallback<RpcResult<BarrierOutput>>() {
98 public void onSuccess(final RpcResult<BarrierOutput> result) {
99 synchronized (deviceCtx) {
100 if (!result.isSuccessful()) {
101 for (RpcError rpcError : result.getErrors()) {
102 LOG.trace("Barrier response with error {}", rpcError, rpcError.getCause());
104 } else if (null != result.getResult().getXid()) {
105 BarrierProcessor.processOutstandingRequests(result.getResult().getXid(), deviceCtx);
107 buildAndFireBarrierTask();
112 public void onFailure(final Throwable t) {
113 LOG.info("Barrier has failed {} ", t.getMessage());
114 LOG.trace("Barrier has failed", t);