Move stats caching to FM StatisticsManager
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / InventoryServiceShim.java
index cd05f3125a460c48ac34d9d3ff97a6230ebf59ed..a6d5a15313719829f92d8be37f2ec3766d7dea1e 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -20,35 +19,36 @@ import java.util.concurrent.CopyOnWriteArrayList;
 
 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener;
 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimInternalListener;
-import org.opendaylight.controller.protocol_plugin.openflow.IOFStatisticsManager;
+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.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;
-
 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.IContainerListener;
-import org.opendaylight.controller.sal.core.Name;
+import org.opendaylight.controller.sal.core.MacAddress;
 import org.opendaylight.controller.sal.core.Node;
-import org.opendaylight.controller.sal.core.Tables;
 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;
 
 /**
  * The class describes a shim layer that bridges inventory events from Openflow
@@ -58,14 +58,13 @@ import org.opendaylight.controller.sal.utils.GlobalConstants;
  *
  */
 public class InventoryServiceShim implements IContainerListener,
-        IMessageListener, ISwitchStateListener {
+        IMessageListener, ISwitchStateListener, IOFStatisticsListener {
     protected static final Logger logger = LoggerFactory
             .getLogger(InventoryServiceShim.class);
     private IController controller = null;
-    private ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
-    private List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
-    private ConcurrentMap<NodeConnector, List<String>> containerMap = new ConcurrentHashMap<NodeConnector, List<String>>();
-    private IOFStatisticsManager statsMgr;
+    private final ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
+    private final List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
+    private final ConcurrentMap<NodeConnector, List<String>> containerMap = new ConcurrentHashMap<NodeConnector, List<String>>();
 
     void setController(IController s) {
         this.controller = s;
@@ -77,52 +76,45 @@ public class InventoryServiceShim implements IContainerListener,
         }
     }
 
-    void setStatisticsManager(IOFStatisticsManager s) {
-        this.statsMgr = s;
-    }
-
-    void unsetStatisticsManager(IOFStatisticsManager s) {
-        if (this.statsMgr == s) {
-            this.statsMgr = null;
-        }
-    }
-
     void setInventoryShimInternalListener(Map<?, ?> props,
             IInventoryShimInternalListener s) {
         if (props == null) {
-            logger.error("Didn't receive the service properties");
+            logger.error("setInventoryShimInternalListener property is null");
             return;
         }
         String containerName = (String) props.get("containerName");
         if (containerName == null) {
-            logger.error("containerName not supplied");
+            logger.error("setInventoryShimInternalListener containerName not supplied");
             return;
         }
         if ((this.inventoryShimInternalListeners != null)
-                && !this.inventoryShimInternalListeners.containsKey(s)) {
+                && !this.inventoryShimInternalListeners.containsValue(s)) {
             this.inventoryShimInternalListeners.put(containerName, s);
-            logger.trace("Added inventoryShimInternalListener for container:"
-                    + containerName);
+            logger.trace(
+                    "Added inventoryShimInternalListener for container {}",
+                    containerName);
         }
     }
 
     void unsetInventoryShimInternalListener(Map<?, ?> props,
             IInventoryShimInternalListener s) {
         if (props == null) {
-            logger.error("Didn't receive the service properties");
+            logger.error("unsetInventoryShimInternalListener property is null");
             return;
         }
         String containerName = (String) props.get("containerName");
         if (containerName == null) {
-            logger.error("containerName not supplied");
+            logger.error("unsetInventoryShimInternalListener containerName not supplied");
             return;
         }
         if ((this.inventoryShimInternalListeners != null)
-                && this.inventoryShimInternalListeners.containsKey(s)) {
+                && this.inventoryShimInternalListeners.get(containerName) != null
+                && this.inventoryShimInternalListeners.get(containerName)
+                        .equals(s)) {
             this.inventoryShimInternalListeners.remove(containerName);
-            logger
-                    .trace("Removed inventoryShimInternalListener for container: "
-                            + containerName);
+            logger.trace(
+                    "Removed inventoryShimInternalListener for container {}",
+                    containerName);
         }
     }
 
@@ -152,8 +144,7 @@ public class InventoryServiceShim implements IContainerListener,
     }
 
     /**
-     * Function called after registering the
-     * service in OSGi service registry.
+     * Function called after registering the service in OSGi service registry.
      */
     void started() {
         /* Start with existing switches */
@@ -161,9 +152,9 @@ public class InventoryServiceShim implements IContainerListener,
     }
 
     /**
-     * Function called by the dependency manager when at least one
-     * dependency become unsatisfied or when the component is shutting
-     * down because for example bundle is being stopped.
+     * Function called by the dependency manager when at least one dependency
+     * become unsatisfied or when the component is shutting down because for
+     * example bundle is being stopped.
      *
      */
     void destroy() {
@@ -182,14 +173,14 @@ public class InventoryServiceShim implements IContainerListener,
                 handlePortStatusMessage(sw, (OFPortStatus) msg);
             }
         } catch (ConstructionException e) {
-            e.printStackTrace();
+            logger.error("",e);
         }
         return;
     }
 
     protected void handlePortStatusMessage(ISwitch sw, OFPortStatus m)
             throws ConstructionException {
-        Node node = new Node(NodeIDType.OPENFLOW, sw.getId());
+        Node node = NodeCreator.createOFNode(sw.getId());
         NodeConnector nodeConnector = PortConverter.toNodeConnector(m.getDesc()
                 .getPortNumber(), node);
         UpdateType type = null;
@@ -202,6 +193,8 @@ public class InventoryServiceShim implements IContainerListener,
             type = UpdateType.CHANGED;
         }
 
