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.action.SupportedFlowActions;
30 import org.opendaylight.controller.sal.connection.ConnectionLocality;
31 import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
32 import org.opendaylight.controller.sal.core.Buffers;
33 import org.opendaylight.controller.sal.core.Capabilities;
34 import org.opendaylight.controller.sal.core.ContainerFlow;
35 import org.opendaylight.controller.sal.core.Description;
36 import org.opendaylight.controller.sal.core.IContainerAware;
37 import org.opendaylight.controller.sal.core.IContainerListener;
38 import org.opendaylight.controller.sal.core.MacAddress;
39 import org.opendaylight.controller.sal.core.Node;
40 import org.opendaylight.controller.sal.core.NodeConnector;
41 import org.opendaylight.controller.sal.core.Property;
42 import org.opendaylight.controller.sal.core.Tables;
43 import org.opendaylight.controller.sal.core.TimeStamp;
44 import org.opendaylight.controller.sal.core.UpdateType;
45 import org.opendaylight.controller.sal.utils.GlobalConstants;
46 import org.opendaylight.controller.sal.utils.NodeCreator;
47 import org.openflow.protocol.OFMessage;
48 import org.openflow.protocol.OFPortStatus;
49 import org.openflow.protocol.OFPortStatus.OFPortReason;
50 import org.openflow.protocol.OFType;
51 import org.openflow.protocol.statistics.OFDescriptionStatistics;
52 import org.openflow.protocol.statistics.OFStatistics;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * The class describes a shim layer that bridges inventory events from Openflow
58 * core to various listeners. The notifications are filtered based on container
63 public class InventoryServiceShim implements IContainerListener,
64 IMessageListener, ISwitchStateListener, IOFStatisticsListener, IContainerAware {
65 protected static final Logger logger = LoggerFactory
66 .getLogger(InventoryServiceShim.class);
67 private IController controller = null;
68 private final ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
69 private final Set<IInventoryShimInternalListener> globalInventoryShimInternalListeners = new HashSet<IInventoryShimInternalListener>();
70 private final List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
71 private final ConcurrentMap<NodeConnector, Set<String>> nodeConnectorContainerMap = new ConcurrentHashMap<NodeConnector, Set<String>>();
72 private final ConcurrentMap<Node, Set<String>> nodeContainerMap = new ConcurrentHashMap<Node, Set<String>>();
73 private final ConcurrentMap<NodeConnector, Set<Property>> nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Set<Property>>();
74 private final ConcurrentMap<Node, Set<Property>> nodeProps = new ConcurrentHashMap<Node, Set<Property>>();
75 private IPluginOutConnectionService connectionOutService;
77 void setController(IController s) {
81 void unsetController(IController s) {
82 if (this.controller == s) {
83 this.controller = null;
87 void setInventoryShimGlobalInternalListener(Map<?, ?> props,
88 IInventoryShimInternalListener s) {
89 if ((this.globalInventoryShimInternalListeners != null)) {
90 this.globalInventoryShimInternalListeners.add(s);
94 void unsetInventoryShimGlobalInternalListener(Map<?, ?> props,
95 IInventoryShimInternalListener s) {
96 if ((this.globalInventoryShimInternalListeners != null)) {
97 this.globalInventoryShimInternalListeners.remove(s);
101 void setInventoryShimInternalListener(Map<?, ?> props,
102 IInventoryShimInternalListener s) {
104 logger.error("setInventoryShimInternalListener property is null");
107 String containerName = (String) props.get("containerName");
108 if (containerName == null) {
109 logger.error("setInventoryShimInternalListener containerName not supplied");
112 if ((this.inventoryShimInternalListeners != null)
113 && !this.inventoryShimInternalListeners.containsValue(s)) {
114 this.inventoryShimInternalListeners.put(containerName, s);
116 "Added inventoryShimInternalListener for container {}",
121 void unsetInventoryShimInternalListener(Map<?, ?> props,
122 IInventoryShimInternalListener s) {
124 logger.error("unsetInventoryShimInternalListener property is null");
127 String containerName = (String) props.get("containerName");
128 if (containerName == null) {
129 logger.error("setInventoryShimInternalListener containerName not supplied");
132 if ((this.inventoryShimInternalListeners != null)
133 && this.inventoryShimInternalListeners.get(containerName) != null
134 && this.inventoryShimInternalListeners.get(containerName)
136 this.inventoryShimInternalListeners.remove(containerName);
138 "Removed inventoryShimInternalListener for container {}",
143 void setInventoryShimExternalListener(IInventoryShimExternalListener s) {
144 logger.trace("Set inventoryShimExternalListener {}", s);
145 if ((this.inventoryShimExternalListeners != null)
146 && !this.inventoryShimExternalListeners.contains(s)) {
147 this.inventoryShimExternalListeners.add(s);
151 void unsetInventoryShimExternalListener(IInventoryShimExternalListener s) {
152 logger.trace("Unset inventoryShimExternalListener {}", s);
153 if ((this.inventoryShimExternalListeners != null)
154 && this.inventoryShimExternalListeners.contains(s)) {
155 this.inventoryShimExternalListeners.remove(s);
159 void setIPluginOutConnectionService(IPluginOutConnectionService s) {
160 connectionOutService = s;
163 void unsetIPluginOutConnectionService(IPluginOutConnectionService s) {
164 if (connectionOutService == s) {
165 connectionOutService = null;
170 * Function called by the dependency manager when all the required
171 * dependencies are satisfied
175 this.controller.addMessageListener(OFType.PORT_STATUS, this);
176 this.controller.addSwitchStateListener(this);
180 * Function called after registering the service in OSGi service registry.
183 /* Start with existing switches */
188 * Function called by the dependency manager when at least one dependency
189 * become unsatisfied or when the component is shutting down because for
190 * example bundle is being stopped.
194 this.controller.removeMessageListener(OFType.PACKET_IN, this);
195 this.controller.removeSwitchStateListener(this);
197 this.inventoryShimInternalListeners.clear();
198 this.nodeConnectorContainerMap.clear();
199 this.nodeContainerMap.clear();
200 this.globalInventoryShimInternalListeners.clear();
201 this.controller = null;
205 public void receive(ISwitch sw, OFMessage msg) {
206 if (msg instanceof OFPortStatus) {
207 handlePortStatusMessage(sw, (OFPortStatus) msg);
212 protected void handlePortStatusMessage(ISwitch sw, OFPortStatus m) {
213 Node node = NodeCreator.createOFNode(sw.getId());
214 NodeConnector nodeConnector = PortConverter.toNodeConnector(
215 m.getDesc().getPortNumber(), node);
216 // get node connector properties
217 Set<Property> props = InventoryServiceHelper.OFPortToProps(m.getDesc());
219 UpdateType type = null;
220 if (m.getReason() == (byte) OFPortReason.OFPPR_ADD.ordinal()) {
221 type = UpdateType.ADDED;
222 nodeConnectorProps.put(nodeConnector, props);
223 } else if (m.getReason() == (byte) OFPortReason.OFPPR_DELETE.ordinal()) {
224 type = UpdateType.REMOVED;
225 nodeConnectorProps.remove(nodeConnector);
226 } else if (m.getReason() == (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
227 type = UpdateType.CHANGED;
228 nodeConnectorProps.put(nodeConnector, props);
231 logger.trace("handlePortStatusMessage {} type {}", nodeConnector, type);
234 notifyInventoryShimListener(nodeConnector, type, props);
239 public void switchAdded(ISwitch sw) {
243 Node node = NodeCreator.createOFNode(sw.getId());
244 if ((nodeProps.get(node) != null) && (connectionOutService.isLocal(node))) {
245 logger.debug("Ignore switchAdded {}", sw);
249 // Add all the nodeConnectors of this switch
250 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
251 .OFSwitchToProps(sw);
252 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps.entrySet()) {
253 Set<Property> props = new HashSet<Property>();
254 Set<Property> prop = entry.getValue();
258 nodeConnectorProps.put(entry.getKey(), props);
259 notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED, entry.getValue());
263 if (connectionOutService.getLocalityStatus(node) != ConnectionLocality.NOT_CONNECTED) {
266 logger.debug("Skipping node addition due to Connectivity Status : {}", connectionOutService.getLocalityStatus(node).name());
271 public void switchDeleted(ISwitch sw) {
280 public void containerModeUpdated(UpdateType t) {
285 public void tagUpdated(String containerName, Node n, short oldTag,
286 short newTag, UpdateType t) {
287 logger.debug("tagUpdated: {} type {} for container {}", new Object[] {
288 n, t, containerName });
292 public void containerFlowUpdated(String containerName,
293 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
297 public void nodeConnectorUpdated(String containerName, NodeConnector nc, UpdateType t) {
298 logger.debug("nodeConnectorUpdated: {} type {} for container {}", new Object[] { nc, t, containerName });
299 Node node = nc.getNode();
300 Set<String> ncContainers = this.nodeConnectorContainerMap.get(nc);
301 Set<String> nodeContainers = this.nodeContainerMap.get(node);
302 if (ncContainers == null) {
303 ncContainers = new CopyOnWriteArraySet<String>();
305 if (nodeContainers == null) {
306 nodeContainers = new CopyOnWriteArraySet<String>();
308 boolean notifyNodeUpdate = false;
312 if (ncContainers.add(containerName)) {
313 this.nodeConnectorContainerMap.put(nc, ncContainers);
315 if (nodeContainers.add(containerName)) {
316 this.nodeContainerMap.put(node, nodeContainers);
317 notifyNodeUpdate = true;
321 if (ncContainers.remove(containerName)) {
322 if (ncContainers.isEmpty()) {
323 // Do cleanup to reduce memory footprint if no
324 // elements to be tracked
325 this.nodeConnectorContainerMap.remove(nc);
327 this.nodeConnectorContainerMap.put(nc, ncContainers);
330 boolean nodeContainerUpdate = true;
331 for (NodeConnector ncContainer : nodeConnectorContainerMap.keySet()) {
332 if ((ncContainer.getNode().equals(node)) && (nodeConnectorContainerMap.get(ncContainer).contains(containerName))) {
333 nodeContainerUpdate = false;
337 if (nodeContainerUpdate) {
338 nodeContainers.remove(containerName);
339 notifyNodeUpdate = true;
340 if (nodeContainers.isEmpty()) {
341 this.nodeContainerMap.remove(node);
343 this.nodeContainerMap.put(node, nodeContainers);
351 Set<Property> nodeProp = nodeProps.get(node);
352 if (nodeProp == null) {
355 Set<Property> ncProp = nodeConnectorProps.get(nc);
356 // notify InventoryService
357 notifyInventoryShimInternalListener(containerName, nc, t, ncProp);
359 if (notifyNodeUpdate) {
360 notifyInventoryShimInternalListener(containerName, node, t, nodeProp);
364 private void notifyInventoryShimExternalListener(Node node, UpdateType type, Set<Property> props) {
365 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
366 s.updateNode(node, type, props);
370 private void notifyInventoryShimExternalListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
371 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
372 s.updateNodeConnector(nodeConnector, type, props);
376 private void notifyInventoryShimInternalListener(String container,
377 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
378 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners.get(container);
379 if (inventoryShimInternalListener != null) {
380 inventoryShimInternalListener.updateNodeConnector(nodeConnector, type, props);
381 logger.trace("notifyInventoryShimInternalListener {} type {} for container {}", new Object[] {
382 nodeConnector, type, container });
387 * Notify all internal and external listeners
389 private void notifyInventoryShimListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
391 //establish locality before notifying
393 if (type == UpdateType.REMOVED){
394 //if removing get the locality first
395 isNodeLocal = connectionOutService.isLocal(nodeConnector.getNode());
396 notifyGlobalInventoryShimInternalListener(nodeConnector, type, props);
398 notifyGlobalInventoryShimInternalListener(nodeConnector, type, props);
399 isNodeLocal = connectionOutService.isLocal(nodeConnector.getNode());
403 // notify other containers
404 Set<String> containers = (nodeConnectorContainerMap.get(nodeConnector) == null) ? new HashSet<String>()
405 : new HashSet<String>(nodeConnectorContainerMap.get(nodeConnector));
406 containers.add(GlobalConstants.DEFAULT.toString());
407 for (String container : containers) {
408 notifyInventoryShimInternalListener(container, nodeConnector, type, props);
411 // Notify plugin listeners (Discovery, DataPacket, OFstats etc.)
412 notifyInventoryShimExternalListener(nodeConnector, type, props);
414 logger.debug("Connection service accepted the inventory notification for {} {}", nodeConnector, type);
416 logger.debug("Connection service dropped the inventory notification for {} {}", nodeConnector, type);
421 * Notify all internal and external listeners
423 private void notifyInventoryShimListener(Node node, UpdateType type, Set<Property> props) {
425 //establish locality before notifying
427 if (type == UpdateType.REMOVED){
428 //if removing get the locality first
429 isNodeLocal = connectionOutService.isLocal(node);
430 notifyGlobalInventoryShimInternalListener(node, type, props);
432 notifyGlobalInventoryShimInternalListener(node, type, props);
433 isNodeLocal = connectionOutService.isLocal(node);
437 // Now notify other containers
438 Set<String> containers = (nodeContainerMap.get(node) == null) ? new HashSet<String>()
439 : new HashSet<String>(nodeContainerMap.get(node));
440 containers.add(GlobalConstants.DEFAULT.toString());
441 for (String container : containers) {
442 notifyInventoryShimInternalListener(container, node, type, props);
445 // Notify plugin listeners (Discovery, DataPacket, OFstats etc.)
446 notifyInventoryShimExternalListener(node, type, props);
448 logger.debug("Connection service accepted the inventory notification for {} {}", node, type);
450 logger.debug("Connection service dropped the inventory notification for {} {}", node, type);
454 private void notifyGlobalInventoryShimInternalListener(Node node, UpdateType type, Set<Property> props) {
455 for (IInventoryShimInternalListener globalListener : globalInventoryShimInternalListeners) {
456 globalListener.updateNode(node, type, props);
457 logger.trace("notifyGlobalInventoryShimInternalListener {} type {}", new Object[] { node, type });
461 private void notifyGlobalInventoryShimInternalListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
462 for (IInventoryShimInternalListener globalListener : globalInventoryShimInternalListeners) {
463 globalListener.updateNodeConnector(nodeConnector, type, props);
465 "notifyGlobalInventoryShimInternalListener {} type {}",
466 new Object[] { nodeConnector, type });
470 private void notifyInventoryShimInternalListener(String container,
471 Node node, UpdateType type, Set<Property> props) {
472 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
474 if (inventoryShimInternalListener != null) {
475 inventoryShimInternalListener.updateNode(node, type, props);
477 "notifyInventoryShimInternalListener {} type {} for container {}",
478 new Object[] { node, type, container });
482 private void addNode(ISwitch sw) {
483 Node node = NodeCreator.createOFNode(sw.getId());
484 UpdateType type = UpdateType.ADDED;
486 Set<Property> props = new HashSet<Property>();
487 Long sid = (Long) node.getID();
489 Date connectedSince = sw.getConnectedDate();
490 Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
492 props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
493 props.add(new MacAddress(deriveMacAddress(sid)));
495 byte tables = sw.getTables();
496 Tables t = new Tables(tables);
500 int cap = sw.getCapabilities();
501 Capabilities c = new Capabilities(cap);
505 int act = sw.getActions();
506 SupportedFlowActions a = new SupportedFlowActions(FlowConverter.getFlowActions(act));
510 int buffers = sw.getBuffers();
511 Buffers b = new Buffers(buffers);
516 if ((nodeProps.get(node) == null) && (connectionOutService.isLocal(node))) {
517 // The switch is connected for the first time, flush all flows
518 // that may exist on this switch
521 nodeProps.put(node, props);
522 // Notify all internal and external listeners
523 notifyInventoryShimListener(node, type, props);
526 private void removeNode(ISwitch sw) {
527 Node node = NodeCreator.createOFNode(sw.getId());
531 removeNodeConnectorProps(node);
532 nodeProps.remove(node);
533 UpdateType type = UpdateType.REMOVED;
534 // Notify all internal and external listeners
535 notifyInventoryShimListener(node, type, null);
538 private void startService() {
539 // Get a snapshot of all the existing switches
540 Map<Long, ISwitch> switches = this.controller.getSwitches();
541 for (ISwitch sw : switches.values()) {
546 private void removeNodeConnectorProps(Node node) {
547 List<NodeConnector> ncList = new ArrayList<NodeConnector>();
548 for (NodeConnector nc : nodeConnectorProps.keySet()) {
549 if (nc.getNode().equals(node)) {
553 for (NodeConnector nc : ncList) {
554 nodeConnectorProps.remove(nc);
559 public void descriptionStatisticsRefreshed(Long switchId, List<OFStatistics> descriptionStats) {
560 Node node = NodeCreator.createOFNode(switchId);
561 Set<Property> properties = new HashSet<Property>(1);
562 OFDescriptionStatistics ofDesc = (OFDescriptionStatistics) descriptionStats.get(0);
563 Description desc = new Description(ofDesc.getDatapathDescription());
564 properties.add(desc);
566 // Notify all internal and external listeners
567 notifyInventoryShimListener(node, UpdateType.CHANGED, properties);
570 private byte[] deriveMacAddress(long dpid) {
571 byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
573 for (short i = 0; i < 6; i++) {
574 mac[5 - i] = (byte) dpid;
582 public void flowStatisticsRefreshed(Long switchId, List<OFStatistics> flows) {
587 public void portStatisticsRefreshed(Long switchId, List<OFStatistics> ports) {
592 public void tableStatisticsRefreshed(Long switchId, List<OFStatistics> tables) {
597 public void containerCreate(String containerName) {
602 public void containerDestroy(String containerName) {
603 Set<NodeConnector> removeNodeConnectorSet = new HashSet<NodeConnector>();
604 Set<Node> removeNodeSet = new HashSet<Node>();
605 for (Map.Entry<NodeConnector, Set<String>> entry : nodeConnectorContainerMap.entrySet()) {
606 Set<String> ncContainers = entry.getValue();
607 if (ncContainers.contains(containerName)) {
608 NodeConnector nodeConnector = entry.getKey();
609 removeNodeConnectorSet.add(nodeConnector);
612 for (Map.Entry<Node, Set<String>> entry : nodeContainerMap.entrySet()) {
613 Set<String> nodeContainers = entry.getValue();
614 if (nodeContainers.contains(containerName)) {
615 Node node = entry.getKey();
616 removeNodeSet.add(node);
619 for (NodeConnector nodeConnector : removeNodeConnectorSet) {
620 Set<String> ncContainers = nodeConnectorContainerMap.get(nodeConnector);
621 ncContainers.remove(containerName);
622 if (ncContainers.isEmpty()) {
623 nodeConnectorContainerMap.remove(nodeConnector);
626 for (Node node : removeNodeSet) {
627 Set<String> nodeContainers = nodeContainerMap.get(node);
628 nodeContainers.remove(containerName);
629 if (nodeContainers.isEmpty()) {
630 nodeContainerMap.remove(node);