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.HashedWheelTimer;
17 import io.netty.util.Timeout;
18 import io.netty.util.TimerTask;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.TimeUnit;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
31 * org.opendaylight.openflowplugin.impl.device
33 * Barrier message self restarting builder.
35 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
37 * Created: Apr 3, 2015
39 public class BarrierTaskBuilder {
41 private static final Logger LOG = LoggerFactory.getLogger(BarrierTaskBuilder.class);
43 private final HashedWheelTimer hashedWheelTimer;
44 private final DeviceContext deviceCtx;
46 public BarrierTaskBuilder (final DeviceContext deviceCtx, final HashedWheelTimer hashedWheelTimer) {
47 this.hashedWheelTimer = Preconditions.checkNotNull(hashedWheelTimer);
48 this.deviceCtx = Preconditions.checkNotNull(deviceCtx);
51 public void buildAndFireBarrierTask() {
52 hashedWheelTimer.newTimeout(makeTimerTask(), 500, TimeUnit.MILLISECONDS);
55 private TimerTask makeTimerTask() {
56 return new TimerTask() {
58 public void run(final Timeout timeout) throws Exception {
59 final Future<RpcResult<BarrierOutput>> future = deviceCtx.getPrimaryConnectionContext()
60 .getConnectionAdapter().barrier(makeBarier());
61 final ListenableFuture<RpcResult<BarrierOutput>> lsFuture = JdkFutureAdapters.listenInPoolThread(future);
62 Futures.addCallback(lsFuture, makeCallBack());
67 private FutureCallback<RpcResult<BarrierOutput>> makeCallBack() {
68 return new FutureCallback<RpcResult<BarrierOutput>>() {
70 public void onSuccess(final RpcResult<BarrierOutput> result) {
71 BarrierProcessor.processOutstandingRequests(result.getResult().getXid(), deviceCtx);
72 buildAndFireBarrierTask();
75 public void onFailure(final Throwable t) {
76 LOG.info("Barrier has failed {} ", t.getMessage());
77 LOG.trace("Barrier has failed", t);
82 private BarrierInput makeBarier() {
83 final BarrierInputBuilder biBuilder = new BarrierInputBuilder();
84 biBuilder.setVersion(deviceCtx.getDeviceState().getVersion());
85 biBuilder.setXid(deviceCtx.getNextXid().getValue());
86 return biBuilder.build();