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 nc, UpdateType t) {
259 logger.debug("nodeConnectorUpdated: {} type {} for container {}", new Object[] { nc, t, containerName });
260 Node node = nc.getNode();
261 Set<String> ncContainers = this.nodeConnectorContainerMap.get(nc);
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(nc, 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(nc);
288 this.nodeConnectorContainerMap.put(nc, ncContainers);
291 boolean nodeContainerUpdate = true;
292 for (NodeConnector ncContainer : nodeConnectorContainerMap.keySet()) {
293 if ((ncContainer.getNode().equals(node)) && (nodeConnectorContainerMap.get(ncContainer).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> nodeProp = nodeProps.get(node);
313 if (nodeProp == null) {
316 Set<Property> ncProp = nodeConnectorProps.get(nc);
317 // notify InventoryService
318 notifyInventoryShimInternalListener(containerName, nc, t, ncProp);
320 if (notifyNodeUpdate) {
321 notifyInventoryShimInternalListener(containerName, node, t, nodeProp);
325 private void notifyInventoryShimExternalListener(Node node,
326 UpdateType type, Set<Property> props) {
327 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
328 s.updateNode(node, type, props);
332 private void notifyInventoryShimExternalListener(
333 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
334 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
335 s.updateNodeConnector(nodeConnector, type, props);
339 private void notifyInventoryShimInternalListener(String container,
340 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
341 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
343 if (inventoryShimInternalListener != null) {
344 inventoryShimInternalListener.updateNodeConnector(nodeConnector,
347 "notifyInventoryShimInternalListener {} type {} for container {}",
348 new Object[] { nodeConnector, type, container });
353 * Notify all internal and external listeners
355 private void notifyInventoryShimListener(NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
356 // notify other containers
357 Set<String> containers = (nodeConnectorContainerMap.get(nodeConnector) == null) ? new HashSet<String>()
358 : new HashSet<String>(nodeConnectorContainerMap.get(nodeConnector));
359 containers.add(GlobalConstants.DEFAULT.toString());
360 for (String container : containers) {
361 notifyInventoryShimInternalListener(container, nodeConnector, type, props);
364 // Notify DiscoveryService
365 notifyInventoryShimExternalListener(nodeConnector, type, props);
369 * Notify all internal and external listeners
371 private void notifyInventoryShimListener(Node node, UpdateType type, Set<Property> props) {
372 // Now notify other containers
373 Set<String> containers = (nodeContainerMap.get(node) == null) ? new HashSet<String>() : new HashSet<String>(
374 nodeContainerMap.get(node));
375 containers.add(GlobalConstants.DEFAULT.toString());
376 for (String container : containers) {
377 notifyInventoryShimInternalListener(container, node, type, props);
380 // Notify external listener
381 notifyInventoryShimExternalListener(node, type, props);
384 private void notifyInventoryShimInternalListener(String container,
385 Node node, UpdateType type, Set<Property> props) {
386 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
388 if (inventoryShimInternalListener != null) {
389 inventoryShimInternalListener.updateNode(node, type, props);
391 "notifyInventoryShimInternalListener {} type {} for container {}",
392 new Object[] { node, type, container });
396 private void addNode(ISwitch sw) {
397 Node node = NodeCreator.createOFNode(sw.getId());
398 UpdateType type = UpdateType.ADDED;
400 Set<Property> props = new HashSet<Property>();
401 Long sid = (Long) node.getID();
403 Date connectedSince = controller.getSwitches().get(sid)
405 Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
407 props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
408 props.add(new MacAddress(deriveMacAddress(sid)));
410 byte tables = sw.getTables();
411 Tables t = new Tables(tables);
415 int cap = sw.getCapabilities();
416 Capabilities c = new Capabilities(cap);
420 int act = sw.getActions();
421 Actions a = new Actions(act);
425 int buffers = sw.getBuffers();
426 Buffers b = new Buffers(buffers);
431 nodeProps.put(node, props);
432 // Notify all internal and external listeners
433 notifyInventoryShimListener(node, type, props);
436 private void removeNode(ISwitch sw) {
437 Node node = NodeCreator.createOFNode(sw.getId());
441 removeNodeConnectorProps(node);
442 nodeProps.remove(node);
443 UpdateType type = UpdateType.REMOVED;
444 // Notify all internal and external listeners
445 notifyInventoryShimListener(node, type, null);
448 private void startService() {
449 // Get a snapshot of all the existing switches
450 Map<Long, ISwitch> switches = this.controller.getSwitches();
451 for (ISwitch sw : switches.values()) {
456 private void removeNodeConnectorProps(Node node) {
457 List<NodeConnector> ncList = new ArrayList<NodeConnector>();
458 for (NodeConnector nc : nodeConnectorProps.keySet()) {
459 if (nc.getNode().equals(node)) {
463 for (NodeConnector nc : ncList) {
464 nodeConnectorProps.remove(nc);
469 public void descriptionStatisticsRefreshed(Long switchId, List<OFStatistics> descriptionStats) {
470 Node node = NodeCreator.createOFNode(switchId);
471 Set<Property> properties = new HashSet<Property>(1);
472 OFDescriptionStatistics ofDesc = (OFDescriptionStatistics) descriptionStats.get(0);
473 Description desc = new Description(ofDesc.getDatapathDescription());
474 properties.add(desc);
476 // Notify all internal and external listeners
477 notifyInventoryShimListener(node, UpdateType.CHANGED, properties);
480 private byte[] deriveMacAddress(long dpid) {
481 byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
483 for (short i = 0; i < 6; i++) {
484 mac[5 - i] = (byte) dpid;
492 public void flowStatisticsRefreshed(Long switchId, List<OFStatistics> flows) {
497 public void portStatisticsRefreshed(Long switchId, List<OFStatistics> ports) {
502 public void tableStatisticsRefreshed(Long switchId, List<OFStatistics> tables) {