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.Date;
13 import java.util.HashSet;
14 import java.util.List;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19 import java.util.concurrent.CopyOnWriteArrayList;
20 import java.util.concurrent.CopyOnWriteArraySet;
22 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener;
23 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimInternalListener;
24 import org.opendaylight.controller.protocol_plugin.openflow.IOFStatisticsListener;
25 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
26 import org.opendaylight.controller.protocol_plugin.openflow.core.IMessageListener;
27 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
28 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitchStateListener;
29 import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
30 import org.opendaylight.controller.sal.core.Actions;
31 import org.opendaylight.controller.sal.core.Buffers;
32 import org.opendaylight.controller.sal.core.Capabilities;
33 import org.opendaylight.controller.sal.core.ContainerFlow;
34 import org.opendaylight.controller.sal.core.Description;
35 import org.opendaylight.controller.sal.core.IContainerListener;
36 import org.opendaylight.controller.sal.core.MacAddress;
37 import org.opendaylight.controller.sal.core.Node;
38 import org.opendaylight.controller.sal.core.NodeConnector;
39 import org.opendaylight.controller.sal.core.Property;
40 import org.opendaylight.controller.sal.core.Tables;
41 import org.opendaylight.controller.sal.core.TimeStamp;
42 import org.opendaylight.controller.sal.core.UpdateType;
43 import org.opendaylight.controller.sal.utils.GlobalConstants;
44 import org.opendaylight.controller.sal.utils.NodeCreator;
45 import org.openflow.protocol.OFMessage;
46 import org.openflow.protocol.OFPortStatus;
47 import org.openflow.protocol.OFPortStatus.OFPortReason;
48 import org.openflow.protocol.OFType;
49 import org.openflow.protocol.statistics.OFDescriptionStatistics;
50 import org.openflow.protocol.statistics.OFStatistics;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * The class describes a shim layer that bridges inventory events from Openflow
56 * core to various listeners. The notifications are filtered based on container
61 public class InventoryServiceShim implements IContainerListener,
62 IMessageListener, ISwitchStateListener, IOFStatisticsListener {
63 protected static final Logger logger = LoggerFactory
64 .getLogger(InventoryServiceShim.class);
65 private IController controller = null;
66 private final ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
67 private final Set<IInventoryShimInternalListener> globalInventoryShimInternalListeners = new HashSet<IInventoryShimInternalListener>();
68 private final List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
69 private final ConcurrentMap<NodeConnector, Set<String>> nodeConnectorContainerMap = new ConcurrentHashMap<NodeConnector, Set<String>>();
70 private final ConcurrentMap<Node, Set<String>> nodeContainerMap = new ConcurrentHashMap<Node, Set<String>>();
71 private final ConcurrentMap<NodeConnector, Set<Property>> nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Set<Property>>();
72 private final ConcurrentMap<Node, Set<Property>> nodeProps = new ConcurrentHashMap<Node, Set<Property>>();
73 private IPluginOutConnectionService connectionOutService;
75 void setController(IController s) {
79 void unsetController(IController s) {
80 if (this.controller == s) {
81 this.controller = null;
85 void setInventoryShimGlobalInternalListener(Map<?, ?> props,
86 IInventoryShimInternalListener s) {
87 if ((this.globalInventoryShimInternalListeners != null)) {
88 this.globalInventoryShimInternalListeners.add(s);
92 void unsetInventoryShimGlobalInternalListener(Map<?, ?> props,
93 IInventoryShimInternalListener s) {
94 if ((this.globalInventoryShimInternalListeners != null)) {
95 this.globalInventoryShimInternalListeners.remove(s);
99 void setInventoryShimInternalListener(Map<?, ?> props,
100 IInventoryShimInternalListener s) {
102 logger.error("setInventoryShimInternalListener property is null");
105 String containerName = (String) props.get("containerName");
106 if (containerName == null) {
107 logger.error("setInventoryShimInternalListener containerName not supplied");
110 if ((this.inventoryShimInternalListeners != null)
111 && !this.inventoryShimInternalListeners.containsValue(s)) {
112 this.inventoryShimInternalListeners.put(containerName, s);
114 "Added inventoryShimInternalListener for container {}",
119 void unsetInventoryShimInternalListener(Map<?, ?> props,
120 IInventoryShimInternalListener s) {
122 logger.error("unsetInventoryShimInternalListener property is null");
125 String containerName = (String) props.get("containerName");
126 if (containerName == null) {
127 logger.error("setInventoryShimInternalListener containerName not supplied");
130 if ((this.inventoryShimInternalListeners != null)
131 && this.inventoryShimInternalListeners.get(containerName) != null
132 && this.inventoryShimInternalListeners.get(containerName)
134 this.inventoryShimInternalListeners.remove(containerName);
136 "Removed inventoryShimInternalListener for container {}",
141 void setInventoryShimExternalListener(IInventoryShimExternalListener s) {
142 logger.trace("Set inventoryShimExternalListener");
143 if ((this.inventoryShimExternalListeners != null)
144 && !this.inventoryShimExternalListeners.contains(s)) {
145 this.inventoryShimExternalListeners.add(s);
149 void unsetInventoryShimExternalListener(IInventoryShimExternalListener s) {
150 if ((this.inventoryShimExternalListeners != null)
151 && this.inventoryShimExternalListeners.contains(s)) {
152 this.inventoryShimExternalListeners.remove(s);
156 void setIPluginOutConnectionService(IPluginOutConnectionService s) {
157 connectionOutService = s;
160 void unsetIPluginOutConnectionService(IPluginOutConnectionService s) {
161 if (connectionOutService == s) {
162 connectionOutService = null;
167 * Function called by the dependency manager when all the required
168 * dependencies are satisfied
172 this.controller.addMessageListener(OFType.PORT_STATUS, this);
173 this.controller.addSwitchStateListener(this);
177 * Function called after registering the service in OSGi service registry.
180 /* Start with existing switches */
185 * Function called by the dependency manager when at least one dependency
186 * become unsatisfied or when the component is shutting down because for
187 * example bundle is being stopped.
191 this.controller.removeMessageListener(OFType.PACKET_IN, this);
192 this.controller.removeSwitchStateListener(this);
194 this.inventoryShimInternalListeners.clear();
195 this.nodeConnectorContainerMap.clear();
196 this.nodeContainerMap.clear();
197 this.globalInventoryShimInternalListeners.clear();
198 this.controller = null;
202 public void receive(ISwitch sw, OFMessage msg) {
203 if (msg instanceof OFPortStatus) {
204 handlePortStatusMessage(sw, (OFPortStatus) msg);
209 protected void handlePortStatusMessage(ISwitch sw, OFPortStatus m) {
210 Node node = NodeCreator.createOFNode(sw.getId());
211 NodeConnector nodeConnector = PortConverter.toNodeConnector(
212 m.getDesc().getPortNumber(), node);
213 // get node connector properties
214 Set<Property> props = InventoryServiceHelper.OFPortToProps(m.getDesc());
216 UpdateType type = null;
217 if (m.getReason() == (byte) OFPortReason.OFPPR_ADD.ordinal()) {
218 type = UpdateType.ADDED;
219 nodeConnectorProps.put(nodeConnector, props);
220 } else if (m.getReason() == (byte) OFPortReason.OFPPR_DELETE.ordinal()) {
221 type = UpdateType.REMOVED;
222 nodeConnectorProps.remove(nodeConnector);
223 } else if (m.getReason() == (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
224 type = UpdateType.CHANGED;
225 nodeConnectorProps.put(nodeConnector, props);
228 logger.trace("handlePortStatusMessage {} type {}", nodeConnector, type);
231 notifyInventoryShimListener(nodeConnector, type, props);
236 public void switchAdded(ISwitch sw) {
241 // Add all the nodeConnectors of this switch
242 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
243 .OFSwitchToProps(sw);
244 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps.entrySet()) {
245 Set<Property> props = new HashSet<Property>();
246 Set<Property> prop = entry.getValue();
250 nodeConnectorProps.put(entry.getKey(), props);
251 notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED,
260 public void switchDeleted(ISwitch sw) {
269 public void containerModeUpdated(UpdateType t) {
274 public void tagUpdated(String containerName, Node n, short oldTag,
275 short newTag, UpdateType t) {
276 logger.debug("tagUpdated: {} type {} for container {}", new Object[] {
277 n, t, containerName });
281 public void containerFlowUpdated(String containerName,
282 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
286 public void nodeConnectorUpdated(String containerName, NodeConnector nc, UpdateType t) {
287 logger.debug("nodeConnectorUpdated: {} type {} for container {}", new Object[] { nc, t, containerName });
288 Node node = nc.getNode();
289 Set<String> ncContainers = this.nodeConnectorContainerMap.get(nc);
290 Set<String> nodeContainers = this.nodeContainerMap.get(node);
291 if (ncContainers == null) {
292 ncContainers = new CopyOnWriteArraySet<String>();
294 if (nodeContainers == null) {
295 nodeContainers = new CopyOnWriteArraySet<String>();
297 boolean notifyNodeUpdate = false;
301 if (ncContainers.add(containerName)) {
302 this.nodeConnectorContainerMap.put(nc, ncContainers);
304 if (nodeContainers.add(containerName)) {
305 this.nodeContainerMap.put(node, nodeContainers);
306 notifyNodeUpdate = true;
310 if (ncContainers.remove(containerName)) {
311 if (ncContainers.isEmpty()) {
312 // Do cleanup to reduce memory footprint if no
313 // elements to be tracked
314 this.nodeConnectorContainerMap.remove(nc);
316 this.nodeConnectorContainerMap.put(nc, ncContainers);
319 boolean nodeContainerUpdate = true;
320 for (NodeConnector ncContainer : nodeConnectorContainerMap.keySet()) {
321 if ((ncContainer.getNode().equals(node)) && (nodeConnectorContainerMap.get(ncContainer).contains(containerName))) {
322 nodeContainerUpdate = false;
326 if (nodeContainerUpdate) {
327 nodeContainers.remove(containerName);
328 notifyNodeUpdate = true;
329 if (nodeContainers.isEmpty()) {
330 this.nodeContainerMap.remove(node);
332 this.nodeContainerMap.put(node, nodeContainers);
340 Set<Property> nodeProp = nodeProps.get(node);
341 if (nodeProp == null) {
344 Set<Property> ncProp = nodeConnectorProps.get(nc);
345 // notify InventoryService
346 notifyInventoryShimInternalListener(containerName, nc, t, ncProp);
348 if (notifyNodeUpdate) {
349 notifyInventoryShimInternalListener(containerName, node, t, nodeProp);
353 private void notifyInventoryShimExternalListener(Node node,
354 UpdateType type, Set<Property> props) {
355 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
356 s.updateNode(node, type, props);
360 private void notifyInventoryShimExternalListener(
361 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
362 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
363 s.updateNodeConnector(nodeConnector, type, props);
367 private void notifyInventoryShimInternalListener(String container,
368 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
369 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
371 if (inventoryShimInternalListener != null) {
372 inventoryShimInternalListener.updateNodeConnector(nodeConnector,
375 "notifyInventoryShimInternalListener {} type {} for container {}",
376 new Object[] { nodeConnector, type, container });
381 * Notify all internal and external listeners
383 private void notifyInventoryShimListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
385 //establish locality before notifying
387 if (type == UpdateType.REMOVED){
388 //if removing get the locality first
389 isNodeLocal = connectionOutService.isLocal(nodeConnector.getNode());
390 notifyGlobalInventoryShimInternalListener(nodeConnector, type, props);
392 notifyGlobalInventoryShimInternalListener(nodeConnector, type, props);
393 isNodeLocal = connectionOutService.isLocal(nodeConnector.getNode());
397 // notify other containers
398 Set<String> containers = (nodeConnectorContainerMap.get(nodeConnector) == null) ? new HashSet<String>()
399 : new HashSet<String>(nodeConnectorContainerMap.get(nodeConnector));
400 containers.add(GlobalConstants.DEFAULT.toString());
401 for (String container : containers) {
402 notifyInventoryShimInternalListener(container, nodeConnector, type, props);
405 // Notify DiscoveryService
406 notifyInventoryShimExternalListener(nodeConnector, type, props);
408 logger.debug("Connection service accepted the inventory notification for {} {}", nodeConnector, type);
410 logger.debug("Connection service dropped the inventory notification for {} {}", nodeConnector, type);
415 * Notify all internal and external listeners
417 private void notifyInventoryShimListener(Node node, UpdateType type, Set<Property> props) {
419 //establish locality before notifying
421 if (type == UpdateType.REMOVED){
422 //if removing get the locality first
423 isNodeLocal = connectionOutService.isLocal(node);
424 notifyGlobalInventoryShimInternalListener(node, type, props);
426 notifyGlobalInventoryShimInternalListener(node, type, props);
427 isNodeLocal = connectionOutService.isLocal(node);
431 // Now notify other containers
432 Set<String> containers = (nodeContainerMap.get(node) == null) ? new HashSet<String>() : new HashSet<String>(
433 nodeContainerMap.get(node));
434 containers.add(GlobalConstants.DEFAULT.toString());
435 for (String container : containers) {
436 notifyInventoryShimInternalListener(container, node, type, props);
439 // Notify external listener
440 notifyInventoryShimExternalListener(node, type, props);
442 logger.debug("Connection service accepted the inventory notification for {} {}", node, type);
444 logger.debug("Connection service dropped the inventory notification for {} {}", node, type);
448 private void notifyGlobalInventoryShimInternalListener(Node node, UpdateType type, Set<Property> props) {
449 for (IInventoryShimInternalListener globalListener : globalInventoryShimInternalListeners) {
450 globalListener.updateNode(node, type, props);
452 "notifyGlobalInventoryShimInternalListener {} type {}",
453 new Object[] { node, type });
457 private void notifyGlobalInventoryShimInternalListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
458 for (IInventoryShimInternalListener globalListener : globalInventoryShimInternalListeners) {
459 globalListener.updateNodeConnector(nodeConnector, type, props);
461 "notifyGlobalInventoryShimInternalListener {} type {}",
462 new Object[] { nodeConnector, type });
466 private void notifyInventoryShimInternalListener(String container,
467 Node node, UpdateType type, Set<Property> props) {
468 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
470 if (inventoryShimInternalListener != null) {
471 inventoryShimInternalListener.updateNode(node, type, props);
473 "notifyInventoryShimInternalListener {} type {} for container {}",
474 new Object[] { node, type, container });
478 private void addNode(ISwitch sw) {
479 Node node = NodeCreator.createOFNode(sw.getId());
480 UpdateType type = UpdateType.ADDED;
482 Set<Property> props = new HashSet<Property>();
483 Long sid = (Long) node.getID();
485 Date connectedSince = controller.getSwitches().get(sid)
487 Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
489 props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
490 props.add(new MacAddress(deriveMacAddress(sid)));
492 byte tables = sw.getTables();
493 Tables t = new Tables(tables);
497 int cap = sw.getCapabilities();
498 Capabilities c = new Capabilities(cap);
502 int act = sw.getActions();
503 Actions a = new Actions(act);
507 int buffers = sw.getBuffers();
508 Buffers b = new Buffers(buffers);
513 nodeProps.put(node, props);
514 // Notify all internal and external listeners
515 notifyInventoryShimListener(node, type, props);
518 private void removeNode(ISwitch sw) {
519 Node node = NodeCreator.createOFNode(sw.getId());
523 removeNodeConnectorProps(node);
524 nodeProps.remove(node);
525 UpdateType type = UpdateType.REMOVED;
526 // Notify all internal and external listeners
527 notifyInventoryShimListener(node, type, null);
530 private void startService() {
531 // Get a snapshot of all the existing switches
532 Map<Long, ISwitch> switches = this.controller.getSwitches();
533 for (ISwitch sw : switches.values()) {
538 private void removeNodeConnectorProps(Node node) {
539 List<NodeConnector> ncList = new ArrayList<NodeConnector>();
540 for (NodeConnector nc : nodeConnectorProps.keySet()) {
541 if (nc.getNode().equals(node)) {
545 for (NodeConnector nc : ncList) {
546 nodeConnectorProps.remove(nc);
551 public void descriptionStatisticsRefreshed(Long switchId, List<OFStatistics> descriptionStats) {
552 Node node = NodeCreator.createOFNode(switchId);
553 Set<Property> properties = new HashSet<Property>(1);
554 OFDescriptionStatistics ofDesc = (OFDescriptionStatistics) descriptionStats.get(0);
555 Description desc = new Description(ofDesc.getDatapathDescription());
556 properties.add(desc);
558 // Notify all internal and external listeners
559 notifyInventoryShimListener(node, UpdateType.CHANGED, properties);
562 private byte[] deriveMacAddress(long dpid) {
563 byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
565 for (short i = 0; i < 6; i++) {
566 mac[5 - i] = (byte) dpid;
574 public void flowStatisticsRefreshed(Long switchId, List<OFStatistics> flows) {
579 public void portStatisticsRefreshed(Long switchId, List<OFStatistics> ports) {
584 public void tableStatisticsRefreshed(Long switchId, List<OFStatistics> tables) {