Bug 5542 - Southbound plugin doesn't allow controller initiated re-connection
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / OvsdbConnectionManager.java
index ecfedfe48f1b787f71331f454c1dc944ad72898e..6f7f8595745acec5b96236bec85e50c0c21f7d03 100644 (file)
@@ -43,11 +43,13 @@ import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
 import org.opendaylight.ovsdb.southbound.transactions.md.OvsdbNodeRemoveCommand;
+import org.opendaylight.ovsdb.southbound.transactions.md.TransactionCommand;
 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -86,11 +88,24 @@ public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoClos
 
     @Override
     public void connected(@Nonnull final OvsdbClient externalClient) {
+        LOG.info("Library connected {} from {}:{} to {}:{}",
+                externalClient.getConnectionInfo().getType(),
+                externalClient.getConnectionInfo().getRemoteAddress(),
+                externalClient.getConnectionInfo().getRemotePort(),
+                externalClient.getConnectionInfo().getLocalAddress(),
+                externalClient.getConnectionInfo().getLocalPort());
+        List<String> databases = new ArrayList<>();
+        try {
+            databases = externalClient.getDatabases().get();
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.warn("Unable to fetch database list");
+        }
 
-        OvsdbConnectionInstance client = connectedButCallBacksNotRegistered(externalClient);
-
-        // Register Cluster Ownership for ConnectionInfo
-        registerEntityForOwnership(client);
+        if(databases.contains(SouthboundConstants.OPEN_V_SWITCH)) {
+            OvsdbConnectionInstance client = connectedButCallBacksNotRegistered(externalClient);
+            // Register Cluster Ownership for ConnectionInfo
+            registerEntityForOwnership(client);
+        }
     }
 
     public OvsdbConnectionInstance connectedButCallBacksNotRegistered(final OvsdbClient externalClient) {
@@ -125,9 +140,12 @@ public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoClos
 
     @Override
     public void disconnected(OvsdbClient client) {
-        LOG.info("OVSDB Disconnected from {}:{}. Cleaning up the operational data store"
-                ,client.getConnectionInfo().getRemoteAddress(),
-                client.getConnectionInfo().getRemotePort());
+        LOG.info("Library disconnected {} from {}:{} to {}:{}. Cleaning up the operational data store",
+                client.getConnectionInfo().getType(),
+                client.getConnectionInfo().getRemoteAddress(),
+                client.getConnectionInfo().getRemotePort(),
+                client.getConnectionInfo().getLocalAddress(),
+                client.getConnectionInfo().getLocalPort());
         ConnectionInfo key = SouthboundMapper.createConnectionInfo(client);
         OvsdbConnectionInstance ovsdbConnectionInstance = getConnectionInstance(key);
         if (ovsdbConnectionInstance != null) {
@@ -140,14 +158,20 @@ public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoClos
             txInvoker.invoke(new OvsdbNodeRemoveCommand(ovsdbConnectionInstance, null, null));
 
             removeConnectionInstance(key);
+
+            //Controller initiated connection can be terminated from switch side.
+            //So cleanup the instance identifier cache.
+            removeInstanceIdentifier(key);
         } else {
             LOG.warn("disconnected : Connection instance not found for OVSDB Node {} ", key);
         }
-        LOG.trace("OvsdbConnectionManager: disconnected exit");
+        LOG.trace("OvsdbConnectionManager: exit disconnected client: {}", client);
     }
 
     public OvsdbClient connect(InstanceIdentifier<Node> iid,
             OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
+        LOG.info("Connecting to {}", SouthboundUtil.connectionInfoToString(ovsdbNode.getConnectionInfo()));
+
         // TODO handle case where we already have a connection
         // TODO use transaction chains to handle ordering issues between disconnected
         // TODO and connected when writing to the operational store
@@ -170,6 +194,7 @@ public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoClos
     }
 
     public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
+        LOG.info("Disconnecting from {}", SouthboundUtil.connectionInfoToString(ovsdbNode.getConnectionInfo()));
         OvsdbConnectionInstance client = getConnectionInstance(ovsdbNode.getConnectionInfo());
         if (client != null) {
             // Unregister Cluster Onwership for ConnectionInfo
@@ -198,7 +223,7 @@ public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoClos
     }
 */
     @Override
-    public void close() throws Exception {
+    public void close() {
         if (ovsdbDeviceEntityOwnershipListener != null) {
             ovsdbDeviceEntityOwnershipListener.close();
         }
@@ -268,7 +293,7 @@ public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoClos
                     LogicalDatastoreType.OPERATIONAL, nodePath);
             transaction.close();
             Optional<Node> optional = nodeFuture.get();
-            if (optional != null && optional.isPresent() && optional.get() instanceof Node) {
+            if (optional != null && optional.isPresent() && optional.get() != null) {
                 return this.getConnectionInstance(optional.get());
             } else {
                 LOG.warn("Found non-topological node {} on path {}",optional);
@@ -300,13 +325,6 @@ public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoClos
         return ovsdbConnectionInstance.getHasDeviceOwnership();
     }
 
-    public void setHasDeviceOwnership(ConnectionInfo connectionInfo, Boolean hasDeviceOwnership) {
-        OvsdbConnectionInstance ovsdbConnectionInstance = getConnectionInstance(connectionInfo);
-        if (ovsdbConnectionInstance != null) {
-            ovsdbConnectionInstance.setHasDeviceOwnership(hasDeviceOwnership);
-        }
-    }
-
     private void handleOwnershipChanged(EntityOwnershipChange ownershipChange) {
         OvsdbConnectionInstance ovsdbConnectionInstance = getConnectionInstanceFromEntity(ownershipChange.getEntity());
         LOG.debug("handleOwnershipChanged: {} event received for device {}",
@@ -371,14 +389,36 @@ public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoClos
 
     private void cleanEntityOperationalData(Entity entity) {
 
-        InstanceIdentifier<Node> nodeIid = (InstanceIdentifier<Node>) SouthboundUtil
-                .getInstanceIdentifierCodec().bindingDeserializer(entity.getId());
+        //Do explicit cleanup rather than using OvsdbNodeRemoveCommand, because there
+        // are chances that other controller instance went down abruptly and it does
+        // not clear manager entry, which OvsdbNodeRemoveCommand look for before cleanup.
+
+        @SuppressWarnings("unchecked") final InstanceIdentifier<Node> nodeIid =
+                (InstanceIdentifier<Node>) SouthboundUtil
+                        .getInstanceIdentifierCodec().bindingDeserializer(entity.getId());
+
+        txInvoker.invoke(new TransactionCommand() {
+            @Override
+            public void execute(ReadWriteTransaction transaction) {
+                Optional<Node> ovsdbNodeOpt = SouthboundUtil.readNode(transaction, nodeIid);
+                if (ovsdbNodeOpt.isPresent()) {
+                    Node ovsdbNode = ovsdbNodeOpt.get();
+                    OvsdbNodeAugmentation nodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
+                    if (nodeAugmentation != null) {
+                        if (nodeAugmentation.getManagedNodeEntry() != null) {
+                            for (ManagedNodeEntry managedNode : nodeAugmentation.getManagedNodeEntry()) {
+                                transaction.delete(
+                                        LogicalDatastoreType.OPERATIONAL, managedNode.getBridgeRef().getValue());
+                            }
+                        } else {
+                            LOG.debug("{} had no managed nodes", ovsdbNode.getNodeId().getValue());
+                        }
+                    }
+                    transaction.delete(LogicalDatastoreType.OPERATIONAL, nodeIid);
+                }
+            }
+        });
 
-        final ReadWriteTransaction transaction = db.newReadWriteTransaction();
-        Optional<Node> node = SouthboundUtil.readNode(transaction, nodeIid);
-        if (node.isPresent()) {
-            SouthboundUtil.deleteNode(transaction, nodeIid);
-        }
     }
 
     private OpenVSwitch getOpenVswitchTableEntry(OvsdbConnectionInstance connectionInstance) {