Merge "Add getTerminationPointOfBridge method to SouthboundUtils"
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepDataChangeListener.java
index 4d3daf5d1954c4eb8dec6062e468adbf8757fa32..5cca754d4b009ab573164316b20c812ae5e8c2a5 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.ovsdb.hwvtepsouthbound;
 
+import java.net.ConnectException;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -16,10 +17,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
@@ -40,7 +41,7 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
 
-public class HwvtepDataChangeListener implements DataTreeChangeListener<Node>, AutoCloseable {
+public class HwvtepDataChangeListener implements ClusteredDataTreeChangeListener<Node>, AutoCloseable {
 
     private ListenerRegistration<HwvtepDataChangeListener> registration;
     private HwvtepConnectionManager hcm;
@@ -61,7 +62,7 @@ public class HwvtepDataChangeListener implements DataTreeChangeListener<Node>, A
             LOG.trace("Registering on path: {}", treeId);
             registration = db.registerDataTreeChangeListener(treeId, HwvtepDataChangeListener.this);
         } catch (final Exception e) {
-            LOG.warn("HwvtepDataChangeListener registration failed");
+            LOG.warn("HwvtepDataChangeListener registration failed", e);
             //TODO: Should we throw an exception here?
         }
     }
@@ -133,9 +134,9 @@ public class HwvtepDataChangeListener implements DataTreeChangeListener<Node>, A
                                         + "to same device, hence dropping the request {}", connection, hwvtepGlobal);
                     } else {
                         try {
-                            hcm.connect(HwvtepSouthboundMapper.createInstanceIdentifier(node.getNodeId()), hwvtepGlobal);
-                        } catch (UnknownHostException e) {
-                            LOG.warn("Failed to connect to OVSDB node", e);
+                            OvsdbClient client = hcm.connect(key, hwvtepGlobal);
+                        } catch (UnknownHostException | ConnectException e) {
+                            LOG.warn("Failed to connect to HWVTEP node", e);
                         }
                     }
                 }
@@ -153,14 +154,19 @@ public class HwvtepDataChangeListener implements DataTreeChangeListener<Node>, A
                 HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
                 HwvtepGlobalAugmentation hgOriginal = original.getAugmentation(HwvtepGlobalAugmentation.class);
                 // Check if user has updated connection information
-                if (hgUpdated != null && hgOriginal != null && hgUpdated.getConnectionInfo() != null) {
+                if (hgUpdated != null && hgOriginal != null && hgUpdated.getConnectionInfo() != null
+                                && !hgUpdated.getConnectionInfo().equals(hgOriginal.getConnectionInfo())) {
                     OvsdbClient client = hcm.getClient(hgUpdated.getConnectionInfo());
                     if (client == null) {
                         try {
                             hcm.disconnect(hgOriginal);
-                            hcm.connect(HwvtepSouthboundMapper.createInstanceIdentifier(original.getNodeId()), hgUpdated);
-                        } catch (UnknownHostException e) {
-                            LOG.warn("Failed to update connection on OVSDB Node", e);
+                            hcm.stopConnectionReconciliationIfActive(key, hgOriginal);
+                            OvsdbClient newClient = hcm.connect(key, hgUpdated);
+                            if (newClient == null) {
+                                hcm.reconcileConnection(key, hgUpdated);
+                            }
+                        } catch (UnknownHostException | ConnectException e) {
+                            LOG.warn("Failed to update connection on HWVTEP Node", e);
                         }
                     }
                 }
@@ -169,7 +175,7 @@ public class HwvtepDataChangeListener implements DataTreeChangeListener<Node>, A
     }
 
     private void updateData(Collection<DataTreeModification<Node>> changes) {
-        /* TODO: 
+        /* TODO:
          * Get connection instances for each change
          * Update data for each connection
          * Requires Command patterns. TBD.
@@ -192,8 +198,9 @@ public class HwvtepDataChangeListener implements DataTreeChangeListener<Node>, A
                 if (hgDeleted != null) {
                     try {
                         hcm.disconnect(hgDeleted);
+                        hcm.stopConnectionReconciliationIfActive(key, hgDeleted);
                     } catch (UnknownHostException e) {
-                        LOG.warn("Failed to disconnect OVSDB Node", e);
+                        LOG.warn("Failed to disconnect HWVTEP Node", e);
                     }
                 }
             }