[DO NOT MERGE]
[controller.git] / opendaylight / ping / plugin / src / main / java / org / opendaylight / controller / ping / plugin / internal / PingImpl.java
index 0fe73000f9f8bfba261c6e2462a2eb2fb473c29d..3305503c30e65adb2da5c3e4a8c4af4d710f8c83 100644 (file)
@@ -1,8 +1,10 @@
 package org.opendaylight.controller.ping.plugin.internal;
 
-import java.io.IOException;
 import java.net.InetAddress;
 import java.util.Collections;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 
 import org.opendaylight.controller.sal.common.util.Rpcs;
@@ -14,43 +16,30 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.ping.rev130911.SendEchoOutp
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 
-import com.google.common.util.concurrent.Futures;
-
 public class PingImpl implements PingService {
-
-    private EchoResult pingHost(InetAddress destination) throws IOException {
-        if (destination.isReachable(5000)) {
-            return EchoResult.Reachable;
-        } else {
-            return EchoResult.Unreachable;
-        }
+    private final ExecutorService pool = Executors.newFixedThreadPool(2);
+
+    private Future<RpcResult<SendEchoOutput>> startPingHost(final SendEchoInput destination) {
+        return pool.submit(new Callable<RpcResult<SendEchoOutput>>() {
+            @Override
+            public RpcResult<SendEchoOutput> call() throws Exception {
+                SendEchoOutputBuilder ob = new SendEchoOutputBuilder();
+                try {
+                    InetAddress dst = InetAddress.getByName(destination.getDestination().getValue());
+                    /* Build the result and return it. */
+                    ob.setEchoResult(dst.isReachable(5000) ? EchoResult.Reachable : EchoResult.Unreachable);
+                } catch (Exception e) {
+                    /* Return error result. */
+                    ob.setEchoResult(EchoResult.Error);
+                }
+                return Rpcs.<SendEchoOutput>getRpcResult(true, ob.build(), Collections.<RpcError>emptySet());
+            }
+        });
     }
 
     @Override
     public Future<RpcResult<SendEchoOutput>> sendEcho(SendEchoInput destination) {
-        try {
-            InetAddress dst = InetAddress.getByName(destination
-                    .getDestination().getValue());
-            EchoResult result = this.pingHost(dst);
-
-            /* Build the result and return it. */
-            SendEchoOutputBuilder ob = new SendEchoOutputBuilder();
-            ob.setEchoResult(result);
-            RpcResult<SendEchoOutput> rpcResult =
-                    Rpcs.<SendEchoOutput> getRpcResult(true, ob.build(),
-                            Collections.<RpcError> emptySet());
-
-            return Futures.immediateFuture(rpcResult);
-        } catch (Exception e) {
-
-            /* Return error result. */
-            SendEchoOutputBuilder ob = new SendEchoOutputBuilder();
-            ob.setEchoResult(EchoResult.Error);
-            RpcResult<SendEchoOutput> rpcResult =
-                    Rpcs.<SendEchoOutput> getRpcResult(true, ob.build(),
-                            Collections.<RpcError> emptySet());
-            return Futures.immediateFuture(rpcResult);
-        }
+        return this.startPingHost(destination);
     }
 
 }