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.core.Actions;
30 import org.opendaylight.controller.sal.core.Buffers;
31 import org.opendaylight.controller.sal.core.Capabilities;
32 import org.opendaylight.controller.sal.core.ContainerFlow;
33 import org.opendaylight.controller.sal.core.Description;
34 import org.opendaylight.controller.sal.core.IContainerListener;
35 import org.opendaylight.controller.sal.core.MacAddress;
36 import org.opendaylight.controller.sal.core.Node;
37 import org.opendaylight.controller.sal.core.NodeConnector;
38 import org.opendaylight.controller.sal.core.Property;
39 import org.opendaylight.controller.sal.core.Tables;
40 import org.opendaylight.controller.sal.core.TimeStamp;
41 import org.opendaylight.controller.sal.core.UpdateType;
42 import org.opendaylight.controller.sal.utils.GlobalConstants;
43 import org.opendaylight.controller.sal.utils.NodeCreator;
44 import org.openflow.protocol.OFMessage;
45 import org.openflow.protocol.OFPortStatus;
46 import org.openflow.protocol.OFPortStatus.OFPortReason;
47 import org.openflow.protocol.OFType;
48 import org.openflow.protocol.statistics.OFDescriptionStatistics;
49 import org.openflow.protocol.statistics.OFStatistics;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * The class describes a shim layer that bridges inventory events from Openflow
55 * core to various listeners. The notifications are filtered based on container
60 public class InventoryServiceShim implements IContainerListener,
61 IMessageListener, ISwitchStateListener, IOFStatisticsListener {
62 protected static final Logger logger = LoggerFactory
63 .getLogger(InventoryServiceShim.class);
64 private IController controller = null;
65 private final ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
66 private final List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
67 private final ConcurrentMap<NodeConnector, Set<String>> nodeConnectorContainerMap = new ConcurrentHashMap<NodeConnector, Set<String>>();
68 private final ConcurrentMap<Node, Set<String>> nodeContainerMap = new ConcurrentHashMap<Node, Set<String>>();
69 private final ConcurrentMap<NodeConnector, Set<Property>> nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Set<Property>>();
70 private final ConcurrentMap<Node, Set<Property>> nodeProps = new ConcurrentHashMap<Node, Set<Property>>();
72 void setController(IController s) {
76 void unsetController(IController s) {
77 if (this.controller == s) {
78 this.controller = null;
82 void setInventoryShimInternalListener(Map<?, ?> props,
83 IInventoryShimInternalListener s) {
85 logger.error("setInventoryShimInternalListener property is null");
88 String containerName = (String) props.get("containerName");
89 if (containerName == null) {
90 logger.error("setInventoryShimInternalListener containerName not supplied");
93 if ((this.inventoryShimInternalListeners != null)
94 && !this.inventoryShimInternalListeners.containsValue(s)) {
95 this.inventoryShimInternalListeners.put(containerName, s);
97 "Added inventoryShimInternalListener for container {}",
102 void unsetInventoryShimInternalListener(Map<?, ?> props,
103 IInventoryShimInternalListener s) {
105 logger.error("unsetInventoryShimInternalListener property is null");
108 String containerName = (String) props.get("containerName");
109 if (containerName == null) {
110 logger.error("unsetInventoryShimInternalListener containerName not supplied");
113 if ((this.inventoryShimInternalListeners != null)
114 && this.inventoryShimInternalListeners.get(containerName) != null
115 && this.inventoryShimInternalListeners.get(containerName)
117 this.inventoryShimInternalListeners.remove(containerName);
119 "Removed inventoryShimInternalListener for container {}",
124 void setInventoryShimExternalListener(IInventoryShimExternalListener s) {
125 logger.trace("Set inventoryShimExternalListener");
126 if ((this.inventoryShimExternalListeners != null)
127 && !this.inventoryShimExternalListeners.contains(s)) {
128 this.inventoryShimExternalListeners.add(s);
132 void unsetInventoryShimExternalListener(IInventoryShimExternalListener s) {
133 if ((this.inventoryShimExternalListeners != null)
134 && this.inventoryShimExternalListeners.contains(s)) {
135 this.inventoryShimExternalListeners.remove(s);
140 * Function called by the dependency manager when all the required
141 * dependencies are satisfied
145 this.controller.addMessageListener(OFType.PORT_STATUS, this);
146 this.controller.addSwitchStateListener(this);
150 * Function called after registering the service in OSGi service registry.
153 /* Start with existing switches */
158 * Function called by the dependency manager when at least one dependency
159 * become unsatisfied or when the component is shutting down because for
160 * example bundle is being stopped.
164 this.controller.removeMessageListener(OFType.PACKET_IN, this);
165 this.controller.removeSwitchStateListener(this);
167 this.inventoryShimInternalListeners.clear();
168 this.nodeConnectorContainerMap.clear();
169 this.nodeContainerMap.clear();
170 this.controller = null;
174 public void receive(ISwitch sw, OFMessage msg) {
175 if (msg instanceof OFPortStatus) {
176 handlePortStatusMessage(sw, (OFPortStatus) msg);
181 protected void handlePortStatusMessage(ISwitch sw, OFPortStatus m) {
182 Node node = NodeCreator.createOFNode(sw.getId());
183 NodeConnector nodeConnector = PortConverter.toNodeConnector(
184 m.getDesc().getPortNumber(), node);
185 // get node connector properties
186 Set<Property> props = InventoryServiceHelper.OFPortToProps(m.getDesc());
188 UpdateType type = null;
189 if (m.getReason() == (byte) OFPortReason.OFPPR_ADD.ordinal()) {
190 type = UpdateType.ADDED;
191 nodeConnectorProps.put(nodeConnector, props);
192 } else if (m.getReason() == (byte) OFPortReason.OFPPR_DELETE.ordinal()) {
193 type = UpdateType.REMOVED;
194 nodeConnectorProps.remove(nodeConnector);
195 } else if (m.getReason() == (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
196 type = UpdateType.CHANGED;
197 nodeConnectorProps.put(nodeConnector, props);
200 logger.trace("handlePortStatusMessage {} type {}", nodeConnector, type);
203 notifyInventoryShimListener(nodeConnector, type, props);
208 public void switchAdded(ISwitch sw) {
213 // Add all the nodeConnectors of this switch
214 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
215 .OFSwitchToProps(sw);
216 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps.entrySet()) {
217 Set<Property> props = new HashSet<Property>();
218 Set<Property> prop = entry.getValue();
222 nodeConnectorProps.put(entry.getKey(), props);
223 notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED,
232 public void switchDeleted(ISwitch sw) {
241 public void containerModeUpdated(UpdateType t) {
246 public void tagUpdated(String containerName, Node n, short oldTag,
247 short newTag, UpdateType t) {
248 logger.debug("tagUpdated: {} type {} for container {}", new Object[] {
249 n, t, containerName });
253 public void containerFlowUpdated(String containerName,
254 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
258 public void nodeConnectorUpdated(String containerName, NodeConnector p, UpdateType t) {
259 logger.debug("nodeConnectorUpdated: {} type {} for container {}", new Object[] { p, t, containerName });
260 Node node = p.getNode();
261 Set<String> ncContainers = this.nodeConnectorContainerMap.get(p);
262 Set<String> nodeContainers = this.nodeContainerMap.get(node);
263 if (ncContainers == null) {
264 ncContainers = new CopyOnWriteArraySet<String>();
266 if (nodeContainers == null) {
267 nodeContainers = new CopyOnWriteArraySet<String>();
269 boolean notifyNodeUpdate = false;
273 if (ncContainers.add(containerName)) {
274 this.nodeConnectorContainerMap.put(p, ncContainers);
276 if (nodeContainers.add(containerName)) {
277 this.nodeContainerMap.put(node, nodeContainers);
278 notifyNodeUpdate = true;
282 if (ncContainers.remove(containerName)) {
283 if (ncContainers.isEmpty()) {
284 // Do cleanup to reduce memory footprint if no
285 // elements to be tracked
286 this.nodeConnectorContainerMap.remove(p);
288 this.nodeConnectorContainerMap.put(p, ncContainers);
291 boolean nodeContainerUpdate = true;
292 for (NodeConnector nc : nodeConnectorContainerMap.keySet()) {
293 if ((nc.getNode().equals(node)) && (nodeConnectorContainerMap.get(nc).contains(containerName))) {
294 nodeContainerUpdate = false;
298 if (nodeContainerUpdate) {
299 nodeContainers.remove(containerName);
300 notifyNodeUpdate = true;
301 if (nodeContainers.isEmpty()) {
302 this.nodeContainerMap.remove(node);
304 this.nodeContainerMap.put(node, nodeContainers);
312 Set<Property> ncProp = nodeConnectorProps.get(p);
313 // notify InventoryService
314 notifyInventoryShimInternalListener(containerName, p, t, ncProp);
316 if (notifyNodeUpdate) {
317 Set<Property> nodeProp = nodeProps.get(node);
318 notifyInventoryShimInternalListener(containerName, node, t, nodeProp);
322 private void notifyInventoryShimExternalListener(Node node,
323 UpdateType type, Set<Property> props) {
324 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
325 s.updateNode(node, type, props);
329 private void notifyInventoryShimExternalListener(
330 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
331 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
332 s.updateNodeConnector(nodeConnector, type, props);
336 private void notifyInventoryShimInternalListener(String container,
337 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
338 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
340 if (inventoryShimInternalListener != null) {
341 inventoryShimInternalListener.updateNodeConnector(nodeConnector,
344 "notifyInventoryShimInternalListener {} type {} for container {}",
345 new Object[] { nodeConnector, type, container });
350 * Notify all internal and external listeners
352 private void notifyInventoryShimListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
353 // notify other containers
354 Set<String> containers = (nodeConnectorContainerMap.get(nodeConnector) == null) ? new HashSet<String>()
355 : new HashSet<String>(nodeConnectorContainerMap.get(nodeConnector));
356 containers.add(GlobalConstants.DEFAULT.toString());
357 for (String container : containers) {
358 notifyInventoryShimInternalListener(container, nodeConnector, type, props);
361 // Notify DiscoveryService
362 notifyInventoryShimExternalListener(nodeConnector, type, props);
366 * Notify all internal and external listeners
368 private void notifyInventoryShimListener(Node node, UpdateType type, Set<Property> props) {
369 // Now notify other containers
370 Set<String> containers = (nodeContainerMap.get(node) == null) ? new HashSet<String>() : new HashSet<String>(
371 nodeContainerMap.get(node));
372 containers.add(GlobalConstants.DEFAULT.toString());
373 for (String container : containers) {
374 notifyInventoryShimInternalListener(container, node, type, props);
377 // Notify external listener
378 notifyInventoryShimExternalListener(node, type, props);
381 private void notifyInventoryShimInternalListener(String container,
382 Node node, UpdateType type, Set<Property> props) {
383 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
385 if (inventoryShimInternalListener != null) {
386 inventoryShimInternalListener.updateNode(node, type, props);
388 "notifyInventoryShimInternalListener {} type {} for container {}",
389 new Object[] { node, type, container });
393 private void addNode(ISwitch sw) {
394 Node node = NodeCreator.createOFNode(sw.getId());
395 UpdateType type = UpdateType.ADDED;
397 Set<Property> props = new HashSet<Property>();
398 Long sid = (Long) node.getID();
400 Date connectedSince = controller.getSwitches().get(sid)
402 Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
404 props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
405 props.add(new MacAddress(deriveMacAddress(sid)));
407 byte tables = sw.getTables();
408 Tables t = new Tables(tables);
412 int cap = sw.getCapabilities();
413 Capabilities c = new Capabilities(cap);
417 int act = sw.getActions();
418 Actions a = new Actions(act);
422 int buffers = sw.getBuffers();
423 Buffers b = new Buffers(buffers);
428 nodeProps.put(node, props);
429 // Notify all internal and external listeners
430 notifyInventoryShimListener(node, type, props);
433 private void removeNode(ISwitch sw) {
434 Node node = NodeCreator.createOFNode(sw.getId());
438 removeNodeConnectorProps(node);
439 nodeProps.remove(node);
440 UpdateType type = UpdateType.REMOVED;
441 // Notify all internal and external listeners
442 notifyInventoryShimListener(node, type, null);
445 private void startService() {
446 // Get a snapshot of all the existing switches
447 Map<Long, ISwitch> switches = this.controller.getSwitches();
448 for (ISwitch sw : switches.values()) {
453 private void removeNodeConnectorProps(Node node) {
454 List<NodeConnector> ncList = new ArrayList<NodeConnector>();
455 for (NodeConnector nc : nodeConnectorProps.keySet()) {
456 if (nc.getNode().equals(node)) {
460 for (NodeConnector nc : ncList) {
461 nodeConnectorProps.remove(nc);
466 public void descriptionStatisticsRefreshed(Long switchId, List<OFStatistics> descriptionStats) {
467 Node node = NodeCreator.createOFNode(switchId);
468 Set<Property> properties = new HashSet<Property>(1);
469 OFDescriptionStatistics ofDesc = (OFDescriptionStatistics) descriptionStats.get(0);
470 Description desc = new Description(ofDesc.getDatapathDescription());
471 properties.add(desc);
473 // Notify all internal and external listeners
474 notifyInventoryShimListener(node, UpdateType.CHANGED, properties);
477 private byte[] deriveMacAddress(long dpid) {
478 byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
480 for (short i = 0; i < 6; i++) {
481 mac[5 - i] = (byte) dpid;
489 public void flowStatisticsRefreshed(Long switchId, List<OFStatistics> flows) {
494 public void portStatisticsRefreshed(Long switchId, List<OFStatistics> ports) {
499 public void tableStatisticsRefreshed(Long switchId, List<OFStatistics> tables) {