Merge "Removed unused code from md-sal."
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / persist / impl / Util.java
index b17309123c8081ce081b178539d66ea68bfdb116..322a9b753b123c006a4298b3209163b9ea82e9ed 100644 (file)
@@ -8,24 +8,46 @@
 
 package org.opendaylight.controller.netconf.persist.impl;
 
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ThreadFactory;
+import org.opendaylight.controller.netconf.client.NetconfClient;
+import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Set;
 
 public final class Util {
+    private static final Logger logger = LoggerFactory.getLogger(Util.class);
+
 
-    public static ScheduledExecutorService getExecutorServiceWithThreadName(final String threadNamePrefix,
-            int threadCount) {
-        return Executors.newScheduledThreadPool(threadCount, new ThreadFactory() {
+    public static boolean isSubset(NetconfClient netconfClient, Set<String> expectedCaps) {
+        return isSubset(netconfClient.getCapabilities(), expectedCaps);
 
-            private int i = 1;
+    }
+
+    private static boolean isSubset(Set<String> currentCapabilities, Set<String> expectedCaps) {
+        for (String exCap : expectedCaps) {
+            if (currentCapabilities.contains(exCap) == false)
+                return false;
+        }
+        return true;
+    }
 
-            @Override
-            public Thread newThread(Runnable r) {
-                Thread thread = new Thread(r);
-                thread.setName(threadNamePrefix + ":" + i++);
-                return thread;
+    public static void closeClientAndDispatcher(NetconfClient client) {
+        NetconfClientDispatcher dispatcher = client.getNetconfClientDispatcher();
+        Exception fromClient = null;
+        try {
+            client.close();
+        } catch (Exception e) {
+            fromClient = e;
+        } finally {
+            try {
+                dispatcher.close();
+            } catch (Exception e) {
+                if (fromClient != null) {
+                    e.addSuppressed(fromClient);
+                }
+                throw new RuntimeException("Error closing temporary client ", e);
             }
-        });
+        }
     }
 }