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.Date;
12 import java.util.HashSet;
13 import java.util.List;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import java.util.concurrent.CopyOnWriteArrayList;
20 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener;
21 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimInternalListener;
22 import org.opendaylight.controller.protocol_plugin.openflow.IOFStatisticsListener;
23 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
24 import org.opendaylight.controller.protocol_plugin.openflow.core.IMessageListener;
25 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
26 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitchStateListener;
27 import org.opendaylight.controller.sal.core.Actions;
28 import org.opendaylight.controller.sal.core.Buffers;
29 import org.opendaylight.controller.sal.core.Capabilities;
30 import org.opendaylight.controller.sal.core.ConstructionException;
31 import org.opendaylight.controller.sal.core.ContainerFlow;
32 import org.opendaylight.controller.sal.core.Description;
33 import org.opendaylight.controller.sal.core.IContainerListener;
34 import org.opendaylight.controller.sal.core.MacAddress;
35 import org.opendaylight.controller.sal.core.Node;
36 import org.opendaylight.controller.sal.core.Node.NodeIDType;
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, List<String>> containerMap = new ConcurrentHashMap<NodeConnector, List<String>>();
69 void setController(IController s) {
73 void unsetController(IController s) {
74 if (this.controller == s) {
75 this.controller = null;
79 void setInventoryShimInternalListener(Map<?, ?> props,
80 IInventoryShimInternalListener s) {
82 logger.error("setInventoryShimInternalListener property is null");
85 String containerName = (String) props.get("containerName");
86 if (containerName == null) {
87 logger.error("setInventoryShimInternalListener containerName not supplied");
90 if ((this.inventoryShimInternalListeners != null)
91 && !this.inventoryShimInternalListeners.containsValue(s)) {
92 this.inventoryShimInternalListeners.put(containerName, s);
94 "Added inventoryShimInternalListener for container {}",
99 void unsetInventoryShimInternalListener(Map<?, ?> props,
100 IInventoryShimInternalListener s) {
102 logger.error("unsetInventoryShimInternalListener property is null");
105 String containerName = (String) props.get("containerName");
106 if (containerName == null) {
107 logger.error("unsetInventoryShimInternalListener containerName not supplied");
110 if ((this.inventoryShimInternalListeners != null)
111 && this.inventoryShimInternalListeners.get(containerName) != null
112 && this.inventoryShimInternalListeners.get(containerName)
114 this.inventoryShimInternalListeners.remove(containerName);
116 "Removed inventoryShimInternalListener for container {}",
121 void setInventoryShimExternalListener(IInventoryShimExternalListener s) {
122 logger.trace("Set inventoryShimExternalListener");
123 if ((this.inventoryShimExternalListeners != null)
124 && !this.inventoryShimExternalListeners.contains(s)) {
125 this.inventoryShimExternalListeners.add(s);
129 void unsetInventoryShimExternalListener(IInventoryShimExternalListener s) {
130 if ((this.inventoryShimExternalListeners != null)
131 && this.inventoryShimExternalListeners.contains(s)) {
132 this.inventoryShimExternalListeners.remove(s);
137 * Function called by the dependency manager when all the required
138 * dependencies are satisfied
142 this.controller.addMessageListener(OFType.PORT_STATUS, this);
143 this.controller.addSwitchStateListener(this);
147 * Function called after registering the service in OSGi service registry.
150 /* Start with existing switches */
155 * Function called by the dependency manager when at least one dependency
156 * become unsatisfied or when the component is shutting down because for
157 * example bundle is being stopped.
161 this.controller.removeMessageListener(OFType.PACKET_IN, this);
162 this.controller.removeSwitchStateListener(this);
164 this.inventoryShimInternalListeners.clear();
165 this.containerMap.clear();
166 this.controller = null;
170 public void receive(ISwitch sw, OFMessage msg) {
171 if (msg instanceof OFPortStatus) {
172 handlePortStatusMessage(sw, (OFPortStatus) msg);
177 protected void handlePortStatusMessage(ISwitch sw, OFPortStatus m) {
178 Node node = NodeCreator.createOFNode(sw.getId());
179 NodeConnector nodeConnector = PortConverter.toNodeConnector(
180 m.getDesc().getPortNumber(), node);
182 UpdateType type = null;
183 if (m.getReason() == (byte) OFPortReason.OFPPR_ADD.ordinal()) {
184 type = UpdateType.ADDED;
185 } else if (m.getReason() == (byte) OFPortReason.OFPPR_DELETE.ordinal()) {
186 type = UpdateType.REMOVED;
187 } else if (m.getReason() == (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
188 type = UpdateType.CHANGED;
191 logger.trace("handlePortStatusMessage {} type {}", nodeConnector, type);
194 // get node connector properties
195 Set<Property> props = InventoryServiceHelper.OFPortToProps(m.getDesc());
196 notifyInventoryShimListener(nodeConnector, type, props);
201 public void switchAdded(ISwitch sw) {
206 // Add all the nodeConnectors of this switch
207 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
208 .OFSwitchToProps(sw);
209 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps.entrySet()) {
210 notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED,
219 public void switchDeleted(ISwitch sw) {
228 public void containerModeUpdated(UpdateType t) {
233 public void tagUpdated(String containerName, Node n, short oldTag,
234 short newTag, UpdateType t) {
235 logger.debug("tagUpdated: {} type {} for container {}", new Object[] {
236 n, t, containerName });
240 public void containerFlowUpdated(String containerName,
241 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
245 public void nodeConnectorUpdated(String containerName, NodeConnector p,
247 logger.debug("nodeConnectorUpdated: {} type {} for container {}",
248 new Object[] { p, t, containerName });
249 if (this.containerMap == null) {
250 logger.error("containerMap is NULL");
253 List<String> containers = this.containerMap.get(p);
254 if (containers == null) {
255 containers = new CopyOnWriteArrayList<String>();
257 boolean updateMap = false;
260 if (!containers.contains(containerName)) {
261 containers.add(containerName);
266 if (containers.contains(containerName)) {
267 containers.remove(containerName);
275 if (containers.isEmpty()) {
276 // Do cleanup to reduce memory footprint if no
277 // elements to be tracked
278 this.containerMap.remove(p);
280 this.containerMap.put(p, containers);
284 // notify InventoryService
285 notifyInventoryShimInternalListener(containerName, p, t, null);
286 notifyInventoryShimInternalListener(containerName, p.getNode(), t, null);
289 private void notifyInventoryShimExternalListener(Node node,
290 UpdateType type, Set<Property> props) {
291 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
292 s.updateNode(node, type, props);
296 private void notifyInventoryShimExternalListener(
297 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
298 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
299 s.updateNodeConnector(nodeConnector, type, props);
303 private void notifyInventoryShimInternalListener(String container,
304 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
305 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
307 if (inventoryShimInternalListener != null) {
308 inventoryShimInternalListener.updateNodeConnector(nodeConnector,
311 "notifyInventoryShimInternalListener {} type {} for container {}",
312 new Object[] { nodeConnector, type, container });
317 * Notify all internal and external listeners
319 private void notifyInventoryShimListener(NodeConnector nodeConnector,
320 UpdateType type, Set<Property> props) {
321 // Always notify default InventoryService. Store properties in default
323 notifyInventoryShimInternalListener(GlobalConstants.DEFAULT.toString(),
324 nodeConnector, type, props);
326 // Now notify other containers
327 List<String> containers = containerMap.get(nodeConnector);
328 if (containers != null) {
329 for (String container : containers) {
330 // no property stored in container components.
331 notifyInventoryShimInternalListener(container, nodeConnector,
336 // Notify DiscoveryService
337 notifyInventoryShimExternalListener(nodeConnector, type, props);
341 * Notify all internal and external listeners
343 private void notifyInventoryShimListener(Node node, UpdateType type,
344 Set<Property> props) {
347 // Notify only the default Inventory Service
348 IInventoryShimInternalListener inventoryShimDefaultListener = inventoryShimInternalListeners
349 .get(GlobalConstants.DEFAULT.toString());
350 if (inventoryShimDefaultListener != null) {
351 inventoryShimDefaultListener.updateNode(node, type, props);
355 // Notify all Inventory Service containers
356 for (IInventoryShimInternalListener inventoryShimInternalListener : inventoryShimInternalListeners
358 inventoryShimInternalListener.updateNode(node, type, null);
362 // Notify only the default Inventory Service
363 inventoryShimDefaultListener = inventoryShimInternalListeners
364 .get(GlobalConstants.DEFAULT.toString());
365 if (inventoryShimDefaultListener != null) {
366 inventoryShimDefaultListener.updateNode(node, type, props);
373 // Notify external listener
374 notifyInventoryShimExternalListener(node, type, props);
377 private void notifyInventoryShimInternalListener(String container,
378 Node node, UpdateType type, Set<Property> props) {
379 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
381 if (inventoryShimInternalListener != null) {
382 inventoryShimInternalListener.updateNode(node, type, props);
384 "notifyInventoryShimInternalListener {} type {} for container {}",
385 new Object[] { node, type, container });
389 private void addNode(ISwitch sw) {
390 Node node = NodeCreator.createOFNode(sw.getId());
391 UpdateType type = UpdateType.ADDED;
393 Set<Property> props = new HashSet<Property>();
394 Long sid = (Long) node.getID();
396 Date connectedSince = controller.getSwitches().get(sid)
398 Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
400 props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
401 props.add(new MacAddress(deriveMacAddress(sid)));
403 byte tables = sw.getTables();
404 Tables t = new Tables(tables);
408 int cap = sw.getCapabilities();
409 Capabilities c = new Capabilities(cap);
413 int act = sw.getActions();
414 Actions a = new Actions(act);
418 int buffers = sw.getBuffers();
419 Buffers b = new Buffers(buffers);
424 // Notify all internal and external listeners
425 notifyInventoryShimListener(node, type, props);
428 private void removeNode(ISwitch sw) {
431 node = new Node(NodeIDType.OPENFLOW, sw.getId());
432 } catch (ConstructionException e) {
433 logger.error("{}", e.getMessage());
437 UpdateType type = UpdateType.REMOVED;
439 // Notify all internal and external listeners
440 notifyInventoryShimListener(node, type, null);
443 private void startService() {
444 // Get a snapshot of all the existing switches
445 Map<Long, ISwitch> switches = this.controller.getSwitches();
446 for (ISwitch sw : switches.values()) {
452 public void descriptionStatisticsRefreshed(Long switchId, List<OFStatistics> descriptionStats) {
453 Node node = NodeCreator.createOFNode(switchId);
454 Set<Property> properties = new HashSet<Property>(1);
455 OFDescriptionStatistics ofDesc = (OFDescriptionStatistics) descriptionStats.get(0);
456 Description desc = new Description(ofDesc.getDatapathDescription());
457 properties.add(desc);
459 // Notify all internal and external listeners
460 notifyInventoryShimListener(node, UpdateType.CHANGED, properties);
463 private byte[] deriveMacAddress(long dpid) {
464 byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
466 for (short i = 0; i < 6; i++) {
467 mac[5 - i] = (byte) dpid;
475 public void flowStatisticsRefreshed(Long switchId, List<OFStatistics> flows) {
480 public void portStatisticsRefreshed(Long switchId, List<OFStatistics> ports) {
485 public void tableStatisticsRefreshed(Long switchId, List<OFStatistics> tables) {