utils: migrate to the mdsal DataBroker
[ovsdb.git] / utils / ovsdb-it-utils / src / main / java / org / opendaylight / ovsdb / utils / ovsdb / it / utils / NodeInfo.java
index 5f81aa3e24adb6f737d11292c635d0c12f730062..b5d69c3cdaa4ca33c31d62a9b3414afa5c455026 100644 (file)
@@ -14,8 +14,8 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import java.util.List;
-
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.ovsdb.utils.mdsal.utils.ControllerNotifyingDataChangeListener;
 import org.opendaylight.ovsdb.utils.mdsal.utils.NotifyingDataChangeListener;
 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
@@ -32,16 +32,16 @@ public class NodeInfo {
     private static final Logger LOG = LoggerFactory.getLogger(NodeInfo.class);
     public static final String INTEGRATION_BRIDGE_NAME = "br-int";
 
-    private ConnectionInfo connectionInfo;
-    private InstanceIdentifier<Node> ovsdbIid;
-    InstanceIdentifier<Node> bridgeIid;
+    private final ConnectionInfo connectionInfo;
+    private final InstanceIdentifier<Node> ovsdbIid;
+    private final InstanceIdentifier<Node> bridgeIid;
     public long datapathId;
     public Node ovsdbNode;
     public Node bridgeNode;
-    NotifyingDataChangeListener ovsdbWaiter;
-    NotifyingDataChangeListener bridgeWaiter;
-    List<NotifyingDataChangeListener> waitList;
-    OvsdbItUtils itUtils;
+    private ControllerNotifyingDataChangeListener ovsdbWaiter;
+    private ControllerNotifyingDataChangeListener bridgeWaiter;
+    private final List<ControllerNotifyingDataChangeListener> waitList;
+    private final OvsdbItUtils itUtils;
 
     /**
      * Create a new NodeInfo object.
@@ -49,7 +49,8 @@ public class NodeInfo {
      * @param itUtils OvsdbItUtils instance
      * @param waitList for tracking outstanding md-sal events
      */
-    NodeInfo(ConnectionInfo connectionInfo, OvsdbItUtils itUtils, List<NotifyingDataChangeListener> waitList) {
+    NodeInfo(ConnectionInfo connectionInfo, OvsdbItUtils itUtils,
+            List<ControllerNotifyingDataChangeListener> waitList) {
         this.connectionInfo = connectionInfo;
         this.itUtils = itUtils;
         this.waitList = waitList;
@@ -57,16 +58,27 @@ public class NodeInfo {
         bridgeIid = SouthboundUtils.createInstanceIdentifier(connectionInfo, INTEGRATION_BRIDGE_NAME);
     }
 
+    private void addWaiters() {
+        ovsdbWaiter = new ControllerNotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL,
+                ControllerNotifyingDataChangeListener.BIT_CREATE, ovsdbIid, waitList);
+        ovsdbWaiter.registerDataChangeListener(itUtils.dataBroker);
+        bridgeWaiter = new ControllerNotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL,
+                ControllerNotifyingDataChangeListener.BIT_CREATE, bridgeIid, waitList);
+        bridgeWaiter.registerDataChangeListener(itUtils.dataBroker);
+    }
+
+    private void closeWaiters() throws Exception {
+        ovsdbWaiter.close();
+        bridgeWaiter.close();
+    }
+
     /**
      * Connect to the OVSDB node, wait for the connection to be established and for the integration bridge
      * to be successfully created. Contains assertions for unexpected states
      * @throws InterruptedException if interrupted while waiting for connection
      */
-    public void connect() throws InterruptedException {
-        ovsdbWaiter = new NotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL, ovsdbIid, waitList);
-        ovsdbWaiter.registerDataChangeListener(itUtils.dataBroker);
-        bridgeWaiter = new NotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL, bridgeIid, waitList);
-        bridgeWaiter.registerDataChangeListener(itUtils.dataBroker);
+    public void connect() throws Exception {
+        addWaiters();
 
         assertNotNull("connection failed", itUtils.southboundUtils.addOvsdbNode(connectionInfo, 0));
 
@@ -90,15 +102,16 @@ public class NodeInfo {
      * Remove integration bridge and teardown connection. Contains assertions for unexpected states.
      * @throws InterruptedException if interrupted while waiting for disconnect to complete
      */
-    public void disconnect() throws InterruptedException {
+    public void disconnect() throws Exception {
+        ovsdbWaiter.setMask(NotifyingDataChangeListener.BIT_DELETE);
+        bridgeWaiter.setMask(NotifyingDataChangeListener.BIT_DELETE);
         assertTrue(itUtils.southboundUtils.deleteBridge(connectionInfo, INTEGRATION_BRIDGE_NAME, 0));
         bridgeWaiter.waitForDeletion();
-        Node bridgeNode = itUtils.mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
-        assertNull("Bridge should not be found", bridgeNode);
+        assertNull("Bridge should not be found", itUtils.mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid));
         assertTrue(itUtils.southboundUtils.disconnectOvsdbNode(connectionInfo, 0));
         ovsdbWaiter.waitForDeletion();
-        Node ovsdbNode = itUtils.mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, ovsdbIid);
-        assertNull("Ovsdb node should not be found", ovsdbNode);
+        assertNull("Ovsdb node should not be found",
+                itUtils.mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, ovsdbIid));
+        closeWaiters();
     }
-
 }