afb9abb23e051bd4037d0b946cbe8ff57dd78e22
[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.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;
28
29 /**
30  * openflowplugin-impl
31  * org.opendaylight.openflowplugin.impl.device
32  *
33  * Barrier message self restarting builder.
34  *
35  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
36  *
37  * Created: Apr 3, 2015
38  */
39 public class BarrierTaskBuilder {
40
41     private static final Logger LOG = LoggerFactory.getLogger(BarrierTaskBuilder.class);
42
43     private final HashedWheelTimer hashedWheelTimer;
44     private final DeviceContext deviceCtx;
45
46     public BarrierTaskBuilder (final DeviceContext deviceCtx, final HashedWheelTimer hashedWheelTimer) {
47         this.hashedWheelTimer = Preconditions.checkNotNull(hashedWheelTimer);
48         this.deviceCtx = Preconditions.checkNotNull(deviceCtx);
49     }
50
51     public void buildAndFireBarrierTask() {
52         hashedWheelTimer.newTimeout(makeTimerTask(), 500, TimeUnit.MILLISECONDS);
53     }
54
55     private TimerTask makeTimerTask() {
56         return new TimerTask() {
57             @Override
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());
63             }
64         };
65     }
66
67     private FutureCallback<RpcResult<BarrierOutput>> makeCallBack() {
68         return new FutureCallback<RpcResult<BarrierOutput>>() {
69             @Override
70             public void onSuccess(final RpcResult<BarrierOutput> result) {
71                 BarrierProcessor.processOutstandingRequests(result.getResult().getXid(), deviceCtx);
72                 buildAndFireBarrierTask();
73             }
74             @Override
75             public void onFailure(final Throwable t) {
76                 LOG.info("Barrier has failed {} ", t.getMessage());
77                 LOG.trace("Barrier has failed", t);
78             }
79         };
80     }
81
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();
87     }
88 }