3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.protocol_plugin.openflow.internal;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
17 import java.util.Timer;
18 import java.util.TimerTask;
19 import java.util.concurrent.BlockingQueue;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22 import java.util.concurrent.CopyOnWriteArrayList;
23 import java.util.concurrent.LinkedBlockingQueue;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.eclipse.osgi.framework.console.CommandInterpreter;
28 import org.eclipse.osgi.framework.console.CommandProvider;
29 import org.opendaylight.controller.protocol_plugin.openflow.IOFStatisticsManager;
30 import org.opendaylight.controller.protocol_plugin.openflow.IRefreshInternalProvider;
31 import org.opendaylight.controller.protocol_plugin.openflow.ITopologyServiceShimListener;
32 import org.osgi.framework.BundleContext;
33 import org.osgi.framework.FrameworkUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import org.opendaylight.controller.sal.core.Bandwidth;
38 import org.opendaylight.controller.sal.core.ContainerFlow;
39 import org.opendaylight.controller.sal.core.Edge;
40 import org.opendaylight.controller.sal.core.IContainerListener;
41 import org.opendaylight.controller.sal.core.Node;
42 import org.opendaylight.controller.sal.core.NodeConnector;
43 import org.opendaylight.controller.sal.core.Property;
44 import org.opendaylight.controller.sal.core.UpdateType;
45 import org.opendaylight.controller.sal.discovery.IDiscoveryService;
46 import org.opendaylight.controller.sal.utils.GlobalConstants;
49 * The class describes a shim layer that relays the topology events from OpenFlow
50 * core to various listeners. The notifications are filtered based on container
53 public class TopologyServiceShim implements IDiscoveryService,
54 IContainerListener, CommandProvider, IRefreshInternalProvider {
55 protected static final Logger logger = LoggerFactory
56 .getLogger(TopologyServiceShim.class);
57 private ConcurrentMap<String, ITopologyServiceShimListener> topologyServiceShimListeners = new ConcurrentHashMap<String, ITopologyServiceShimListener>();
58 private ConcurrentMap<NodeConnector, List<String>> containerMap = new ConcurrentHashMap<NodeConnector, List<String>>();
59 private ConcurrentMap<String, Map<NodeConnector, Pair<Edge, Set<Property>>>> edgeMap = new ConcurrentHashMap<String, Map<NodeConnector, Pair<Edge, Set<Property>>>>();
61 private BlockingQueue<NotifyEntry> notifyQ;
62 private Thread notifyThread;
63 private BlockingQueue<String> bulkNotifyQ;
64 private Thread ofPluginTopoBulkUpdate;
65 private volatile Boolean shuttingDown = false;
66 private IOFStatisticsManager statsMgr;
67 private Timer pollTimer;
68 private TimerTask txRatePoller;
69 private Thread bwUtilNotifyThread;
70 private BlockingQueue<UtilizationUpdate> bwUtilNotifyQ;
71 private List<NodeConnector> connectorsOverUtilized;
72 private float bwThresholdFactor = (float) 0.8; // Threshold = 80% of link bandwidth
76 Pair<Edge, Set<Property>> edgeProps;
79 NotifyEntry(String container, Pair<Edge, Set<Property>> edgeProps,
81 this.container = container;
82 this.edgeProps = edgeProps;
87 class TopologyNotify implements Runnable {
88 private final BlockingQueue<NotifyEntry> notifyQ;
90 TopologyNotify(BlockingQueue<NotifyEntry> notifyQ) {
91 this.notifyQ = notifyQ;
97 NotifyEntry entry = notifyQ.take();
99 ITopologyServiceShimListener topologServiceShimListener = topologyServiceShimListeners
100 .get(entry.container);
101 topologServiceShimListener.edgeUpdate(entry.edgeProps
102 .getLeft(), entry.type, entry.edgeProps.getRight());
105 } catch (InterruptedException e1) {
106 logger.warn("TopologyNotify interrupted", e1.getMessage());
110 } catch (Exception e2) {
111 e2.printStackTrace();
117 class UtilizationUpdate {
118 NodeConnector connector;
121 UtilizationUpdate(NodeConnector connector, UpdateType type) {
122 this.connector = connector;
127 class BwUtilizationNotify implements Runnable {
128 private final BlockingQueue<UtilizationUpdate> notifyQ;
130 BwUtilizationNotify(BlockingQueue<UtilizationUpdate> notifyQ) {
131 this.notifyQ = notifyQ;
137 UtilizationUpdate update = notifyQ.take();
138 NodeConnector connector = update.connector;
139 Set<String> containerList = edgeMap.keySet();//.get(connector);
140 for (String container : containerList) {
141 Map<NodeConnector, Pair<Edge, Set<Property>>> edgePropsMap = edgeMap
143 Edge edge = edgePropsMap.get(connector).getLeft();
144 if (edge.getTailNodeConnector().equals(connector)) {
145 ITopologyServiceShimListener topologServiceShimListener = topologyServiceShimListeners
147 if (update.type == UpdateType.ADDED) {
148 topologServiceShimListener
149 .edgeOverUtilized(edge);
151 topologServiceShimListener
152 .edgeUtilBackToNormal(edge);
156 } catch (InterruptedException e1) {
159 "Edge Bandwidth Utilization Notify Thread interrupted",
164 } catch (Exception e2) {
165 e2.printStackTrace();
172 * Function called by the dependency manager when all the required
173 * dependencies are satisfied
177 logger.trace("Init called");
178 connectorsOverUtilized = new ArrayList<NodeConnector>();
179 notifyQ = new LinkedBlockingQueue<NotifyEntry>();
180 notifyThread = new Thread(new TopologyNotify(notifyQ));
181 bwUtilNotifyQ = new LinkedBlockingQueue<UtilizationUpdate>();
182 bwUtilNotifyThread = new Thread(new BwUtilizationNotify(bwUtilNotifyQ));
183 bulkNotifyQ = new LinkedBlockingQueue<String>();
184 ofPluginTopoBulkUpdate = new Thread(new Runnable() {
189 String containerName = bulkNotifyQ.take();
190 logger.debug("Bulk Notify container:{}", containerName);
191 TopologyBulkUpdate(containerName);
192 } catch (InterruptedException e) {
193 logger.warn("Topology Bulk update thread interrupted");
200 }, "Topology Bulk Update");
202 // Initialize node connector tx bit rate poller timer
203 pollTimer = new Timer();
204 txRatePoller = new TimerTask() {
211 registerWithOSGIConsole();
215 * Continuously polls the transmit bit rate for all the node connectors
216 * from statistics manager and trigger the warning notification upward
217 * when the transmit rate is above a threshold which is a percentage of
220 protected void pollTxBitRates() {
221 Map<NodeConnector, Pair<Edge, Set<Property>>> globalContainerEdges = edgeMap
222 .get(GlobalConstants.DEFAULT.toString());
223 if (globalContainerEdges == null) {
227 for (NodeConnector connector : globalContainerEdges.keySet()) {
228 // Skip if node connector belongs to production switch
229 if (connector.getType() == NodeConnector.NodeConnectorIDType.PRODUCTION) {
233 // Get edge for which this node connector is head
234 Pair<Edge, Set<Property>> props = this.edgeMap.get(
235 GlobalConstants.DEFAULT.toString()).get(connector);
236 // On switch mgr restart the props get reset
240 Set<Property> propSet = props.getRight();
241 if (propSet == null) {
246 for (Property prop : propSet) {
247 if (prop instanceof Bandwidth) {
248 bw = ((Bandwidth) prop).getValue();
253 // Skip if agent did not provide a bandwidth info for the edge
258 // Compare bandwidth usage
259 Long switchId = (Long) connector.getNode().getID();
260 Short port = (Short) connector.getID();
261 float rate = statsMgr.getTransmitRate(switchId, port);
262 if (rate > bwThresholdFactor * bw) {
263 if (!connectorsOverUtilized.contains(connector)) {
264 connectorsOverUtilized.add(connector);
265 this.bwUtilNotifyQ.add(new UtilizationUpdate(connector,
269 if (connectorsOverUtilized.contains(connector)) {
270 connectorsOverUtilized.remove(connector);
271 this.bwUtilNotifyQ.add(new UtilizationUpdate(connector,
272 UpdateType.REMOVED));
280 * Function called by the dependency manager when at least one
281 * dependency become unsatisfied or when the component is shutting
282 * down because for example bundle is being stopped.
286 logger.trace("DESTROY called!");
292 * Function called by dependency manager after "init ()" is called
293 * and after the services provided by the class are registered in
294 * the service registry
298 logger.trace("START called!");
299 notifyThread.start();
300 bwUtilNotifyThread.start();
301 ofPluginTopoBulkUpdate.start();
302 pollTimer.scheduleAtFixedRate(txRatePoller, 10000, 5000);
306 * Function called by the dependency manager before the services
307 * exported by the component are unregistered, this will be
308 * followed by a "destroy ()" calls
312 logger.trace("STOP called!");
314 notifyThread.interrupt();
317 void setTopologyServiceShimListener(Map<?, ?> props,
318 ITopologyServiceShimListener s) {
320 logger.error("Didn't receive the service properties");
323 String containerName = (String) props.get("containerName");
324 if (containerName == null) {
325 logger.error("containerName not supplied");
328 if ((this.topologyServiceShimListeners != null)
329 && !this.topologyServiceShimListeners.containsKey(s)) {
330 this.topologyServiceShimListeners.put(containerName, s);
331 logger.trace("Added topologyServiceShimListener for container:"
336 void unsetTopologyServiceShimListener(Map<?, ?> props,
337 ITopologyServiceShimListener s) {
339 logger.error("Didn't receive the service properties");
342 String containerName = (String) props.get("containerName");
343 if (containerName == null) {
344 logger.error("containerName not supplied");
347 if ((this.topologyServiceShimListeners != null)
348 && this.topologyServiceShimListeners.containsKey(s)) {
349 this.topologyServiceShimListeners.remove(containerName);
350 logger.trace("Removed topologyServiceShimListener for container: "
355 void setStatisticsManager(IOFStatisticsManager s) {
359 void unsetStatisticsManager(IOFStatisticsManager s) {
360 if (this.statsMgr == s) {
361 this.statsMgr = null;
365 private void removeNodeConnector(String container,
366 NodeConnector nodeConnector) {
367 Map<NodeConnector, Pair<Edge, Set<Property>>> edgePropsMap = edgeMap
369 if (edgePropsMap == null) {
373 // Remove edge in one direction
374 Pair<Edge, Set<Property>> edgeProps = edgePropsMap.get(nodeConnector);
375 if (edgeProps == null) {
378 notifyEdge(container, edgeProps.getLeft(), UpdateType.REMOVED, null);
380 // Remove edge in another direction
381 edgeProps = edgePropsMap
382 .get(edgeProps.getLeft().getHeadNodeConnector());
383 if (edgeProps == null) {
386 notifyEdge(container, edgeProps.getLeft(), UpdateType.REMOVED, null);
389 private void notifyEdge(String container, Edge edge, UpdateType type,
390 Set<Property> props) {
391 Map<NodeConnector, Pair<Edge, Set<Property>>> edgePropsMap = edgeMap
393 NodeConnector src = edge.getTailNodeConnector();
394 Pair<Edge, Set<Property>> edgeProps = new ImmutablePair<Edge, Set<Property>>(
400 if (edgePropsMap == null) {
401 edgePropsMap = new HashMap<NodeConnector, Pair<Edge, Set<Property>>>();
403 if (edgePropsMap.containsKey(src)
404 && edgePropsMap.get(src).equals(edgeProps)) {
405 // Entry already exists. Return here.
409 edgePropsMap.put(src, edgeProps);
410 edgeMap.put(container, edgePropsMap);
413 if (edgePropsMap != null) {
414 edgePropsMap.remove(src);
415 if (edgePropsMap.isEmpty()) {
416 edgeMap.remove(container);
418 edgeMap.put(container, edgePropsMap);
423 logger.debug("Invalid " + type + " Edge " + edge
424 + " in container {}", container);
428 notifyQ.add(new NotifyEntry(container, edgeProps, type));
430 logger.trace(type + " Edge " + edge + " in container {}", container);
434 public void notifyEdge(Edge edge, UpdateType type, Set<Property> props) {
435 if ((edge == null) || (type == null)) {
439 // Notify default container
440 notifyEdge(GlobalConstants.DEFAULT.toString(), edge, type, props);
442 // Notify the corresponding containers
443 List<String> containers = getEdgeContainers(edge);
444 if (containers != null) {
445 for (String container : containers) {
446 notifyEdge(container, edge, type, props);
452 * Return a list of containers the edge associated with
454 private List<String> getEdgeContainers(Edge edge) {
455 NodeConnector src = edge.getTailNodeConnector(), dst = edge
456 .getHeadNodeConnector();
458 if (!src.getType().equals(
459 NodeConnector.NodeConnectorIDType.PRODUCTION)) {
460 /* Find the common containers for both ends */
461 List<String> srcContainers = this.containerMap.get(src), dstContainers = this.containerMap
462 .get(dst), cmnContainers = null;
463 if ((srcContainers != null) && (dstContainers != null)) {
464 cmnContainers = new ArrayList<String>(srcContainers);
465 cmnContainers.retainAll(dstContainers);
467 return cmnContainers;
470 * If the neighbor is part of a monitored production network, get
471 * the containers that the edge port belongs to
473 return this.containerMap.get(dst);
478 public void tagUpdated(String containerName, Node n, short oldTag,
479 short newTag, UpdateType t) {
483 public void containerFlowUpdated(String containerName,
484 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
488 public void nodeConnectorUpdated(String containerName, NodeConnector p,
490 if (this.containerMap == null) {
491 logger.error("containerMap is NULL");
494 List<String> containers = this.containerMap.get(p);
495 if (containers == null) {
496 containers = new CopyOnWriteArrayList<String>();
498 boolean updateMap = false;
501 if (!containers.contains(containerName)) {
502 containers.add(containerName);
507 if (containers.contains(containerName)) {
508 containers.remove(containerName);
510 removeNodeConnector(containerName, p);
517 if (containers.isEmpty()) {
518 // Do cleanup to reduce memory footprint if no
519 // elements to be tracked
520 this.containerMap.remove(p);
522 this.containerMap.put(p, containers);
528 public void containerModeUpdated(UpdateType t) {
532 private void registerWithOSGIConsole() {
533 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
535 bundleContext.registerService(CommandProvider.class.getName(), this,
540 public String getHelp() {
541 StringBuffer help = new StringBuffer();
542 help.append("---Topology Service Shim---\n");
544 .append("\t pem [container] - Print edgeMap entries for a given container\n");
545 return help.toString();
548 public void _pem(CommandInterpreter ci) {
549 String container = ci.nextArgument();
550 if (container == null) {
551 container = GlobalConstants.DEFAULT.toString();
554 ci.println("Container: " + container);
556 .println(" Edge Bandwidth");
558 Map<NodeConnector, Pair<Edge, Set<Property>>> edgePropsMap = edgeMap
560 if (edgePropsMap == null) {
563 for (Pair<Edge, Set<Property>> edgeProps : edgePropsMap.values()) {
564 if (edgeProps == null) {
569 for (Property prop : edgeProps.getRight()) {
570 if (prop.getName().equals(Bandwidth.BandwidthPropName)) {
571 bw = ((Bandwidth) prop).getValue();
575 ci.println(edgeProps.getLeft() + " " + bw);
579 public void _bwfactor(CommandInterpreter ci) {
580 String factorString = ci.nextArgument();
581 if (factorString == null) {
582 ci.println("Bw threshold: " + this.bwThresholdFactor);
583 ci.println("Insert a non null bw threshold");
586 bwThresholdFactor = Float.parseFloat(factorString);
587 ci.println("New Bw threshold: " + this.bwThresholdFactor);
591 * This method will trigger topology updates to be sent
592 * toward SAL. SAL then pushes the updates to ALL the applications
593 * that have registered as listeners for this service. SAL has no
594 * way of knowing which application requested for the refresh.
596 * As an example of this case, is stopping and starting the
597 * Topology Manager. When the topology Manager is stopped,
598 * and restarted, it will no longer have the latest topology.
599 * Hence, a request is sent here.
601 * @param containerName
605 public void requestRefresh(String containerName) {
606 // wake up a bulk update thread and exit
607 // the thread will execute the bulkUpdate()
608 bulkNotifyQ.add(containerName);
612 * Reading the current topology database, the method will replay
613 * all the edge updates for the ITopologyServiceShimListener instance
614 * in the given container, which will in turn publish them toward SAL.
615 * @param containerName
617 private void TopologyBulkUpdate(String containerName) {
618 Map<NodeConnector, Pair<Edge, Set<Property>>> edgePropMap = null;
620 logger.debug("Try bulk update for container:{}", containerName);
621 edgePropMap = edgeMap.get(containerName);
622 if (edgePropMap == null) {
623 logger.debug("No edges known for container:{}", containerName);
626 ITopologyServiceShimListener topologServiceShimListener = topologyServiceShimListeners
628 if (topologServiceShimListener == null) {
629 logger.debug("No topology service shim listener for container:{}",
634 for (Pair<Edge, Set<Property>> edgeProps : edgePropMap.values()) {
635 if (edgeProps != null) {
637 logger.trace("Add edge {}", edgeProps.getLeft());
638 topologServiceShimListener.edgeUpdate(edgeProps.getLeft(),
639 UpdateType.ADDED, edgeProps.getRight());
642 logger.debug("Sent {} updates", i);