Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / hosttracker / implementation / src / main / java / org / opendaylight / controller / hosttracker / internal / HostTrackerCallable.java
1 /*
2  * Copyright (c) 2013 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 /*
10  * Provides a mechanism for applications to do an inline Host Discovery as opposed
11  * to a delayed discovery
12  */
13 package org.opendaylight.controller.hosttracker.internal;
14
15 /**
16  * This Class provides methods to discover Host through a blocking call
17  * mechanism. Applications can make use of these methods if they don't
18  * find a host in HostTracker's database and want to discover the host
19  * in the same thread without being called by a callback function.
20  */
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.CountDownLatch;
23
24 import org.opendaylight.controller.hosttracker.IHostId;
25 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
26
27 /**
28  *
29  *
30  */
31 public class HostTrackerCallable implements Callable<HostNodeConnector> {
32
33     //host id which could be ip or a combination of ip + mac based on the scheme chosen.
34     IHostId trackedHost;
35     HostTracker hostTracker;
36     protected CountDownLatch latch;
37
38     public HostTrackerCallable(HostTracker tracker, IHostId inet) {
39         trackedHost = inet;
40         hostTracker = tracker;
41         latch = new CountDownLatch(1);
42     }
43
44     @Override
45     public HostNodeConnector call() throws Exception {
46         HostNodeConnector h = hostTracker.hostFind(trackedHost);
47         if (h != null)
48             return h;
49         hostTracker.setCallableOnPendingARP(trackedHost, this);
50         Thread.sleep(2000); // wait 2sec to see if the host responds
51         return hostTracker.hostQuery(trackedHost);
52     }
53
54     public void wakeup() {
55         this.latch.countDown();
56     }
57 }