+        logger.trace("handlePortStatusMessage {} type {}", nodeConnector, type);
+
         if (type != null) {
             // get node connector properties
             Set<Property> props = InventoryServiceHelper.OFPortToProps(m
@@ -212,15 +205,16 @@ public class InventoryServiceShim implements IContainerListener,
 
     @Override
     public void switchAdded(ISwitch sw) {
-        if (sw == null)
+        if (sw == null) {
             return;
+        }
 
         // Add all the nodeConnectors of this switch
         Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
                 .OFSwitchToProps(sw);
         for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps.entrySet()) {
-            notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED, entry
-                    .getValue());
+            notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED,
+                    entry.getValue());
         }
 
         // Add this node
@@ -229,8 +223,9 @@ public class InventoryServiceShim implements IContainerListener,
 
     @Override
     public void switchDeleted(ISwitch sw) {
-        if (sw == null)
+        if (sw == null) {
             return;
+        }
 
         removeNode(sw);
     }
@@ -243,6 +238,8 @@ public class InventoryServiceShim implements IContainerListener,
     @Override
     public void tagUpdated(String containerName, Node n, short oldTag,
             short newTag, UpdateType t) {
+        logger.debug("tagUpdated: {} type {} for container {}", new Object[] {
+                n, t, containerName });
     }
 
     @Override
@@ -253,6 +250,8 @@ 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;
@@ -290,6 +289,7 @@ public class InventoryServiceShim implements IContainerListener,
 
         // notify InventoryService
         notifyInventoryShimInternalListener(containerName, p, t, null);
+        notifyInventoryShimInternalListener(containerName, p.getNode(), t, null);
     }
 
     private void notifyInventoryShimExternalListener(Node node,
@@ -313,17 +313,19 @@ public class InventoryServiceShim implements IContainerListener,
         if (inventoryShimInternalListener != null) {
             inventoryShimInternalListener.updateNodeConnector(nodeConnector,
                     type, props);
-            logger.trace(type + " " + nodeConnector + " on container "
-                    + container);
+            logger.trace(
+                    "notifyInventoryShimInternalListener {} type {} for container {}",
+                    new Object[] { nodeConnector, type, container });
         }
     }
 
     /*
-     *  Notify all internal and external listeners
+     * 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.
+        // Always notify default InventoryService. Store properties in default
+        // one.
         notifyInventoryShimInternalListener(GlobalConstants.DEFAULT.toString(),
                 nodeConnector, type, props);
 
@@ -342,7 +344,7 @@ public class InventoryServiceShim implements IContainerListener,
     }
 
     /*
-     *  Notify all internal and external listeners
+     * Notify all internal and external listeners
      */
     private void notifyInventoryShimListener(Node node, UpdateType type,
             Set<Property> props) {
@@ -362,6 +364,14 @@ public class InventoryServiceShim implements IContainerListener,
                 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);
