X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fprotocol_plugins%2Fopenflow%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fprotocol_plugin%2Fopenflow%2Finternal%2FInventoryService.java;h=a5a071122f62d6b0d0df9b45fb1f8b2775aa347e;hp=d00cd58c59b952b4c711ba9d215f745839e5e532;hb=b6d3e684e8ffc5125dc71a619ac41323924a32c5;hpb=d192c699590d441eb96a697b9e8ab7a028f18860 diff --git a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/InventoryService.java b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/InventoryService.java index d00cd58c59..a5a071122f 100644 --- a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/InventoryService.java +++ b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/InventoryService.java @@ -1,4 +1,3 @@ - /* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * @@ -9,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; @@ -18,28 +15,21 @@ 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.IStatisticsListener; 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.core.Description; +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; import org.opendaylight.controller.sal.utils.GlobalConstants; -import org.openflow.protocol.statistics.OFDescriptionStatistics; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,15 +42,16 @@ import org.slf4j.LoggerFactory; * */ public class InventoryService implements IInventoryShimInternalListener, - IPluginInInventoryService, IStatisticsListener { + IPluginInInventoryService, IInventoryProvider { protected static final Logger logger = LoggerFactory .getLogger(InventoryService.class); - private Set pluginOutInventoryServices = Collections - .synchronizedSet(new HashSet()); + private Set pluginOutInventoryServices = + new CopyOnWriteArraySet(); private IController controller = null; - private ConcurrentMap> nodeProps; // properties are maintained in default container only - private ConcurrentMap> nodeConnectorProps; // properties are maintained in default container only + private ConcurrentMap> nodeProps; // properties are maintained in global container only + private ConcurrentMap> nodeConnectorProps; // properties are maintained in global container only private boolean isDefaultContainer = false; + private String containerName = null; void setController(IController s) { this.controller = s; @@ -83,9 +74,11 @@ 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>(); @@ -119,6 +112,7 @@ public class InventoryService implements IInventoryShimInternalListener, */ void stop() { logger.trace("STOP called!"); + pluginOutInventoryServices.clear(); } public void setPluginOutInventoryServices(IPluginOutInventoryService service) { @@ -136,70 +130,21 @@ 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> getNodeProps() { - if (nodeProps == null) - return null; - Map switches = controller.getSwitches(); - for (Map.Entry entry : switches.entrySet()) { - ISwitch sw = entry.getValue(); - Node node = OFSwitchToNode(sw); - Map propMap = null; - if (isDefaultContainer) { - propMap = new HashMap(); - 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; } @Override public ConcurrentMap> getNodeConnectorProps( Boolean refresh) { - if (nodeConnectorProps == null) + if (nodeConnectorProps == null) { return null; + } if (isDefaultContainer && refresh) { Map switches = controller.getSwitches(); @@ -208,8 +153,8 @@ public class InventoryService implements IInventoryShimInternalListener, .OFSwitchToProps(sw); for (Map.Entry> entry : ncProps .entrySet()) { - updateNodeConnector(entry.getKey(), UpdateType.ADDED, entry - .getValue()); + updateNodeConnector(entry.getKey(), UpdateType.ADDED, + entry.getValue()); } } } @@ -220,24 +165,20 @@ public class InventoryService implements IInventoryShimInternalListener, @Override public void updateNodeConnector(NodeConnector nodeConnector, UpdateType type, Set props) { - logger.trace("NodeConnector id " + nodeConnector.getID() - + " type " + nodeConnector.getType() + " " - + type.getName() + " for Node id " - + nodeConnector.getNode().getID()); - + logger.trace("updateNodeConnector {} type {}", nodeConnector, + type.getName()); if (nodeConnectorProps == null) { + logger.trace("nodeConnectorProps is null"); return; } - - Map propMap = nodeConnectorProps - .get(nodeConnector); + Map propMap = nodeConnectorProps.get(nodeConnector); switch (type) { case ADDED: case CHANGED: - if (propMap == null) + if (propMap == null) { propMap = new HashMap(); - + } if (props != null) { for (Property prop : props) { propMap.put(prop.getName(), prop); @@ -253,37 +194,44 @@ 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 props) { - logger.trace("{} added", node); - if (nodeProps == null) + if (nodeProps == null) { return; + } + + logger.trace("addNode: {} added, props: {} for container {}", + new Object[] { node, props, containerName }); // update local cache - Map propMap = new HashMap(); - for (Property prop : props) { - propMap.put(prop.getName(), prop); + Map propMap = nodeProps.get(node); + if (propMap == null) { + propMap = new HashMap(); + } + + 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); } } private void removeNode(Node node) { logger.trace("{} removed", node); - if (nodeProps == null) + if (nodeProps == null) { return; + } // update local cache nodeProps.remove(node); @@ -299,40 +247,35 @@ 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 updateSwitchProperty(Long switchId, Set propSet) { - // update local cache - Node node = OFSwitchToNode(controller.getSwitch(switchId)); - Map propMap = nodeProps.get(node); - if (propMap == null) { - propMap = new HashMap(); + private void updateNode(Node node, Set properties) { + logger.trace("{} updated, props: {}", node, properties); + if ((nodeProps == null) || !nodeProps.containsKey(node) || + (properties == null) || properties.isEmpty()) { + return; } - - boolean change = false; - for (Property prop : propSet) { - String propertyName = prop.getName(); - Property currentProp = propMap.get(propertyName); - if (!prop.equals(currentProp)) { - change = true; - propMap.put(propertyName, prop); - } + + // Update local cache with new properties + Set newProperties = new HashSet(properties.size()); + Map propertyMap = nodeProps.get(node); + for (Property property : properties) { + String name = property.getName(); + Property currentProperty = propertyMap.get(name); + if (!property.equals(currentProperty)) { + propertyMap.put(name, property); + newProperties.add(property); + } } - nodeProps.put(node, propMap); - // Update sal if any of the properties has changed - if (change) { - synchronized (pluginOutInventoryServices) { - for (IPluginOutInventoryService service : pluginOutInventoryServices) { - service.updateNode(node, UpdateType.CHANGED, propSet); - } - } + // Update SAL if we got new properties + if (!newProperties.isEmpty()) { + for (IPluginOutInventoryService service : pluginOutInventoryServices) { + service.updateNode(node, UpdateType.CHANGED, newProperties); + } } } @@ -345,19 +288,11 @@ public class InventoryService implements IInventoryShimInternalListener, case REMOVED: removeNode(node); break; + case CHANGED: + updateNode(node, props); + break; default: break; } } - - @Override - public void descriptionRefreshed(Long switchId, - OFDescriptionStatistics descriptionStats) { - - Set propSet = new HashSet(1); - Description desc = - new Description(descriptionStats.getDatapathDescription()); - propSet.add(desc); - this.updateSwitchProperty(switchId, propSet); - } }