Fix for cache cleanup in protocol plugin on container deletion
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / InventoryServiceShim.java
index e2d77b5b73f1b1c3c73da363b2f40949319c0a0a..adc121816b818d099469aa1919b74b2295b60152 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.controller.protocol_plugin.openflow.internal;
 
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
@@ -16,35 +17,38 @@ import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
 
 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener;
 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimInternalListener;
-import org.opendaylight.controller.protocol_plugin.openflow.IStatisticsListener;
+import org.opendaylight.controller.protocol_plugin.openflow.IOFStatisticsListener;
 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
 import org.opendaylight.controller.protocol_plugin.openflow.core.IMessageListener;
 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitchStateListener;
+import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
 import org.opendaylight.controller.sal.core.Actions;
 import org.opendaylight.controller.sal.core.Buffers;
 import org.opendaylight.controller.sal.core.Capabilities;
-import org.opendaylight.controller.sal.core.ConstructionException;
 import org.opendaylight.controller.sal.core.ContainerFlow;
 import org.opendaylight.controller.sal.core.Description;
+import org.opendaylight.controller.sal.core.IContainerAware;
 import org.opendaylight.controller.sal.core.IContainerListener;
 import org.opendaylight.controller.sal.core.MacAddress;
 import org.opendaylight.controller.sal.core.Node;
-import org.opendaylight.controller.sal.core.Node.NodeIDType;
 import org.opendaylight.controller.sal.core.NodeConnector;
 import org.opendaylight.controller.sal.core.Property;
 import org.opendaylight.controller.sal.core.Tables;
 import org.opendaylight.controller.sal.core.TimeStamp;
 import org.opendaylight.controller.sal.core.UpdateType;
 import org.opendaylight.controller.sal.utils.GlobalConstants;
+import org.opendaylight.controller.sal.utils.NodeCreator;
 import org.openflow.protocol.OFMessage;
 import org.openflow.protocol.OFPortStatus;
 import org.openflow.protocol.OFPortStatus.OFPortReason;
 import org.openflow.protocol.OFType;
 import org.openflow.protocol.statistics.OFDescriptionStatistics;
+import org.openflow.protocol.statistics.OFStatistics;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -56,13 +60,18 @@ import org.slf4j.LoggerFactory;
  *
  */
 public class InventoryServiceShim implements IContainerListener,
