-
/*
* Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
*
import java.util.concurrent.ConcurrentMap;
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.core.Node;
import org.opendaylight.controller.sal.core.Node.NodeIDType;
import org.opendaylight.controller.sal.core.NodeConnector;
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;
*
*/
public class InventoryService implements IInventoryShimInternalListener,
- IPluginInInventoryService, IStatisticsListener {
+ IPluginInInventoryService, IInventoryProvider {
protected static final Logger logger = LoggerFactory
.getLogger(InventoryService.class);
private Set<IPluginOutInventoryService> pluginOutInventoryServices = Collections
.synchronizedSet(new HashSet<IPluginOutInventoryService>());
private IController controller = null;
- private ConcurrentMap<Node, Map<String, Property>> nodeProps; // properties are maintained in default container only
- private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in default container only
+ 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;
void setController(IController s) {
try {
node = new Node(NodeIDType.OPENFLOW, id);
} catch (ConstructionException e) {
- e.printStackTrace();
+ logger.error("", e);
}
return node;
return null;
Map<Long, ISwitch> switches = controller.getSwitches();
for (Map.Entry<Long, ISwitch> entry : switches.entrySet()) {
- ISwitch sw = entry.getValue();
+ ISwitch sw = entry.getValue();
Node node = OFSwitchToNode(sw);
Map<String, Property> propMap = null;
if (isDefaultContainer) {
byte tables = sw.getTables();
Tables t = new Tables(tables);
if (t != null) {
- propMap.put(Tables.TablesPropName,t);
+ propMap.put(Tables.TablesPropName, t);
}
int cap = sw.getCapabilities();
Capabilities c = new Capabilities(cap);
if (c != null) {
- propMap.put(Capabilities.CapabilitiesPropName, c);
+ propMap.put(Capabilities.CapabilitiesPropName, c);
}
int act = sw.getActions();
Actions a = new Actions(act);
if (a != null) {
- propMap.put(Actions.ActionsPropName,a);
+ propMap.put(Actions.ActionsPropName, a);
}
int buffers = sw.getBuffers();
Buffers b = new Buffers(buffers);
if (b != null) {
- propMap.put(Buffers.BuffersPropName,b);
+ propMap.put(Buffers.BuffersPropName, b);
}
Date connectedSince = sw.getConnectedDate();
Long connectedSinceTime = (connectedSince == null) ? 0
@Override
public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
Boolean refresh) {
- if (nodeConnectorProps == null)
+ if (nodeConnectorProps == null) {
return null;
+ }
if (isDefaultContainer && refresh) {
Map<Long, ISwitch> switches = controller.getSwitches();
.OFSwitchToProps(sw);
for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps
.entrySet()) {
- updateNodeConnector(entry.getKey(), UpdateType.ADDED, entry
- .getValue());
+ updateNodeConnector(entry.getKey(), UpdateType.ADDED,
+ entry.getValue());
}
}
}
@Override
public void updateNodeConnector(NodeConnector nodeConnector,
UpdateType type, Set<Property> 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<String, Property> propMap = nodeConnectorProps
- .get(nodeConnector);
+ Map<String, Property> propMap = nodeConnectorProps.get(nodeConnector);
switch (type) {
case ADDED:
case CHANGED:
- if (propMap == null)
+ if (propMap == null) {
propMap = new HashMap<String, Property>();
-
+ }
if (props != null) {
for (Property prop : props) {
propMap.put(prop.getName(), prop);
}
private void addNode(Node node, Set<Property> props) {
- logger.trace("{} added", node);
- if (nodeProps == null)
+ logger.trace("{} added, props: {}", node, props);
+ if (nodeProps == null) {
return;
+ }
// update local cache
Map<String, Property> propMap = new HashMap<String, Property>();
}
}
-
- private void updateSwitchProperty(Long switchId, Set<Property> propSet) {
- // update local cache
- Node node = OFSwitchToNode(controller.getSwitch(switchId));
- Map<String, Property> propMap = nodeProps.get(node);
- if (propMap == null) {
- propMap = new HashMap<String, Property>();
+ private void updateNode(Node node, Set<Property> 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<Property> newProperties = new HashSet<Property>(properties.size());
+ Map<String, Property> 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()) {
+ synchronized (pluginOutInventoryServices) {
+ for (IPluginOutInventoryService service : pluginOutInventoryServices) {
+ service.updateNode(node, UpdateType.CHANGED, newProperties);
+ }
+ }
}
}
case REMOVED:
removeNode(node);
break;
+ case CHANGED:
+ updateNode(node, props);
+ break;
default:
break;
}
}
- @Override
- public void descriptionRefreshed(Long switchId,
- OFDescriptionStatistics descriptionStats) {
-
- Set<Property> propSet = new HashSet<Property>(1);
- Description desc =
- new Description(descriptionStats.getDatapathDescription());
- propSet.add(desc);
- this.updateSwitchProperty(switchId, propSet);
- }
}