X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fprotocol_plugins%2Fopenflow%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fprotocol_plugin%2Fopenflow%2Finternal%2FTopologyServiceShim.java;h=1d7b4263e7b191cc13e83666130464442b66b5f2;hb=refs%2Fchanges%2F17%2F417%2F1;hp=a811f6529e99546300b9809e9fdb95aa667d9ddb;hpb=a1f02e35be4dff06e523d3aeb05a7b9e1ca91a07;p=controller.git diff --git a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/TopologyServiceShim.java b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/TopologyServiceShim.java index a811f6529e..1d7b4263e7 100644 --- a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/TopologyServiceShim.java +++ b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/TopologyServiceShim.java @@ -25,6 +25,8 @@ import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import org.eclipse.osgi.framework.console.CommandInterpreter; import org.eclipse.osgi.framework.console.CommandProvider; +import org.opendaylight.controller.protocol_plugin.openflow.IDiscoveryListener; +import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener; import org.opendaylight.controller.protocol_plugin.openflow.IOFStatisticsManager; import org.opendaylight.controller.protocol_plugin.openflow.IRefreshInternalProvider; import org.opendaylight.controller.protocol_plugin.openflow.ITopologyServiceShimListener; @@ -34,14 +36,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.opendaylight.controller.sal.core.Bandwidth; +import org.opendaylight.controller.sal.core.Config; import org.opendaylight.controller.sal.core.ContainerFlow; import org.opendaylight.controller.sal.core.Edge; import org.opendaylight.controller.sal.core.IContainerListener; import org.opendaylight.controller.sal.core.Node; import org.opendaylight.controller.sal.core.NodeConnector; import org.opendaylight.controller.sal.core.Property; +import org.opendaylight.controller.sal.core.State; import org.opendaylight.controller.sal.core.UpdateType; -import org.opendaylight.controller.sal.discovery.IDiscoveryService; +import org.opendaylight.controller.sal.topology.TopoEdgeUpdate; import org.opendaylight.controller.sal.utils.GlobalConstants; /** @@ -49,8 +53,9 @@ import org.opendaylight.controller.sal.utils.GlobalConstants; * OpenFlow core to various listeners. The notifications are filtered based on * container configurations. */ -public class TopologyServiceShim implements IDiscoveryService, - IContainerListener, CommandProvider, IRefreshInternalProvider { +public class TopologyServiceShim implements IDiscoveryListener, + IContainerListener, CommandProvider, IRefreshInternalProvider, + IInventoryShimExternalListener { protected static final Logger logger = LoggerFactory .getLogger(TopologyServiceShim.class); private ConcurrentMap topologyServiceShimListeners = new ConcurrentHashMap(); @@ -73,19 +78,31 @@ public class TopologyServiceShim implements IDiscoveryService, class NotifyEntry { String container; - Pair> edgeProps; - UpdateType type; + List teuList; - NotifyEntry(String container, Pair> edgeProps, - UpdateType type) { + public NotifyEntry(String container, TopoEdgeUpdate teu) { this.container = container; - this.edgeProps = edgeProps; - this.type = type; + this.teuList = new ArrayList(); + if (teu != null) { + this.teuList.add(teu); + } + } + + public NotifyEntry(String container, List teuList) { + this.container = container; + this.teuList = new ArrayList(); + if (teuList != null) { + this.teuList.addAll(teuList); + } } } class TopologyNotify implements Runnable { private final BlockingQueue notifyQ; + private NotifyEntry entry; + private Map> teuMap = new HashMap>(); + private List teuList; + private boolean notifyListeners; TopologyNotify(BlockingQueue notifyQ) { this.notifyQ = notifyQ; @@ -94,22 +111,37 @@ public class TopologyServiceShim implements IDiscoveryService, public void run() { while (true) { try { - NotifyEntry entry = notifyQ.take(); + teuMap.clear(); + notifyListeners = false; + while (!notifyQ.isEmpty()) { + entry = notifyQ.take(); + teuList = teuMap.get(entry.container); + if (teuList == null) { + teuList = new ArrayList(); + } + // group all the updates together + teuList.addAll(entry.teuList); + teuMap.put(entry.container, teuList); + notifyListeners = true; + } - ITopologyServiceShimListener topologServiceShimListener = topologyServiceShimListeners - .get(entry.container); - topologServiceShimListener.edgeUpdate( - entry.edgeProps.getLeft(), entry.type, - entry.edgeProps.getRight()); + if (notifyListeners) { + for (String container : teuMap.keySet()) { + // notify the listener + topologyServiceShimListeners.get(container) + .edgeUpdate(teuMap.get(container)); + } + } - entry = null; + Thread.sleep(100); } catch (InterruptedException e1) { - logger.warn("TopologyNotify interrupted", e1.getMessage()); + logger.warn("TopologyNotify interrupted {}", + e1.getMessage()); if (shuttingDown) { return; } } catch (Exception e2) { - e2.printStackTrace(); + logger.error("", e2); } } } @@ -156,13 +188,13 @@ public class TopologyServiceShim implements IDiscoveryService, } } catch (InterruptedException e1) { logger.warn( - "Edge Bandwidth Utilization Notify Thread interrupted", + "Edge Bandwidth Utilization Notify Thread interrupted {}", e1.getMessage()); if (shuttingDown) { return; } } catch (Exception e2) { - e2.printStackTrace(); + logger.error("", e2); } } } @@ -171,7 +203,7 @@ public class TopologyServiceShim implements IDiscoveryService, /** * Function called by the dependency manager when all the required * dependencies are satisfied - * + * */ void init() { logger.trace("Init called"); @@ -281,7 +313,7 @@ public class TopologyServiceShim implements IDiscoveryService, * 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!"); @@ -292,7 +324,7 @@ public class TopologyServiceShim implements IDiscoveryService, /** * 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!"); @@ -306,7 +338,7 @@ public class TopologyServiceShim implements IDiscoveryService, * 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!"); @@ -329,8 +361,8 @@ public class TopologyServiceShim implements IDiscoveryService, && !this.topologyServiceShimListeners .containsKey(containerName)) { this.topologyServiceShimListeners.put(containerName, s); - logger.trace("Added topologyServiceShimListener for container:" - + containerName); + logger.trace("Added topologyServiceShimListener for container: {}", + containerName); } } @@ -350,8 +382,9 @@ public class TopologyServiceShim implements IDiscoveryService, && this.topologyServiceShimListeners.get(containerName).equals( s)) { this.topologyServiceShimListeners.remove(containerName); - logger.trace("Removed topologyServiceShimListener for container: " - + containerName); + logger.trace( + "Removed topologyServiceShimListener for container: {}", + containerName); } } @@ -367,6 +400,7 @@ public class TopologyServiceShim implements IDiscoveryService, private void removeNodeConnector(String container, NodeConnector nodeConnector) { + List teuList = new ArrayList(); Map>> edgePropsMap = edgeMap .get(container); if (edgePropsMap == null) { @@ -378,7 +412,8 @@ public class TopologyServiceShim implements IDiscoveryService, if (edgeProps == null) { return; } - notifyEdge(container, edgeProps.getLeft(), UpdateType.REMOVED, null); + teuList.add(new TopoEdgeUpdate(edgeProps.getLeft(), null, + UpdateType.REMOVED)); // Remove edge in another direction edgeProps = edgePropsMap @@ -386,51 +421,130 @@ public class TopologyServiceShim implements IDiscoveryService, if (edgeProps == null) { return; } - notifyEdge(container, edgeProps.getLeft(), UpdateType.REMOVED, null); + teuList.add(new TopoEdgeUpdate(edgeProps.getLeft(), null, + UpdateType.REMOVED)); + + // Update in one shot + notifyEdge(container, teuList); } - private void notifyEdge(String container, Edge edge, UpdateType type, - Set props) { + /** + * Update local cache and return true if it needs to notify upper layer + * Topology listeners. + * + * @param container + * The network container + * @param edge + * The edge + * @param type + * The update type + * @param props + * The edge properties + * @return true if it needs to notify upper layer Topology listeners + */ + private boolean updateLocalEdgeMap(String container, Edge edge, + UpdateType type, Set props) { ConcurrentMap>> edgePropsMap = edgeMap .get(container); NodeConnector src = edge.getTailNodeConnector(); Pair> edgeProps = new ImmutablePair>( edge, props); + boolean rv = false; switch (type) { case ADDED: case CHANGED: if (edgePropsMap == null) { edgePropsMap = new ConcurrentHashMap>>(); + rv = true; } else { if (edgePropsMap.containsKey(src) && edgePropsMap.get(src).equals(edgeProps)) { - // Entry already exists. Return here. - return; + // Entry already exists. No update. + rv = false; + } else { + rv = true; } } - edgePropsMap.put(src, edgeProps); - edgeMap.put(container, edgePropsMap); + if (rv) { + edgePropsMap.put(src, edgeProps); + edgeMap.put(container, edgePropsMap); + } break; case REMOVED: - if (edgePropsMap != null) { + if ((edgePropsMap != null) && edgePropsMap.containsKey(src)) { edgePropsMap.remove(src); if (edgePropsMap.isEmpty()) { edgeMap.remove(container); } else { edgeMap.put(container, edgePropsMap); } + rv = true; } break; default: - logger.debug("Invalid " + type + " Edge " + edge - + " in container {}", container); + logger.debug( + "notifyLocalEdgeMap: invalid {} for Edge {} in container {}", + new Object[] { type.getName(), edge, container }); + } + + if (rv) { + logger.debug( + "notifyLocalEdgeMap: {} for Edge {} in container {}", + new Object[] { type.getName(), edge, container }); + } + + return rv; + } + + private void notifyEdge(String container, Edge edge, UpdateType type, + Set props) { + boolean notifyListeners; + + // Update local cache + notifyListeners = updateLocalEdgeMap(container, edge, type, props); + + // Prepare to update TopologyService + if (notifyListeners) { + notifyQ.add(new NotifyEntry(container, new TopoEdgeUpdate(edge, props, + type))); + logger.debug("notifyEdge: {} Edge {} in container {}", + new Object[] { type.getName(), edge, container }); + } + } + + private void notifyEdge(String container, List etuList) { + if (etuList == null) { return; } - notifyQ.add(new NotifyEntry(container, edgeProps, type)); + Edge edge; + UpdateType type; + List etuNotifyList = new ArrayList(); + boolean notifyListeners = false, rv; + + for (TopoEdgeUpdate etu : etuList) { + edge = etu.getEdge(); + type = etu.getUpdateType(); + + // Update local cache + rv = updateLocalEdgeMap(container, edge, type, etu.getProperty()); + if (rv) { + if (!notifyListeners) { + notifyListeners = true; + } + etuNotifyList.add(etu); + logger.debug( + "notifyEdge(TopoEdgeUpdate): {} Edge {} in container {}", + new Object[] { type.getName(), edge, container }); + } + } - logger.trace(type + " Edge " + edge + " in container {}", container); + // Prepare to update TopologyService + if (notifyListeners) { + notifyQ.add(new NotifyEntry(container, etuNotifyList)); + logger.debug("notifyEdge(TopoEdgeUpdate): add notifyQ"); + } } @Override @@ -568,9 +682,12 @@ public class TopologyServiceShim implements IDiscoveryService, } long bw = 0; - for (Property prop : edgeProps.getRight()) { - if (prop.getName().equals(Bandwidth.BandwidthPropName)) { - bw = ((Bandwidth) prop).getValue(); + Set props = edgeProps.getRight(); + if (props != null) { + for (Property prop : props) { + if (prop.getName().equals(Bandwidth.BandwidthPropName)) { + bw = ((Bandwidth) prop).getValue(); + } } } count++; @@ -595,11 +712,11 @@ public class TopologyServiceShim implements IDiscoveryService, * pushes the updates to ALL the applications that have registered as * listeners for this service. SAL has no way of knowing which application * requested for the refresh. - * + * * As an example of this case, is stopping and starting the Topology * Manager. When the topology Manager is stopped, and restarted, it will no * longer have the latest topology. Hence, a request is sent here. - * + * * @param containerName * @return void */ @@ -614,7 +731,7 @@ public class TopologyServiceShim implements IDiscoveryService, * Reading the current topology database, the method will replay all the * edge updates for the ITopologyServiceShimListener instance in the given * container, which will in turn publish them toward SAL. - * + * * @param containerName */ private void TopologyBulkUpdate(String containerName) { @@ -634,15 +751,71 @@ public class TopologyServiceShim implements IDiscoveryService, return; } int i = 0; + List teuList = new ArrayList(); for (Pair> edgeProps : edgePropMap.values()) { if (edgeProps != null) { i++; + teuList.add(new TopoEdgeUpdate(edgeProps.getLeft(), edgeProps + .getRight(), UpdateType.ADDED)); logger.trace("Add edge {}", edgeProps.getLeft()); - topologServiceShimListener.edgeUpdate(edgeProps.getLeft(), - UpdateType.ADDED, edgeProps.getRight()); } } + if (i > 0) { + topologServiceShimListener.edgeUpdate(teuList); + } logger.debug("Sent {} updates", i); } + @Override + public void updateNode(Node node, UpdateType type, Set props) { + } + + @Override + public void updateNodeConnector(NodeConnector nodeConnector, + UpdateType type, Set props) { + List containers = new ArrayList(); + List conList = this.containerMap.get(nodeConnector); + + containers.add(GlobalConstants.DEFAULT.toString()); + if (conList != null) { + containers.addAll(conList); + } + + switch (type) { + case ADDED: + break; + case CHANGED: + if (props == null) { + break; + } + + boolean rmEdge = false; + for (Property prop : props) { + if (((prop instanceof Config) && (((Config) prop).getValue() != Config.ADMIN_UP)) + || ((prop instanceof State) && (((State) prop) + .getValue() != State.EDGE_UP))) { + /* + * If port admin down or link down, remove the edges + * associated with the port + */ + rmEdge = true; + break; + } + } + + if (rmEdge) { + for (String cName : containers) { + removeNodeConnector(cName, nodeConnector); + } + } + break; + case REMOVED: + for (String cName : containers) { + removeNodeConnector(cName, nodeConnector); + } + break; + default: + break; + } + } }