Bug 6692: remove InstanceIdentifierCodec instance from SBU
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / OvsdbDataTreeChangeListener.java
index b5cebe7b7e5db6ac690e6eb75e659859b37f4db8..8efe3d41b2b813858e5c1041fc8b73141a8e2a98 100644 (file)
@@ -8,15 +8,13 @@
 
 package org.opendaylight.ovsdb.southbound;
 
+import java.net.ConnectException;
 import java.net.UnknownHostException;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-
 import javax.annotation.Nonnull;
-
 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;
@@ -34,7 +32,6 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -55,6 +52,9 @@ public class OvsdbDataTreeChangeListener implements ClusteredDataTreeChangeListe
     /** The data broker. */
     private final DataBroker db;
 
+    /** The instance identifier codec. */
+    private final InstanceIdentifierCodec instanceIdentifierCodec;
+
     /** Logger. */
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbDataTreeChangeListener.class);
 
@@ -64,9 +64,11 @@ public class OvsdbDataTreeChangeListener implements ClusteredDataTreeChangeListe
      * @param db The data broker.
      * @param cm The connection manager.
      */
-    OvsdbDataTreeChangeListener(DataBroker db, OvsdbConnectionManager cm) {
+    OvsdbDataTreeChangeListener(DataBroker db, OvsdbConnectionManager cm,
+            InstanceIdentifierCodec instanceIdentifierCodec) {
         this.cm = cm;
         this.db = db;
+        this.instanceIdentifierCodec = instanceIdentifierCodec;
         InstanceIdentifier<Node> path = InstanceIdentifier
                 .create(NetworkTopology.class)
                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
@@ -122,7 +124,7 @@ public class OvsdbDataTreeChangeListener implements ClusteredDataTreeChangeListe
                             InstanceIdentifier<Node> instanceIdentifier = change.getRootPath().getRootIdentifier();
                             cm.connect(instanceIdentifier, ovsdbNode);
                             LOG.info("OVSDB node has been connected: {}",ovsdbNode);
-                        } catch (UnknownHostException e) {
+                        } catch (UnknownHostException | ConnectException e) {
                             LOG.warn("Failed to connect to ovsdbNode", e);
                         }
                     }
@@ -168,7 +170,7 @@ public class OvsdbDataTreeChangeListener implements ClusteredDataTreeChangeListe
                                 cm.disconnect(ovsdbNodeModification.getDataBefore());
                                 cm.connect(change.getRootPath().getRootIdentifier(), ovsdbNodeModification
                                         .getDataAfter());
-                            } catch (UnknownHostException e) {
+                            } catch (UnknownHostException | ConnectException e) {
                                 LOG.warn("Error disconnecting from or connecting to ovsdbNode", e);
                             }
                         }
@@ -183,7 +185,7 @@ public class OvsdbDataTreeChangeListener implements ClusteredDataTreeChangeListe
                 connectionInstancesFromChanges(changes).entrySet()) {
             OvsdbConnectionInstance connectionInstance = connectionInstanceEntry.getValue();
             connectionInstance.transact(new TransactCommandAggregator(),
-                    new BridgeOperationalState(db, changes), changes);
+                    new BridgeOperationalState(db, changes), changes, instanceIdentifierCodec);
         }
     }
 
@@ -193,28 +195,33 @@ public class OvsdbDataTreeChangeListener implements ClusteredDataTreeChangeListe
                 new HashMap<>();
         for (DataTreeModification<Node> change : changes) {
             OvsdbConnectionInstance client = null;
-            Node node = change.getRootNode().getDataAfter() != null?
-                    change.getRootNode().getDataAfter() : change.getRootNode().getDataBefore();
+            Node node = change.getRootNode().getDataAfter() != null
+                    change.getRootNode().getDataAfter() : change.getRootNode().getDataBefore();
             if (node != null) {
                 InstanceIdentifier<Node> nodeIid;
-                Augmentation nodeAug = node.getAugmentation(OvsdbNodeAugmentation.class) !=null?
-                        node.getAugmentation(OvsdbNodeAugmentation.class):node.getAugmentation(OvsdbBridgeAugmentation.class);
+                Augmentation nodeAug = node.getAugmentation(OvsdbNodeAugmentation.class) != null
+                        ? node.getAugmentation(OvsdbNodeAugmentation.class)
+                        : node.getAugmentation(OvsdbBridgeAugmentation.class);
 
-                if(nodeAug instanceof OvsdbNodeAugmentation) {
+                if (nodeAug instanceof OvsdbNodeAugmentation) {
                     OvsdbNodeAugmentation ovsdbNode = (OvsdbNodeAugmentation) nodeAug;
-                    if(ovsdbNode.getConnectionInfo() != null) {
+                    if (ovsdbNode.getConnectionInfo() != null) {
                         client = cm.getConnectionInstance(ovsdbNode.getConnectionInfo());
-                    }else {
+                    } else {
                         client = cm.getConnectionInstance(SouthboundMapper.createInstanceIdentifier(node.getNodeId()));
                     }
                 }
-                if(nodeAug instanceof OvsdbBridgeAugmentation) {
+                if (nodeAug instanceof OvsdbBridgeAugmentation) {
                     OvsdbBridgeAugmentation bridgeAugmentation = (OvsdbBridgeAugmentation)nodeAug;
-                    if(bridgeAugmentation.getManagedBy() != null) {
+                    if (bridgeAugmentation.getManagedBy() != null) {
                         nodeIid = (InstanceIdentifier<Node>) bridgeAugmentation.getManagedBy().getValue();
                         client = cm.getConnectionInstance(nodeIid);
                     }
                 }
+                if (client == null) {
+                    //Try getting from change root identifier
+                    client = cm.getConnectionInstance(change.getRootPath().getRootIdentifier());
+                }
             } else {
                 LOG.warn("Following change don't have after/before data {}", change);
             }