-        IMessageListener, ISwitchStateListener, IStatisticsListener {
+        IMessageListener, ISwitchStateListener, IOFStatisticsListener, IContainerAware {
     protected static final Logger logger = LoggerFactory
             .getLogger(InventoryServiceShim.class);
     private IController controller = null;
     private final ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
+    private final Set<IInventoryShimInternalListener> globalInventoryShimInternalListeners = new HashSet<IInventoryShimInternalListener>();
     private final List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
-    private final ConcurrentMap<NodeConnector, List<String>> containerMap = new ConcurrentHashMap<NodeConnector, List<String>>();
+    private final ConcurrentMap<NodeConnector, Set<String>> nodeConnectorContainerMap = new ConcurrentHashMap<NodeConnector, Set<String>>();
+    private final ConcurrentMap<Node, Set<String>> nodeContainerMap = new ConcurrentHashMap<Node, Set<String>>();
+    private final ConcurrentMap<NodeConnector, Set<Property>> nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Set<Property>>();
+    private final ConcurrentMap<Node, Set<Property>> nodeProps = new ConcurrentHashMap<Node, Set<Property>>();
+    private IPluginOutConnectionService connectionOutService;
 
     void setController(IController s) {
         this.controller = s;
@@ -74,6 +83,20 @@ public class InventoryServiceShim implements IContainerListener,
         }
     }
 
+    void setInventoryShimGlobalInternalListener(Map<?, ?> props,
+            IInventoryShimInternalListener s) {
+        if ((this.globalInventoryShimInternalListeners != null)) {
+            this.globalInventoryShimInternalListeners.add(s);
+        }
+    }
+
+    void unsetInventoryShimGlobalInternalListener(Map<?, ?> props,
+            IInventoryShimInternalListener s) {
+        if ((this.globalInventoryShimInternalListeners != null)) {
+            this.globalInventoryShimInternalListeners.remove(s);
+        }
+    }
+
     void setInventoryShimInternalListener(Map<?, ?> props,
             IInventoryShimInternalListener s) {
         if (props == null) {
@@ -102,7 +125,7 @@ public class InventoryServiceShim implements IContainerListener,
         }
         String containerName = (String) props.get("containerName");
         if (containerName == null) {
-            logger.error("unsetInventoryShimInternalListener containerName not supplied");
+            logger.error("setInventoryShimInternalListener containerName not supplied");
             return;
         }
         if ((this.inventoryShimInternalListeners != null)
@@ -117,7 +140,7 @@ public class InventoryServiceShim implements IContainerListener,
     }
 
     void setInventoryShimExternalListener(IInventoryShimExternalListener s) {
-        logger.trace("Set inventoryShimExternalListener");
+        logger.trace("Set inventoryShimExternalListener {}", s);
         if ((this.inventoryShimExternalListeners != null)
                 && !this.inventoryShimExternalListeners.contains(s)) {
             this.inventoryShimExternalListeners.add(s);
@@ -125,12 +148,23 @@ public class InventoryServiceShim implements IContainerListener,
     }
 
     void unsetInventoryShimExternalListener(IInventoryShimExternalListener s) {
+        logger.trace("Unset inventoryShimExternalListener {}", s);
         if ((this.inventoryShimExternalListeners != null)
                 && this.inventoryShimExternalListeners.contains(s)) {
             this.inventoryShimExternalListeners.remove(s);
         }
     }
 
+    void setIPluginOutConnectionService(IPluginOutConnectionService s) {
+        connectionOutService = s;
+    }
+
+    void unsetIPluginOutConnectionService(IPluginOutConnectionService s) {
+        if (connectionOutService == s) {
+            connectionOutService = null;
+        }
+    }
+
     /**
      * Function called by the dependency manager when all the required
      * dependencies are satisfied
@@ -160,43 +194,42 @@ public class InventoryServiceShim implements IContainerListener,
         this.controller.removeSwitchStateListener(this);
 
         this.inventoryShimInternalListeners.clear();
-        this.containerMap.clear();
+        this.nodeConnectorContainerMap.clear();
+        this.nodeContainerMap.clear();
+        this.globalInventoryShimInternalListeners.clear();
         this.controller = null;
     }
 
     @Override
     public void receive(ISwitch sw, OFMessage msg) {
-        try {
-            if (msg instanceof OFPortStatus) {
-                handlePortStatusMessage(sw, (OFPortStatus) msg);
-            }
-        } catch (ConstructionException e) {
-            logger.error("",e);
+        if (msg instanceof OFPortStatus) {
+            handlePortStatusMessage(sw, (OFPortStatus) msg);
         }
         return;
     }
 
-    protected void handlePortStatusMessage(ISwitch sw, OFPortStatus m)
-            throws ConstructionException {
-        Node node = new Node(NodeIDType.OPENFLOW, sw.getId());
-        NodeConnector nodeConnector = PortConverter.toNodeConnector(m.getDesc()
-                .getPortNumber(), node);
-        UpdateType type = null;
+    protected void handlePortStatusMessage(ISwitch sw, OFPortStatus m) {
+        Node node = NodeCreator.createOFNode(sw.getId());
+        NodeConnector nodeConnector = PortConverter.toNodeConnector(
+            m.getDesc().getPortNumber(), node);
+        // get node connector properties
+        Set<Property> props = InventoryServiceHelper.OFPortToProps(m.getDesc());
 
+        UpdateType type = null;
         if (m.getReason() == (byte) OFPortReason.OFPPR_ADD.ordinal()) {
             type = UpdateType.ADDED;
+            nodeConnectorProps.put(nodeConnector, props);
         } else if (m.getReason() == (byte) OFPortReason.OFPPR_DELETE.ordinal()) {
             type = UpdateType.REMOVED;
+            nodeConnectorProps.remove(nodeConnector);
         } else if (m.getReason() == (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
             type = UpdateType.CHANGED;
+            nodeConnectorProps.put(nodeConnector, props);
         }
 
         logger.trace("handlePortStatusMessage {} type {}", nodeConnector, type);
 
         if (type != null) {
-            // get node connector properties
-            Set<Property> props = InventoryServiceHelper.OFPortToProps(m
-                    .getDesc());
             notifyInventoryShimListener(nodeConnector, type, props);
         }
     }
@@ -211,6 +244,12 @@ public class InventoryServiceShim implements IContainerListener,
         Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
                 .OFSwitchToProps(sw);
         for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps.entrySet()) {
+            Set<Property> props = new HashSet<Property>();
+            Set<Property> prop = entry.getValue();
+            if (prop != null) {
+                props.addAll(prop);
+            }
+            nodeConnectorProps.put(entry.getKey(), props);
             notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED,
                     entry.getValue());
         }
@@ -246,48 +285,71 @@ public class InventoryServiceShim implements IContainerListener,
     }
 
     @Override
-    public void nodeConnectorUpdated(String containerName, NodeConnector p,
-            UpdateType t) {
-        logger.debug("nodeConnectorUpdated: {} type {} for container {}",
-                new Object[] { p, t, containerName });
-        if (this.containerMap == null) {
-            logger.error("containerMap is NULL");
-            return;
+    public void nodeConnectorUpdated(String containerName, NodeConnector nc, UpdateType t) {
+        logger.debug("nodeConnectorUpdated: {} type {} for container {}", new Object[] { nc, t, containerName });
+        Node node = nc.getNode();
+        Set<String> ncContainers = this.nodeConnectorContainerMap.get(nc);
+        Set<String> nodeContainers = this.nodeContainerMap.get(node);
+        if (ncContainers == null) {
+            ncContainers = new CopyOnWriteArraySet<String>();
         }
-        List<String> containers = this.containerMap.get(p);
-        if (containers == null) {
-            containers = new CopyOnWriteArrayList<String>();
+        if (nodeContainers == null) {
+            nodeContainers = new CopyOnWriteArraySet<String>();
         }
-        boolean updateMap = false;
+        boolean notifyNodeUpdate = false;
+
         switch (t) {
         case ADDED:
-            if (!containers.contains(containerName)) {
-                containers.add(containerName);
-                updateMap = true;
+            if (ncContainers.add(containerName)) {
+                this.nodeConnectorContainerMap.put(nc, ncContainers);
+            }
+            if (nodeContainers.add(containerName)) {
+                this.nodeContainerMap.put(node, nodeContainers);
+                notifyNodeUpdate = true;
             }
             break;
         case REMOVED:
-            if (containers.contains(containerName)) {
-                containers.remove(containerName);
-                updateMap = true;
+            if (ncContainers.remove(containerName)) {
+                if (ncContainers.isEmpty()) {
+                    // Do cleanup to reduce memory footprint if no
+                    // elements to be tracked
+                    this.nodeConnectorContainerMap.remove(nc);
+                } else {
+                    this.nodeConnectorContainerMap.put(nc, ncContainers);
+                }
+            }
+            boolean nodeContainerUpdate = true;
+            for (NodeConnector ncContainer : nodeConnectorContainerMap.keySet()) {
+                if ((ncContainer.getNode().equals(node)) && (nodeConnectorContainerMap.get(ncContainer).contains(containerName))) {
+                    nodeContainerUpdate = false;
+                    break;
+                }
+            }
+            if (nodeContainerUpdate) {
+                nodeContainers.remove(containerName);
+                notifyNodeUpdate = true;
+                if (nodeContainers.isEmpty()) {
+                    this.nodeContainerMap.remove(node);
+                } else {
+                    this.nodeContainerMap.put(node, nodeContainers);
+                }
             }
             break;
         case CHANGED:
             break;
         }
-        if (updateMap) {
-            if (containers.isEmpty()) {
-                // Do cleanup to reduce memory footprint if no
-                // elements to be tracked
-                this.containerMap.remove(p);
-            } else {
-                this.containerMap.put(p, containers);
-            }
-        }
 
+        Set<Property> nodeProp = nodeProps.get(node);
+        if (nodeProp == null) {
+            return;
+        }
+        Set<Property> ncProp = nodeConnectorProps.get(nc);
         // notify InventoryService
-        notifyInventoryShimInternalListener(containerName, p, t, null);
-        notifyInventoryShimInternalListener(containerName, p.getNode(), t, null);
+        notifyInventoryShimInternalListener(containerName, nc, t, ncProp);
+
+        if (notifyNodeUpdate) {
+            notifyInventoryShimInternalListener(containerName, node, t, nodeProp);
+        }
     }
 
     private void notifyInventoryShimExternalListener(Node node,
@@ -320,62 +382,87 @@ public class InventoryServiceShim implements IContainerListener,
     /*
      * Notify all internal and external listeners
      */
-    private void notifyInventoryShimListener(NodeConnector nodeConnector,
-            UpdateType type, Set<Property> props) {
-        // Always notify default InventoryService. Store properties in default
-        // one.
-        notifyInventoryShimInternalListener(GlobalConstants.DEFAULT.toString(),
-                nodeConnector, type, props);
-
-        // Now notify other containers
-        List<String> containers = containerMap.get(nodeConnector);
-        if (containers != null) {
+    private void notifyInventoryShimListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
+
+        //establish locality before notifying
+        boolean isNodeLocal;
+        if (type == UpdateType.REMOVED){
+            //if removing get the locality first
+            isNodeLocal = connectionOutService.isLocal(nodeConnector.getNode());
+            notifyGlobalInventoryShimInternalListener(nodeConnector, type, props);
+        } else {
+            notifyGlobalInventoryShimInternalListener(nodeConnector, type, props);
+            isNodeLocal = connectionOutService.isLocal(nodeConnector.getNode());
+        }
+
+        if (isNodeLocal) {
+            // notify other containers
+            Set<String> containers = (nodeConnectorContainerMap.get(nodeConnector) == null) ? new HashSet<String>()
+                    : new HashSet<String>(nodeConnectorContainerMap.get(nodeConnector));
+            containers.add(GlobalConstants.DEFAULT.toString());
             for (String container : containers) {
-                // no property stored in container components.
-                notifyInventoryShimInternalListener(container, nodeConnector,
-                        type, null);
+                notifyInventoryShimInternalListener(container, nodeConnector, type, props);
             }
-        }
 
-        // Notify DiscoveryService
-        notifyInventoryShimExternalListener(nodeConnector, type, props);
+            // Notify DiscoveryService
+            notifyInventoryShimExternalListener(nodeConnector, type, props);
+
+            logger.debug("Connection service accepted the inventory notification for {} {}", nodeConnector, type);
+        } else {
+            logger.debug("Connection service dropped the inventory notification for {} {}", nodeConnector, type);
+        }
     }
 
     /*
      * Notify all internal and external listeners
      */
-    private void notifyInventoryShimListener(Node node, UpdateType type,
-            Set<Property> props) {
-        switch (type) {
-        case ADDED:
-            // Notify only the default Inventory Service
-            IInventoryShimInternalListener inventoryShimDefaultListener = inventoryShimInternalListeners
-                    .get(GlobalConstants.DEFAULT.toString());
-            if (inventoryShimDefaultListener != null) {
-                inventoryShimDefaultListener.updateNode(node, type, props);
-            }
-            break;
-        case REMOVED:
-            // Notify all Inventory Service containers
-            for (IInventoryShimInternalListener inventoryShimInternalListener : inventoryShimInternalListeners
-                    .values()) {
-                inventoryShimInternalListener.updateNode(node, type, null);
-            }
-            break;
-        case CHANGED:
-            // Notify only the default Inventory Service
-            inventoryShimDefaultListener = inventoryShimInternalListeners
-                    .get(GlobalConstants.DEFAULT.toString());
-            if (inventoryShimDefaultListener != null) {
-                inventoryShimDefaultListener.updateNode(node, type, props);
+    private void notifyInventoryShimListener(Node node, UpdateType type, Set<Property> props) {
+
+        //establish locality before notifying
+        boolean isNodeLocal;
+        if (type == UpdateType.REMOVED){
+            //if removing get the locality first
+            isNodeLocal = connectionOutService.isLocal(node);
+            notifyGlobalInventoryShimInternalListener(node, type, props);
+        } else {
+            notifyGlobalInventoryShimInternalListener(node, type, props);
+            isNodeLocal = connectionOutService.isLocal(node);
+        }
+
+        if (isNodeLocal) {
+            // Now notify other containers
+            Set<String> containers = (nodeContainerMap.get(node) == null) ? new HashSet<String>() : new HashSet<String>(
+                    nodeContainerMap.get(node));
+            containers.add(GlobalConstants.DEFAULT.toString());
+            for (String container : containers) {
+                notifyInventoryShimInternalListener(container, node, type, props);
             }
-            break;
-        default:
-            break;
+
+            // Notify external listener
+            notifyInventoryShimExternalListener(node, type, props);
+
+            logger.debug("Connection service accepted the inventory notification for {} {}", node, type);
+        } else {
+            logger.debug("Connection service dropped the inventory notification for {} {}", node, type);
         }
+    }
 
-        // Notify external listener
-        notifyInventoryShimExternalListener(node, type, props);
+    private void notifyGlobalInventoryShimInternalListener(Node node, UpdateType type, Set<Property> props) {
+        for (IInventoryShimInternalListener globalListener : globalInventoryShimInternalListeners) {
+            globalListener.updateNode(node, type, props);
+            logger.trace(
+                    "notifyGlobalInventoryShimInternalListener {} type {}",
+                    new Object[] { node, type });
+        }
+    }
+
+    private void notifyGlobalInventoryShimInternalListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
+        for (IInventoryShimInternalListener globalListener : globalInventoryShimInternalListeners) {
+            globalListener.updateNodeConnector(nodeConnector, type, props);
+            logger.trace(
+                    "notifyGlobalInventoryShimInternalListener {} type {}",
+                    new Object[] { nodeConnector, type });
+        }
     }
 
     private void notifyInventoryShimInternalListener(String container,
@@ -391,14 +478,7 @@ public class InventoryServiceShim implements IContainerListener,
     }
 
     private void addNode(ISwitch sw) {
-        Node node;
-        try {
-            node = new Node(NodeIDType.OPENFLOW, sw.getId());
-        } catch (ConstructionException e) {
-            logger.error("{}", e.getMessage());
-            return;
-        }
-
+        Node node = NodeCreator.createOFNode(sw.getId());
         UpdateType type = UpdateType.ADDED;
 
         Set<Property> props = new HashSet<Property>();
@@ -432,21 +512,19 @@ public class InventoryServiceShim implements IContainerListener,
             props.add(b);
         }
 
+        nodeProps.put(node, props);
         // Notify all internal and external listeners
         notifyInventoryShimListener(node, type, props);
     }
 
     private void removeNode(ISwitch sw) {
-        Node node;
-        try {
-            node = new Node(NodeIDType.OPENFLOW, sw.getId());
-        } catch (ConstructionException e) {
-            logger.error("{}", e.getMessage());
+        Node node = NodeCreator.createOFNode(sw.getId());
+        if(node == null) {
             return;
         }
-
+        removeNodeConnectorProps(node);
+        nodeProps.remove(node);
         UpdateType type = UpdateType.REMOVED;
-
         // Notify all internal and external listeners
         notifyInventoryShimListener(node, type, null);
     }
@@ -459,20 +537,24 @@ public class InventoryServiceShim implements IContainerListener,
         }
     }
 
-    @Override
-    public void descriptionRefreshed(Long switchId,
-            OFDescriptionStatistics descriptionStats) {
-        Node node;
-        try {
-            node = new Node(NodeIDType.OPENFLOW, switchId);
-        } catch (ConstructionException e) {
-            logger.error("{}", e.getMessage());
-            return;
+    private void removeNodeConnectorProps(Node node) {
+        List<NodeConnector> ncList = new ArrayList<NodeConnector>();
+        for (NodeConnector nc : nodeConnectorProps.keySet()) {
+            if (nc.getNode().equals(node)) {
+                ncList.add(nc);
+            }
         }
+        for (NodeConnector nc : ncList) {
+            nodeConnectorProps.remove(nc);
+        }
+    }
 
+    @Override
+    public void descriptionStatisticsRefreshed(Long switchId, List<OFStatistics> descriptionStats) {
+        Node node = NodeCreator.createOFNode(switchId);
         Set<Property> properties = new HashSet<Property>(1);
-        Description desc = new Description(
-                descriptionStats.getDatapathDescription());
+        OFDescriptionStatistics ofDesc = (OFDescriptionStatistics) descriptionStats.get(0);
+        Description desc = new Description(ofDesc.getDatapathDescription());
         properties.add(desc);
 
         // Notify all internal and external listeners
@@ -489,4 +571,58 @@ public class InventoryServiceShim implements IContainerListener,
 
         return mac;
     }
+
+    @Override
+    public void flowStatisticsRefreshed(Long switchId, List<OFStatistics> flows) {
+        // Nothing to do
+    }
+
+    @Override
+    public void portStatisticsRefreshed(Long switchId, List<OFStatistics> ports) {
+        // Nothing to do
+    }
+
+    @Override
+    public void tableStatisticsRefreshed(Long switchId, List<OFStatistics> tables) {
+        // Nothing to do
+    }
+
+    @Override
+    public void containerCreate(String containerName) {
+        // Nothing to do
+    }
+
+    @Override
+    public void containerDestroy(String containerName) {
+        Set<NodeConnector> removeNodeConnectorSet = new HashSet<NodeConnector>();
+        Set<Node> removeNodeSet = new HashSet<Node>();
+        for (Map.Entry<NodeConnector, Set<String>> entry : nodeConnectorContainerMap.entrySet()) {
+            Set<String> ncContainers = entry.getValue();
+            if (ncContainers.contains(containerName)) {
+                NodeConnector nodeConnector = entry.getKey();
+                removeNodeConnectorSet.add(nodeConnector);
+            }
+        }
+        for (Map.Entry<Node, Set<String>> entry : nodeContainerMap.entrySet()) {
+            Set<String> nodeContainers = entry.getValue();
+            if (nodeContainers.contains(containerName)) {
+                Node node = entry.getKey();
+                removeNodeSet.add(node);
+            }
+        }
+        for (NodeConnector nodeConnector : removeNodeConnectorSet) {
+            Set<String> ncContainers = nodeConnectorContainerMap.get(nodeConnector);
+            ncContainers.remove(containerName);
+            if (ncContainers.isEmpty()) {
+                nodeConnectorContainerMap.remove(nodeConnector);
+            }
+        }
+        for (Node node : removeNodeSet) {
+            Set<String> nodeContainers = nodeContainerMap.get(node);
+            nodeContainers.remove(containerName);
+            if (nodeContainers.isEmpty()) {
+                nodeContainerMap.remove(node);
+            }
+        }
+    }
 }