Adding code to handle multiple hosts per node connector 38/838/1
authorAsad Ahmed <asaahmed@cisco.com>
Fri, 9 Aug 2013 21:56:27 +0000 (14:56 -0700)
committerAsad Ahmed <asaahmed@cisco.com>
Fri, 9 Aug 2013 22:26:14 +0000 (15:26 -0700)
Change-Id: I32b18fe019a2806f0bb2778321f0a3ac6c6b23a2
Signed-off-by: Asad Ahmed <asaahmed@cisco.com>
opendaylight/topologymanager/src/main/java/org/opendaylight/controller/topologymanager/ITopologyManager.java
opendaylight/topologymanager/src/main/java/org/opendaylight/controller/topologymanager/internal/TopologyManagerImpl.java

index de4f0d5347eb5b7954e8f9e5c2d84748f562ee84..1f377593bb8282cb6f303354531e4b6368f5c1ff 100644 (file)
@@ -9,6 +9,7 @@
 
 package org.opendaylight.controller.topologymanager;
 
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentMap;
@@ -67,12 +68,23 @@ public interface ITopologyManager {
     public Set<NodeConnector> getNodeConnectorWithHost();
 
     /**
-     * Return the Host attached to a NodeConnector with Host
-     *
+     * Return the Host attached to a NodeConnector with Host.
+     * Multiple hosts maybe attached to a node connector.
+     * This method returns the first one only. Hence it has been
+     * deprecated. Use the new method getHostsAttachedToNodeConnector
+     * that returns the complete list of hosts.
      * @return The Host attached to a NodeConnector
      */
+    @Deprecated
     public Host getHostAttachedToNodeConnector(NodeConnector p);
 
+    /**
+     * Returns a list of hosts attached to a NodeConnector
+     * @param p NodeConnector
+     * @return The list of hosts attached to a NodeConnector
+     */
+    public List<Host> getHostsAttachedToNodeConnector(NodeConnector p);
+
     /**
      * Returns a copy map which associates every node with all the
      * NodeConnectors of the node that have an Host attached to it
index 4972d3b5b5d73d41df3cfbbb99543db8fde2eb56..4bd295c62a2bc641ca877136b26992222ef39e4e 100644 (file)
@@ -12,12 +12,12 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.util.ArrayList;
-import java.util.Date;
 import java.util.Dictionary;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -89,7 +89,7 @@ public class TopologyManagerImpl implements
     // NodeConnector of a Production Edge is not part of this DB.
     private ConcurrentMap<NodeConnector, Set<Property>> nodeConnectorsDB;
     // DB of all the NodeConnectors with an Host attached to it
-    private ConcurrentMap<NodeConnector, ImmutablePair<Host, Set<Property>>> hostsDB;
+    private ConcurrentMap<NodeConnector, Set<ImmutablePair<Host, Set<Property>>>> hostsDB;
     // Topology Manager Aware listeners
     private Set<ITopologyManagerAware> topologyManagerAware = new CopyOnWriteArraySet<ITopologyManagerAware>();
     // Topology Manager Aware listeners - for clusterwide updates
@@ -105,7 +105,7 @@ public class TopologyManagerImpl implements
 
     void nonClusterObjectCreate() {
         edgesDB = new ConcurrentHashMap<Edge, Set<Property>>();
-        hostsDB = new ConcurrentHashMap<NodeConnector, ImmutablePair<Host, Set<Property>>>();
+        hostsDB = new ConcurrentHashMap<NodeConnector, Set<ImmutablePair<Host, Set<Property>>>>();
         nodeConnectorsDB = new ConcurrentHashMap<NodeConnector, Set<Property>>();
         userLinksDB = new ConcurrentHashMap<String, TopologyUserLinkConfig>();
     }
@@ -201,7 +201,7 @@ public class TopologyManagerImpl implements
 
         try {
             this.hostsDB =
-                    (ConcurrentMap<NodeConnector, ImmutablePair<Host, Set<Property>>>) this.clusterContainerService.createCache(
+                    (ConcurrentMap<NodeConnector, Set<ImmutablePair<Host, Set<Property>>>>) this.clusterContainerService.createCache(
                             TOPOHOSTSDB, EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
         } catch (CacheExistException cee) {
             log.debug(TOPOHOSTSDB + " Cache already exists - destroy and recreate if needed");
@@ -243,7 +243,7 @@ public class TopologyManagerImpl implements
         }
 
         this.hostsDB =
-                (ConcurrentMap<NodeConnector, ImmutablePair<Host, Set<Property>>>) this.clusterContainerService.getCache(TOPOHOSTSDB);
+                (ConcurrentMap<NodeConnector, Set<ImmutablePair<Host, Set<Property>>>>) this.clusterContainerService.getCache(TOPOHOSTSDB);
         if (hostsDB == null) {
             log.error("Failed to get cache for " + TOPOHOSTSDB);
         }
@@ -451,11 +451,25 @@ public class TopologyManagerImpl implements
 
     @Override
     public Host getHostAttachedToNodeConnector(NodeConnector port) {
-        ImmutablePair<Host, Set<Property>> host;
-        if ((this.hostsDB == null) || ((host = this.hostsDB.get(port)) == null)) {
+        List<Host> hosts = getHostsAttachedToNodeConnector(port);
+        if(hosts != null && !hosts.isEmpty()){
+            return hosts.get(0);
+        }
+        return null;
+    }
+
+    @Override
+    public List<Host> getHostsAttachedToNodeConnector(NodeConnector p) {
+        Set<ImmutablePair<Host, Set<Property>>> hosts;
+        if (this.hostsDB == null || (hosts = this.hostsDB.get(p)) == null) {
             return null;
         }
-        return host.getLeft();
+        // create a list of hosts
+        List<Host> retHosts = new LinkedList<Host>();
+        for(ImmutablePair<Host, Set<Property>> host : hosts) {
+            retHosts.add(host.getLeft());
+        }
+        return retHosts;
     }
 
     @Override
@@ -471,14 +485,25 @@ public class TopologyManagerImpl implements
         }
         ImmutablePair<Host, Set<Property>> thisHost = new ImmutablePair<Host, Set<Property>>(h, props);
 
+        // get the host list
+        Set<ImmutablePair<Host, Set<Property>>> hostSet = this.hostsDB.get(port);
+        if(hostSet == null) {
+            hostSet = new HashSet<ImmutablePair<Host, Set<Property>>>();
+        }
         switch (t) {
         case ADDED:
         case CHANGED:
-            this.hostsDB.put(port, thisHost);
+            hostSet.add(thisHost);
+            this.hostsDB.put(port, hostSet);
             break;
         case REMOVED:
-            //remove only if hasn't been concurrently modified
-            this.hostsDB.remove(port, thisHost);
+            hostSet.remove(thisHost);
+            if(hostSet.isEmpty()) {
+                //remove only if hasn't been concurrently modified
+                this.hostsDB.remove(port, hostSet);
+            } else {
+                this.hostsDB.put(port, hostSet);
+            }
             break;
         }
     }