2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.protocol_plugin.openflow.internal;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
16 import java.util.Timer;
17 import java.util.TimerTask;
18 import java.util.concurrent.BlockingQueue;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
21 import java.util.concurrent.CopyOnWriteArrayList;
22 import java.util.concurrent.LinkedBlockingQueue;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.apache.commons.lang3.tuple.Pair;
26 import org.eclipse.osgi.framework.console.CommandInterpreter;
27 import org.eclipse.osgi.framework.console.CommandProvider;
28 import org.opendaylight.controller.protocol_plugin.openflow.IDiscoveryListener;
29 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener;
30 import org.opendaylight.controller.protocol_plugin.openflow.IOFStatisticsManager;
31 import org.opendaylight.controller.protocol_plugin.openflow.IRefreshInternalProvider;
32 import org.opendaylight.controller.protocol_plugin.openflow.ITopologyServiceShimListener;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.FrameworkUtil;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import org.opendaylight.controller.sal.core.Bandwidth;
39 import org.opendaylight.controller.sal.core.Config;
40 import org.opendaylight.controller.sal.core.ContainerFlow;
41 import org.opendaylight.controller.sal.core.Edge;
42 import org.opendaylight.controller.sal.core.IContainerListener;
43 import org.opendaylight.controller.sal.core.Node;
44 import org.opendaylight.controller.sal.core.NodeConnector;
45 import org.opendaylight.controller.sal.core.Property;
46 import org.opendaylight.controller.sal.core.State;
47 import org.opendaylight.controller.sal.core.UpdateType;
48 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
49 import org.opendaylight.controller.sal.utils.GlobalConstants;
52 * The class describes a shim layer that relays the topology events from
53 * OpenFlow core to various listeners. The notifications are filtered based on
54 * container configurations.
56 public class TopologyServiceShim implements IDiscoveryListener,
57 IContainerListener, CommandProvider, IRefreshInternalProvider,
58 IInventoryShimExternalListener {
59 protected static final Logger logger = LoggerFactory
60 .getLogger(TopologyServiceShim.class);
61 private ConcurrentMap<String, ITopologyServiceShimListener> topologyServiceShimListeners = new ConcurrentHashMap<String, ITopologyServiceShimListener>();
62 private ConcurrentMap<NodeConnector, List<String>> containerMap = new ConcurrentHashMap<NodeConnector, List<String>>();
63 private ConcurrentMap<String, ConcurrentMap<NodeConnector, Pair<Edge, Set<Property>>>> edgeMap = new ConcurrentHashMap<String, ConcurrentMap<NodeConnector, Pair<Edge, Set<Property>>>>();
65 private BlockingQueue<NotifyEntry> notifyQ;
66 private Thread notifyThread;
67 private BlockingQueue<String> bulkNotifyQ;
68 private Thread ofPluginTopoBulkUpdate;
69 private volatile Boolean shuttingDown = false;
70 private IOFStatisticsManager statsMgr;
71 private Timer pollTimer;
72 private TimerTask txRatePoller;
73 private Thread bwUtilNotifyThread;
74 private BlockingQueue<UtilizationUpdate> bwUtilNotifyQ;
75 private List<NodeConnector> connectorsOverUtilized;
76 private float bwThresholdFactor = (float) 0.8; // Threshold = 80% of link
81 List<TopoEdgeUpdate> teuList;
83 public NotifyEntry(String container, TopoEdgeUpdate teu) {
84 this.container = container;
85 this.teuList = new ArrayList<TopoEdgeUpdate>();
87 this.teuList.add(teu);
91 public NotifyEntry(String container, List<TopoEdgeUpdate> teuList) {
92 this.container = container;
93 this.teuList = new ArrayList<TopoEdgeUpdate>();
94 if (teuList != null) {
95 this.teuList.addAll(teuList);
100 class TopologyNotify implements Runnable {
101 private final BlockingQueue<NotifyEntry> notifyQ;
102 private NotifyEntry entry;
103 private Map<String, List<TopoEdgeUpdate>> teuMap = new HashMap<String, List<TopoEdgeUpdate>>();
104 private List<TopoEdgeUpdate> teuList;
105 private boolean notifyListeners;
107 TopologyNotify(BlockingQueue<NotifyEntry> notifyQ) {
108 this.notifyQ = notifyQ;
116 notifyListeners = false;
117 while (!notifyQ.isEmpty()) {
118 entry = notifyQ.take();
119 teuList = teuMap.get(entry.container);
120 if (teuList == null) {
121 teuList = new ArrayList<TopoEdgeUpdate>();
123 // group all the updates together
124 teuList.addAll(entry.teuList);
125 teuMap.put(entry.container, teuList);
126 notifyListeners = true;
129 if (notifyListeners) {
130 for (String container : teuMap.keySet()) {
131 // notify the listener
132 topologyServiceShimListeners.get(container)
133 .edgeUpdate(teuMap.get(container));
138 } catch (InterruptedException e1) {
139 logger.warn("TopologyNotify interrupted {}",
144 } catch (Exception e2) {
145 logger.error("", e2);
151 class UtilizationUpdate {
152 NodeConnector connector;
155 UtilizationUpdate(NodeConnector connector, UpdateType type) {
156 this.connector = connector;
161 class BwUtilizationNotify implements Runnable {
162 private final BlockingQueue<UtilizationUpdate> notifyQ;
164 BwUtilizationNotify(BlockingQueue<UtilizationUpdate> notifyQ) {
165 this.notifyQ = notifyQ;
172 UtilizationUpdate update = notifyQ.take();
173 NodeConnector connector = update.connector;
174 Set<String> containerList = edgeMap.keySet();
175 for (String container : containerList) {
176 Map<NodeConnector, Pair<Edge, Set<Property>>> edgePropsMap = edgeMap
178 Edge edge = edgePropsMap.get(connector).getLeft();
179 if (edge.getTailNodeConnector().equals(connector)) {
180 ITopologyServiceShimListener topologServiceShimListener = topologyServiceShimListeners
182 if (update.type == UpdateType.ADDED) {
183 topologServiceShimListener
184 .edgeOverUtilized(edge);
186 topologServiceShimListener
187 .edgeUtilBackToNormal(edge);
191 } catch (InterruptedException e1) {
193 "Edge Bandwidth Utilization Notify Thread interrupted {}",
198 } catch (Exception e2) {
199 logger.error("", e2);
206 * Function called by the dependency manager when all the required
207 * dependencies are satisfied
211 logger.trace("Init called");
212 connectorsOverUtilized = new ArrayList<NodeConnector>();
213 notifyQ = new LinkedBlockingQueue<NotifyEntry>();
214 notifyThread = new Thread(new TopologyNotify(notifyQ));
215 bwUtilNotifyQ = new LinkedBlockingQueue<UtilizationUpdate>();
216 bwUtilNotifyThread = new Thread(new BwUtilizationNotify(bwUtilNotifyQ));
217 bulkNotifyQ = new LinkedBlockingQueue<String>();
218 ofPluginTopoBulkUpdate = new Thread(new Runnable() {
223 String containerName = bulkNotifyQ.take();
224 logger.debug("Bulk Notify container:{}", containerName);
225 TopologyBulkUpdate(containerName);
226 } catch (InterruptedException e) {
227 logger.warn("Topology Bulk update thread interrupted");
234 }, "Topology Bulk Update");
236 // Initialize node connector tx bit rate poller timer
237 pollTimer = new Timer();
238 txRatePoller = new TimerTask() {
245 registerWithOSGIConsole();
249 * Continuously polls the transmit bit rate for all the node connectors from
250 * statistics manager and trigger the warning notification upward when the
251 * transmit rate is above a threshold which is a percentage of the edge
254 protected void pollTxBitRates() {
255 Map<NodeConnector, Pair<Edge, Set<Property>>> globalContainerEdges = edgeMap
256 .get(GlobalConstants.DEFAULT.toString());
257 if (globalContainerEdges == null) {
261 for (NodeConnector connector : globalContainerEdges.keySet()) {
262 // Skip if node connector belongs to production switch
263 if (connector.getType().equals(
264 NodeConnector.NodeConnectorIDType.PRODUCTION)) {
268 // Get edge for which this node connector is head
269 Pair<Edge, Set<Property>> props = this.edgeMap.get(
270 GlobalConstants.DEFAULT.toString()).get(connector);
271 // On switch mgr restart the props get reset
275 Set<Property> propSet = props.getRight();
276 if (propSet == null) {
281 for (Property prop : propSet) {
282 if (prop instanceof Bandwidth) {
283 bw = ((Bandwidth) prop).getValue();
288 // Skip if agent did not provide a bandwidth info for the edge
293 // Compare bandwidth usage
294 Long switchId = (Long) connector.getNode().getID();
295 Short port = (Short) connector.getID();
296 float rate = statsMgr.getTransmitRate(switchId, port);
297 if (rate > bwThresholdFactor * bw) {
298 if (!connectorsOverUtilized.contains(connector)) {
299 connectorsOverUtilized.add(connector);
300 this.bwUtilNotifyQ.add(new UtilizationUpdate(connector,
304 if (connectorsOverUtilized.contains(connector)) {
305 connectorsOverUtilized.remove(connector);
306 this.bwUtilNotifyQ.add(new UtilizationUpdate(connector,
307 UpdateType.REMOVED));
315 * Function called by the dependency manager when at least one dependency
316 * become unsatisfied or when the component is shutting down because for
317 * example bundle is being stopped.
321 logger.trace("DESTROY called!");
327 * Function called by dependency manager after "init ()" is called and after
328 * the services provided by the class are registered in the service registry
332 logger.trace("START called!");
333 notifyThread.start();
334 bwUtilNotifyThread.start();
335 ofPluginTopoBulkUpdate.start();
336 pollTimer.scheduleAtFixedRate(txRatePoller, 10000, 5000);
340 * Function called by the dependency manager before the services exported by
341 * the component are unregistered, this will be followed by a "destroy ()"
346 logger.trace("STOP called!");
348 notifyThread.interrupt();
351 void setTopologyServiceShimListener(Map<?, ?> props,
352 ITopologyServiceShimListener s) {
354 logger.error("Didn't receive the service properties");
357 String containerName = (String) props.get("containerName");
358 if (containerName == null) {
359 logger.error("containerName not supplied");
362 if ((this.topologyServiceShimListeners != null)
363 && !this.topologyServiceShimListeners
364 .containsKey(containerName)) {
365 this.topologyServiceShimListeners.put(containerName, s);
366 logger.trace("Added topologyServiceShimListener for container: {}",
371 void unsetTopologyServiceShimListener(Map<?, ?> props,
372 ITopologyServiceShimListener s) {
374 logger.error("Didn't receive the service properties");
377 String containerName = (String) props.get("containerName");
378 if (containerName == null) {
379 logger.error("containerName not supplied");
382 if ((this.topologyServiceShimListeners != null)
383 && this.topologyServiceShimListeners.containsKey(containerName)
384 && this.topologyServiceShimListeners.get(containerName).equals(
386 this.topologyServiceShimListeners.remove(containerName);
388 "Removed topologyServiceShimListener for container: {}",
393 void setStatisticsManager(IOFStatisticsManager s) {
397 void unsetStatisticsManager(IOFStatisticsManager s) {
398 if (this.statsMgr == s) {
399 this.statsMgr = null;
403 private void removeNodeConnector(String container,
404 NodeConnector nodeConnector) {
405 List<TopoEdgeUpdate> teuList = new ArrayList<TopoEdgeUpdate>();
406 Map<NodeConnector, Pair<Edge, Set<Property>>> edgePropsMap = edgeMap
408 if (edgePropsMap == null) {
412 // Remove edge in one direction
413 Pair<Edge, Set<Property>> edgeProps = edgePropsMap.get(nodeConnector);
414 if (edgeProps == null) {
417 teuList.add(new TopoEdgeUpdate(edgeProps.getLeft(), null,
418 UpdateType.REMOVED));
420 // Remove edge in another direction
421 edgeProps = edgePropsMap
422 .get(edgeProps.getLeft().getHeadNodeConnector());
423 if (edgeProps == null) {
426 teuList.add(new TopoEdgeUpdate(edgeProps.getLeft(), null,
427 UpdateType.REMOVED));
429 // Update in one shot
430 notifyEdge(container, teuList);
434 * Update local cache and return true if it needs to notify upper layer
435 * Topology listeners.
438 * The network container
444 * The edge properties
445 * @return true if it needs to notify upper layer Topology listeners
447 private boolean updateLocalEdgeMap(String container, Edge edge,
448 UpdateType type, Set<Property> props) {
449 ConcurrentMap<NodeConnector, Pair<Edge, Set<Property>>> edgePropsMap = edgeMap
451 NodeConnector src = edge.getTailNodeConnector();
452 Pair<Edge, Set<Property>> edgeProps = new ImmutablePair<Edge, Set<Property>>(
459 if (edgePropsMap == null) {
460 edgePropsMap = new ConcurrentHashMap<NodeConnector, Pair<Edge, Set<Property>>>();
463 if (edgePropsMap.containsKey(src)
464 && edgePropsMap.get(src).equals(edgeProps)) {
465 // Entry already exists. No update.
472 edgePropsMap.put(src, edgeProps);
473 edgeMap.put(container, edgePropsMap);
477 if ((edgePropsMap != null) && edgePropsMap.containsKey(src)) {
478 edgePropsMap.remove(src);
479 if (edgePropsMap.isEmpty()) {
480 edgeMap.remove(container);
482 edgeMap.put(container, edgePropsMap);
489 "notifyLocalEdgeMap: invalid {} for Edge {} in container {}",
490 new Object[] { type.getName(), edge, container });
495 "notifyLocalEdgeMap: {} for Edge {} in container {}",
496 new Object[] { type.getName(), edge, container });
502 private void notifyEdge(String container, Edge edge, UpdateType type,
503 Set<Property> props) {
504 boolean notifyListeners;
506 // Update local cache
507 notifyListeners = updateLocalEdgeMap(container, edge, type, props);
509 // Prepare to update TopologyService
510 if (notifyListeners) {
511 notifyQ.add(new NotifyEntry(container, new TopoEdgeUpdate(edge, props,
513 logger.debug("notifyEdge: {} Edge {} in container {}",
514 new Object[] { type.getName(), edge, container });
518 private void notifyEdge(String container, List<TopoEdgeUpdate> etuList) {
519 if (etuList == null) {
525 List<TopoEdgeUpdate> etuNotifyList = new ArrayList<TopoEdgeUpdate>();
526 boolean notifyListeners = false, rv;
528 for (TopoEdgeUpdate etu : etuList) {
529 edge = etu.getEdge();
530 type = etu.getUpdateType();
532 // Update local cache
533 rv = updateLocalEdgeMap(container, edge, type, etu.getProperty());
535 if (!notifyListeners) {
536 notifyListeners = true;
538 etuNotifyList.add(etu);
540 "notifyEdge(TopoEdgeUpdate): {} Edge {} in container {}",
541 new Object[] { type.getName(), edge, container });
545 // Prepare to update TopologyService
546 if (notifyListeners) {
547 notifyQ.add(new NotifyEntry(container, etuNotifyList));
548 logger.debug("notifyEdge(TopoEdgeUpdate): add notifyQ");
553 public void notifyEdge(Edge edge, UpdateType type, Set<Property> props) {
554 if ((edge == null) || (type == null)) {
558 // Notify default container
559 notifyEdge(GlobalConstants.DEFAULT.toString(), edge, type, props);
561 // Notify the corresponding containers
562 List<String> containers = getEdgeContainers(edge);
563 if (containers != null) {
564 for (String container : containers) {
565 notifyEdge(container, edge, type, props);
571 * Return a list of containers the edge associated with
573 private List<String> getEdgeContainers(Edge edge) {
574 NodeConnector src = edge.getTailNodeConnector(), dst = edge
575 .getHeadNodeConnector();
577 if (!src.getType().equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) {
578 /* Find the common containers for both ends */
579 List<String> srcContainers = this.containerMap.get(src), dstContainers = this.containerMap
580 .get(dst), cmnContainers = null;
581 if ((srcContainers != null) && (dstContainers != null)) {
582 cmnContainers = new ArrayList<String>(srcContainers);
583 cmnContainers.retainAll(dstContainers);
585 return cmnContainers;
588 * If the neighbor is part of a monitored production network, get
589 * the containers that the edge port belongs to
591 return this.containerMap.get(dst);
596 public void tagUpdated(String containerName, Node n, short oldTag,
597 short newTag, UpdateType t) {
601 public void containerFlowUpdated(String containerName,
602 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
606 public void nodeConnectorUpdated(String containerName, NodeConnector p,
608 if (this.containerMap == null) {
609 logger.error("containerMap is NULL");
612 List<String> containers = this.containerMap.get(p);
613 if (containers == null) {
614 containers = new CopyOnWriteArrayList<String>();
616 boolean updateMap = false;
619 if (!containers.contains(containerName)) {
620 containers.add(containerName);
625 if (containers.contains(containerName)) {
626 containers.remove(containerName);
628 removeNodeConnector(containerName, p);
635 if (containers.isEmpty()) {
636 // Do cleanup to reduce memory footprint if no
637 // elements to be tracked
638 this.containerMap.remove(p);
640 this.containerMap.put(p, containers);
646 public void containerModeUpdated(UpdateType t) {
650 private void registerWithOSGIConsole() {
651 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
653 bundleContext.registerService(CommandProvider.class.getName(), this,
658 public String getHelp() {
659 StringBuffer help = new StringBuffer();
660 help.append("---Topology Service Shim---\n");
661 help.append("\t pem [container] - Print edgeMap entries");
662 help.append(" for a given container\n");
663 return help.toString();
666 public void _pem(CommandInterpreter ci) {
667 String container = ci.nextArgument();
668 if (container == null) {
669 container = GlobalConstants.DEFAULT.toString();
672 ci.println("Container: " + container);
673 ci.println(" Edge Bandwidth");
675 Map<NodeConnector, Pair<Edge, Set<Property>>> edgePropsMap = edgeMap
677 if (edgePropsMap == null) {
681 for (Pair<Edge, Set<Property>> edgeProps : edgePropsMap.values()) {
682 if (edgeProps == null) {
687 Set<Property> props = edgeProps.getRight();
689 for (Property prop : props) {
690 if (prop.getName().equals(Bandwidth.BandwidthPropName)) {
691 bw = ((Bandwidth) prop).getValue();
696 ci.println(edgeProps.getLeft() + " " + bw);
698 ci.println("Total number of Edges: " + count);
701 public void _bwfactor(CommandInterpreter ci) {
702 String factorString = ci.nextArgument();
703 if (factorString == null) {
704 ci.println("Bw threshold: " + this.bwThresholdFactor);
705 ci.println("Insert a non null bw threshold");
708 bwThresholdFactor = Float.parseFloat(factorString);
709 ci.println("New Bw threshold: " + this.bwThresholdFactor);
713 * This method will trigger topology updates to be sent toward SAL. SAL then
714 * pushes the updates to ALL the applications that have registered as
715 * listeners for this service. SAL has no way of knowing which application
716 * requested for the refresh.
718 * As an example of this case, is stopping and starting the Topology
719 * Manager. When the topology Manager is stopped, and restarted, it will no
720 * longer have the latest topology. Hence, a request is sent here.
722 * @param containerName
726 public void requestRefresh(String containerName) {
727 // wake up a bulk update thread and exit
728 // the thread will execute the bulkUpdate()
729 bulkNotifyQ.add(containerName);
733 * Retrieve/construct edge map for a given container
735 * @param containerName
737 * @return the edges and their properties
739 private Map<NodeConnector, Pair<Edge, Set<Property>>> getEdgeMap(String containerName) {
740 Map<NodeConnector, Pair<Edge, Set<Property>>> edgePropMap = null;
743 * When container is freshly created, need to construct map based on global map.
745 edgePropMap = edgeMap.get(containerName);
751 * Reading the current topology database, the method will replay all the
752 * edge updates for the ITopologyServiceShimListener instance in the given
753 * container, which will in turn publish them toward SAL.
755 * @param containerName
758 private void TopologyBulkUpdate(String containerName) {
759 Map<NodeConnector, Pair<Edge, Set<Property>>> edgePropMap = null;
761 logger.debug("Try bulk update for container:{}", containerName);
762 edgePropMap = getEdgeMap(containerName);
763 if (edgePropMap == null) {
764 logger.debug("No edges known for container:{}", containerName);
767 ITopologyServiceShimListener topologServiceShimListener = topologyServiceShimListeners
769 if (topologServiceShimListener == null) {
770 logger.debug("No topology service shim listener for container:{}",
775 List<TopoEdgeUpdate> teuList = new ArrayList<TopoEdgeUpdate>();
776 for (Pair<Edge, Set<Property>> edgeProps : edgePropMap.values()) {
777 if (edgeProps != null) {
779 teuList.add(new TopoEdgeUpdate(edgeProps.getLeft(), edgeProps
780 .getRight(), UpdateType.ADDED));
781 logger.trace("Add edge {}", edgeProps.getLeft());
785 topologServiceShimListener.edgeUpdate(teuList);
787 logger.debug("Sent {} updates", i);
791 public void updateNode(Node node, UpdateType type, Set<Property> props) {
795 public void updateNodeConnector(NodeConnector nodeConnector,
796 UpdateType type, Set<Property> props) {
797 List<String> containers = new ArrayList<String>();
798 List<String> conList = this.containerMap.get(nodeConnector);
800 containers.add(GlobalConstants.DEFAULT.toString());
801 if (conList != null) {
802 containers.addAll(conList);
813 boolean rmEdge = false;
814 for (Property prop : props) {
815 if (((prop instanceof Config) && (((Config) prop).getValue() != Config.ADMIN_UP))
816 || ((prop instanceof State) && (((State) prop)
817 .getValue() != State.EDGE_UP))) {
819 * If port admin down or link down, remove the edges
820 * associated with the port
828 for (String cName : containers) {
829 removeNodeConnector(cName, nodeConnector);
834 for (String cName : containers) {
835 removeNodeConnector(cName, nodeConnector);