Checkstyle enforcer
[controller.git] / opendaylight / hosttracker / implementation / src / main / java / org / opendaylight / controller / hosttracker / internal / HostTrackerCallable.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 /*
11  * Provides a mechanism for applications to do an inline Host Discovery as opposed
12  * to a delayed discovery
13  */
14 package org.opendaylight.controller.hosttracker.internal;
15
16 /**
17  * This Class provides methods to discover Host through a blocking call
18  * mechanism. Applications can make use of these methods if they don't
19  * find a host in HostTracker's database and want to discover the host
20  * in the same thread without being called by a callback function.
21  */
22 import java.net.InetAddress;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.CountDownLatch;
25
26 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
27
28 public class HostTrackerCallable implements Callable<HostNodeConnector> {
29
30     InetAddress trackedHost;
31     HostTracker hostTracker;
32     protected CountDownLatch latch;
33
34     public HostTrackerCallable(HostTracker tracker, InetAddress inet) {
35         trackedHost = inet;
36         hostTracker = tracker;
37         latch = new CountDownLatch(1);
38     }
39
40     @Override
41     public HostNodeConnector call() throws Exception {
42         HostNodeConnector h = hostTracker.hostFind(trackedHost);
43         if (h != null)
44             return h;
45         hostTracker.setCallableOnPendingARP(trackedHost, this);
46         latch.await();
47         return hostTracker.hostQuery(trackedHost);
48     }
49
50     public void wakeup() {
51         this.latch.countDown();
52     }
53 }