2 * Copyright (c) 2013 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
10 * Class which will monitor the completion of a FlowEntryDistributionOrder it
11 * implements a Future interface so it can be inspected by who is waiting for
14 package org.opendaylight.controller.forwardingrulesmanager.internal;
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
22 import org.opendaylight.controller.forwardingrulesmanager.implementation.data.FlowEntryDistributionOrder;
23 import org.opendaylight.controller.sal.utils.Status;
24 import org.opendaylight.controller.sal.utils.StatusCode;
27 * Class which will monitor the completion of a FlowEntryDistributionOrder it
28 * implements a Future interface so it can be inspected by who is waiting for
31 final class FlowEntryDistributionOrderFutureTask implements Future<Status> {
32 private final FlowEntryDistributionOrder order;
33 private boolean amICancelled;
34 private CountDownLatch waitingLatch;
35 private Status retStatus;
39 * for which we are monitoring the execution
41 FlowEntryDistributionOrderFutureTask(FlowEntryDistributionOrder order) {
42 // Order being monitored
44 this.amICancelled = false;
45 // We need to wait for one completion to happen
46 this.waitingLatch = new CountDownLatch(1);
47 // No return status yet!
48 this.retStatus = new Status(StatusCode.UNDEFINED);
52 public boolean cancel(boolean mayInterruptIfRunning) {
57 public Status get() throws InterruptedException, ExecutionException {
58 // If i'm done lets return the status as many times as caller wants
59 if (this.waitingLatch.getCount() == 0L) {
63 // Wait till someone signal that we are done
64 this.waitingLatch.await();
66 // Return the known status
71 public Status get(long timeout, TimeUnit unit) throws InterruptedException,
72 ExecutionException, TimeoutException {
73 // If i'm done lets return the status as many times as caller wants
74 if (this.waitingLatch.getCount() == 0L) {
78 // Wait till someone signal that we are done
79 this.waitingLatch.await(timeout, unit);
81 // Return the known status, could also be null if didn't return
86 public boolean isCancelled() {
87 return this.amICancelled;
91 public boolean isDone() {
92 return (this.waitingLatch.getCount() == 0L);
96 * Used by the thread that gets back the status for the order so can unblock
97 * an eventual caller waiting on the result to comes back
102 void gotStatus(FlowEntryDistributionOrder order, Status retStatus) {
103 if (order != this.order) {
104 // Weird we got a call for an order we didn't make
107 this.retStatus = retStatus;
108 // Now we are not waiting any longer
109 this.waitingLatch.countDown();