+            }
+            break;
         default:
             break;
         }
@@ -370,15 +380,20 @@ public class InventoryServiceShim implements IContainerListener,
         notifyInventoryShimExternalListener(node, type, props);
     }
 
-    private void addNode(ISwitch sw) {
-        Node node;
-        try {
-            node = new Node(NodeIDType.OPENFLOW, sw.getId());
-        } catch (ConstructionException e) {
-            logger.error("{}", e.getMessage());
-            return;
+    private void notifyInventoryShimInternalListener(String container,
+            Node node, UpdateType type, Set<Property> props) {
+        IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
+                .get(container);
+        if (inventoryShimInternalListener != null) {
+            inventoryShimInternalListener.updateNode(node, type, props);
+            logger.trace(
+                    "notifyInventoryShimInternalListener {} type {} for container {}",
+                    new Object[] { node, type, container });
         }
+    }
 
+    private void addNode(ISwitch sw) {
+        Node node = NodeCreator.createOFNode(sw.getId());
         UpdateType type = UpdateType.ADDED;
 
         Set<Property> props = new HashSet<Property>();
@@ -389,37 +404,29 @@ public class InventoryServiceShim implements IContainerListener,
         Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
                 .getTime();
         props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
+        props.add(new MacAddress(deriveMacAddress(sid)));
 
-        String name = "";
-        if (statsMgr != null && statsMgr.getOFDescStatistics(sid) != null) {
-            List<OFStatistics> stats = statsMgr.getOFDescStatistics(sid);
-            if (stats.size() > 0) {
-                name = ((OFDescriptionStatistics) stats.get(0))
-                        .getSerialNumber();
-            }
-        }
-        props.add(new Name(name));
-        
         byte tables = sw.getTables();
         Tables t = new Tables(tables);
         if (t != null) {
-               props.add(t);
+            props.add(t);
         }
         int cap = sw.getCapabilities();
         Capabilities c = new Capabilities(cap);
         if (c != null) {
-               props.add(c);
+            props.add(c);
         }
         int act = sw.getActions();
         Actions a = new Actions(act);
         if (a != null) {
-               props.add(a);
+            props.add(a);
         }
         int buffers = sw.getBuffers();
         Buffers b = new Buffers(buffers);
         if (b != null) {
-               props.add(b);
+            props.add(b);
         }
+
         // Notify all internal and external listeners
         notifyInventoryShimListener(node, type, props);
     }
@@ -446,4 +453,42 @@ public class InventoryServiceShim implements IContainerListener,
             switchAdded(sw);
         }
     }
+
+    @Override
+    public void descriptionStatisticsRefreshed(Long switchId, List<OFStatistics> descriptionStats) {
+        Node node = NodeCreator.createOFNode(switchId);
+        Set<Property> properties = new HashSet<Property>(1);
+        OFDescriptionStatistics ofDesc = (OFDescriptionStatistics) descriptionStats.get(0);
+        Description desc = new Description(ofDesc.getDatapathDescription());
+        properties.add(desc);
+
+        // Notify all internal and external listeners
+        notifyInventoryShimListener(node, UpdateType.CHANGED, properties);
+    }
+
+    private byte[] deriveMacAddress(long dpid) {
+        byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+
+        for (short i = 0; i < 6; i++) {
+            mac[5 - i] = (byte) dpid;
+            dpid >>= 8;
+        }
+
+        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
+    }
 }