Merge "In case of an error event, cleanup the config too"
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / InventoryServiceShim.java
index 837426eda42576ceb09ebb169e40ce954983df62..a920adf71d35163094514674fbb5b8a158bf66d7 100644 (file)
@@ -26,11 +26,14 @@ 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.ConnectionLocality;
+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.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;
@@ -58,16 +61,18 @@ import org.slf4j.LoggerFactory;
  *
  */
 public class InventoryServiceShim implements IContainerListener,
-        IMessageListener, ISwitchStateListener, IOFStatisticsListener {
+        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, 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;
@@ -79,6 +84,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) {
@@ -107,7 +126,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)
@@ -122,7 +141,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);
@@ -130,12 +149,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
@@ -167,6 +197,7 @@ public class InventoryServiceShim implements IContainerListener,
         this.inventoryShimInternalListeners.clear();
         this.nodeConnectorContainerMap.clear();
         this.nodeContainerMap.clear();
+        this.globalInventoryShimInternalListeners.clear();
         this.controller = null;
     }
 
@@ -209,6 +240,11 @@ public class InventoryServiceShim implements IContainerListener,
         if (sw == null) {
             return;
         }
+        Node node = NodeCreator.createOFNode(sw.getId());
+        if ((nodeProps.get(node) != null)  && (connectionOutService.isLocal(node))) {
+            logger.debug("Ignore switchAdded {}", sw);
+            return;
+        }
 
         // Add all the nodeConnectors of this switch
         Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
@@ -220,12 +256,15 @@ public class InventoryServiceShim implements IContainerListener,
                 props.addAll(prop);
             }
             nodeConnectorProps.put(entry.getKey(), props);
-            notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED,
-                    entry.getValue());
+            notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED, entry.getValue());
         }
 
         // Add this node
-        addNode(sw);
+        if (connectionOutService.getLocalityStatus(node) != ConnectionLocality.NOT_CONNECTED) {
+            addNode(sw);
+        } else {
+            logger.debug("Skipping node addition due to Connectivity Status : {}", connectionOutService.getLocalityStatus(node).name());
+        }
     }
 
     @Override
@@ -255,10 +294,10 @@ 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 });
-        Node node = p.getNode();
-        Set<String> ncContainers = this.nodeConnectorContainerMap.get(p);
+    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>();
@@ -271,7 +310,7 @@ public class InventoryServiceShim implements IContainerListener,
         switch (t) {
         case ADDED:
             if (ncContainers.add(containerName)) {
-                this.nodeConnectorContainerMap.put(p, ncContainers);
+                this.nodeConnectorContainerMap.put(nc, ncContainers);
             }
             if (nodeContainers.add(containerName)) {
                 this.nodeContainerMap.put(node, nodeContainers);
@@ -283,14 +322,14 @@ public class InventoryServiceShim implements IContainerListener,
                 if (ncContainers.isEmpty()) {
                     // Do cleanup to reduce memory footprint if no
                     // elements to be tracked
-                    this.nodeConnectorContainerMap.remove(p);
+                    this.nodeConnectorContainerMap.remove(nc);
                 } else {
-                    this.nodeConnectorContainerMap.put(p, ncContainers);
+                    this.nodeConnectorContainerMap.put(nc, ncContainers);
                 }
             }
             boolean nodeContainerUpdate = true;
-            for (NodeConnector nc : nodeConnectorContainerMap.keySet()) {
-                if ((nc.getNode().equals(node)) && (nodeConnectorContainerMap.get(nc).contains(containerName))) {
+            for (NodeConnector ncContainer : nodeConnectorContainerMap.keySet()) {
+                if ((ncContainer.getNode().equals(node)) && (nodeConnectorContainerMap.get(ncContainer).contains(containerName))) {
                     nodeContainerUpdate = false;
                     break;
                 }
@@ -309,25 +348,26 @@ public class InventoryServiceShim implements IContainerListener,
             break;
         }
 
-        Set<Property> ncProp = nodeConnectorProps.get(p);
+        Set<Property> nodeProp = nodeProps.get(node);
+        if (nodeProp == null) {
+            return;
+        }
+        Set<Property> ncProp = nodeConnectorProps.get(nc);
         // notify InventoryService
-        notifyInventoryShimInternalListener(containerName, p, t, ncProp);
+        notifyInventoryShimInternalListener(containerName, nc, t, ncProp);
 
         if (notifyNodeUpdate) {
-            Set<Property> nodeProp = nodeProps.get(node);
             notifyInventoryShimInternalListener(containerName, node, t, nodeProp);
         }
     }
 
-    private void notifyInventoryShimExternalListener(Node node,
-            UpdateType type, Set<Property> props) {
+    private void notifyInventoryShimExternalListener(Node node, UpdateType type, Set<Property> props) {
         for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
             s.updateNode(node, type, props);
         }
     }
 
-    private void notifyInventoryShimExternalListener(
-            NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
+    private void notifyInventoryShimExternalListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
         for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
             s.updateNodeConnector(nodeConnector, type, props);
         }
@@ -335,14 +375,11 @@ public class InventoryServiceShim implements IContainerListener,
 
     private void notifyInventoryShimInternalListener(String container,
             NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
-        IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
-                .get(container);
+        IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners.get(container);
         if (inventoryShimInternalListener != null) {
-            inventoryShimInternalListener.updateNodeConnector(nodeConnector,
-                    type, props);
-            logger.trace(
-                    "notifyInventoryShimInternalListener {} type {} for container {}",
-                    new Object[] { nodeConnector, type, container });
+            inventoryShimInternalListener.updateNodeConnector(nodeConnector, type, props);
+            logger.trace("notifyInventoryShimInternalListener {} type {} for container {}", new Object[] {
+                    nodeConnector, type, container });
         }
     }
 
@@ -350,32 +387,84 @@ public class InventoryServiceShim implements IContainerListener,
      * Notify all internal and external listeners
      */
     private void notifyInventoryShimListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
-        // 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) {
-            notifyInventoryShimInternalListener(container, nodeConnector, type, props);
-        }
 
-        // Notify DiscoveryService
-        notifyInventoryShimExternalListener(nodeConnector, type, 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) {
+                notifyInventoryShimInternalListener(container, nodeConnector, type, props);
+            }
+
+            // Notify plugin listeners (Discovery, DataPacket, OFstats etc.)
+            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) {
-        // 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);
+
+        //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);
+            }
+
+            // Notify plugin listeners (Discovery, DataPacket, OFstats etc.)
+            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);
+        }
+    }
+
+    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 });
         }
+    }
 
-        // Notify external listener
-        notifyInventoryShimExternalListener(node, type, props);
+    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,
@@ -397,8 +486,7 @@ public class InventoryServiceShim implements IContainerListener,
         Set<Property> props = new HashSet<Property>();
         Long sid = (Long) node.getID();
 
-        Date connectedSince = controller.getSwitches().get(sid)
-                .getConnectedDate();
+        Date connectedSince = sw.getConnectedDate();
         Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
                 .getTime();
         props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
@@ -425,6 +513,11 @@ public class InventoryServiceShim implements IContainerListener,
             props.add(b);
         }
 
+        if ((nodeProps.get(node) == null) &&  (connectionOutService.isLocal(node)))  {
+            // The switch is connected for the first time, flush all flows
+            // that may exist on this switch
+            sw.deleteAllFlows();
+       }
         nodeProps.put(node, props);
         // Notify all internal and external listeners
         notifyInventoryShimListener(node, type, props);
@@ -499,4 +592,43 @@ public class InventoryServiceShim implements IContainerListener,
     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);
+            }
+        }
+    }
 }