OpenFlow Protocol_plugin changes to make use of the Connection Manager infrastructure...
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / InventoryService.java
index 8af9b3ea058d894bf544c960bedf019256924721..9fded15f42383a1edceb436f5c8b010909a6e03b 100644 (file)
@@ -8,8 +8,6 @@
 
 package org.opendaylight.controller.protocol_plugin.openflow.internal;
 
-import java.util.Collections;
-import java.util.Date;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -17,21 +15,17 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CopyOnWriteArraySet;
 
 import org.apache.felix.dm.Component;
+import org.opendaylight.controller.protocol_plugin.openflow.IInventoryProvider;
 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimInternalListener;
 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
-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.connection.IPluginOutConnectionService;
 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.inventory.IPluginInInventoryService;
 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
@@ -44,19 +38,19 @@ import org.slf4j.LoggerFactory;
  * container of the network. Each instance gets container specific inventory
  * events from InventoryServiceShim. It interacts with SAL to pass inventory
  * data to the upper application.
- * 
- * 
+ *
+ *
  */
 public class InventoryService implements IInventoryShimInternalListener,
-        IPluginInInventoryService {
+        IPluginInInventoryService, IInventoryProvider {
     protected static final Logger logger = LoggerFactory
             .getLogger(InventoryService.class);
-    private Set<IPluginOutInventoryService> pluginOutInventoryServices = Collections
-            .synchronizedSet(new HashSet<IPluginOutInventoryService>());
+    private Set<IPluginOutInventoryService> pluginOutInventoryServices;
     private IController controller = null;
     private ConcurrentMap<Node, Map<String, Property>> nodeProps; // properties are maintained in global container only
     private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in global container only
     private boolean isDefaultContainer = false;
+    private String containerName = null;
 
     void setController(IController s) {
         this.controller = s;
@@ -71,7 +65,7 @@ public class InventoryService implements IInventoryShimInternalListener,
     /**
      * Function called by the dependency manager when all the required
      * dependencies are satisfied
-     * 
+     *
      */
     @SuppressWarnings("rawtypes")
     void init(Component c) {
@@ -79,20 +73,23 @@ public class InventoryService implements IInventoryShimInternalListener,
 
         Dictionary props = c.getServiceProperties();
         if (props != null) {
-            String containerName = (String) props.get("containerName");
-            isDefaultContainer = containerName.equals(GlobalConstants.DEFAULT
-                    .toString());
+            containerName = (String) props.get("containerName");
+            if (containerName != null) {
+                isDefaultContainer = containerName.equals(GlobalConstants.DEFAULT
+                        .toString());
+            }
         }
 
         nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
         nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
+        pluginOutInventoryServices = new CopyOnWriteArraySet<IPluginOutInventoryService>();
     }
 
     /**
      * 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() {
         logger.trace("DESTROY called!");
@@ -101,7 +98,7 @@ public class InventoryService implements IInventoryShimInternalListener,
     /**
      * Function called by dependency manager after "init ()" is called and after
      * the services provided by the class are registered in the service registry
-     * 
+     *
      */
     void start() {
         logger.trace("START called!");
@@ -111,7 +108,7 @@ public class InventoryService implements IInventoryShimInternalListener,
      * Function called by the dependency manager before the services exported by
      * the component are unregistered, this will be followed by a "destroy ()"
      * calls
-     * 
+     *
      */
     void stop() {
         logger.trace("STOP called!");
@@ -132,62 +129,12 @@ public class InventoryService implements IInventoryShimInternalListener,
         }
     }
 
-    protected Node OFSwitchToNode(ISwitch sw) {
-        Node node = null;
-        Object id = sw.getId();
-
-        try {
-            node = new Node(NodeIDType.OPENFLOW, id);
-        } catch (ConstructionException e) {
-            logger.error("", e);
-        }
-
-        return node;
-    }
-
     /**
      * Retrieve nodes from openflow
      */
     @Override
     public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
-        if (nodeProps == null)
-            return null;
-        Map<Long, ISwitch> switches = controller.getSwitches();
-        for (Map.Entry<Long, ISwitch> entry : switches.entrySet()) {
-            ISwitch sw = entry.getValue();
-            Node node = OFSwitchToNode(sw);
-            Map<String, Property> propMap = null;
-            if (isDefaultContainer) {
-                propMap = new HashMap<String, Property>();
-                byte tables = sw.getTables();
-                Tables t = new Tables(tables);
-                if (t != null) {
-                    propMap.put(Tables.TablesPropName, t);
-                }
-                int cap = sw.getCapabilities();
-                Capabilities c = new Capabilities(cap);
-                if (c != null) {
-                    propMap.put(Capabilities.CapabilitiesPropName, c);
-                }
-                int act = sw.getActions();
-                Actions a = new Actions(act);
-                if (a != null) {
-                    propMap.put(Actions.ActionsPropName, a);
-                }
-                int buffers = sw.getBuffers();
-                Buffers b = new Buffers(buffers);
-                if (b != null) {
-                    propMap.put(Buffers.BuffersPropName, b);
-                }
-                Date connectedSince = sw.getConnectedDate();
-                Long connectedSinceTime = (connectedSince == null) ? 0
-                        : connectedSince.getTime();
-                TimeStamp timeStamp = new TimeStamp(connectedSinceTime,
-                        "connectedSince");
-                propMap.put(TimeStamp.TimeStampPropName, timeStamp);
-                nodeProps.put(node, propMap);
-            }
-        }
+        logger.debug("getNodePros for container {}", containerName);
         return nodeProps;
     }
 
@@ -246,31 +193,36 @@ public class InventoryService implements IInventoryShimInternalListener,
         }
 
         // update sal and discovery
-        synchronized (pluginOutInventoryServices) {
-            for (IPluginOutInventoryService service : pluginOutInventoryServices) {
-                service.updateNodeConnector(nodeConnector, type, props);
-            }
+        for (IPluginOutInventoryService service : pluginOutInventoryServices) {
+            service.updateNodeConnector(nodeConnector, type, props);
         }
+
     }
 
     private void addNode(Node node, Set<Property> props) {
-        logger.trace("{} added, props: {}", node, props);
         if (nodeProps == null) {
             return;
         }
 
+        logger.trace("addNode: {} added, props: {} for container {}",
+                new Object[] { node, props, containerName });
+
         // update local cache
-        Map<String, Property> propMap = new HashMap<String, Property>();
-        for (Property prop : props) {
-            propMap.put(prop.getName(), prop);
+        Map<String, Property> propMap = nodeProps.get(node);
+        if (propMap == null) {
+            propMap = new HashMap<String, Property>();
+        }
+
+        if (props != null) {
+            for (Property prop : props) {
+                propMap.put(prop.getName(), prop);
+            }
         }
         nodeProps.put(node, propMap);
 
         // update sal
-        synchronized (pluginOutInventoryServices) {
-            for (IPluginOutInventoryService service : pluginOutInventoryServices) {
-                service.updateNode(node, UpdateType.ADDED, props);
-            }
+        for (IPluginOutInventoryService service : pluginOutInventoryServices) {
+            service.updateNode(node, UpdateType.ADDED, props);
         }
     }
 
@@ -293,13 +245,11 @@ public class InventoryService implements IInventoryShimInternalListener,
         }
 
         // update sal
-        synchronized (pluginOutInventoryServices) {
-            for (IPluginOutInventoryService service : pluginOutInventoryServices) {
-                service.updateNode(node, UpdateType.REMOVED, null);
-            }
+        for (IPluginOutInventoryService service : pluginOutInventoryServices) {
+            service.updateNode(node, UpdateType.REMOVED, null);
         }
     }
-    
+
     private void updateNode(Node node, Set<Property> properties) {
         logger.trace("{} updated, props: {}", node, properties);
         if (nodeProps == null || !nodeProps.containsKey(node) ||
@@ -315,16 +265,14 @@ public class InventoryService implements IInventoryShimInternalListener,
             Property currentProperty = propertyMap.get(name);
             if (!property.equals(currentProperty)) {
                 propertyMap.put(name, property);
-                newProperties.add(property);                
+                newProperties.add(property);
             }
         }
 
         // Update SAL if we got new properties
         if (!newProperties.isEmpty()) {
-            synchronized (pluginOutInventoryServices) {
-                for (IPluginOutInventoryService service : pluginOutInventoryServices) {
-                    service.updateNode(node, UpdateType.CHANGED, newProperties);
-                }
+            for (IPluginOutInventoryService service : pluginOutInventoryServices) {
+                service.updateNode(node, UpdateType.CHANGED, newProperties);
             }
         }
     }
@@ -345,5 +293,4 @@ public class InventoryService implements IInventoryShimInternalListener,
             break;
         }
     }
-
 }