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.core.IController;
23 import org.opendaylight.controller.protocol_plugin.openflow.core.IMessageListener;
24 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
25 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitchStateListener;
26 import org.opendaylight.controller.sal.core.Actions;
27 import org.opendaylight.controller.sal.core.Buffers;
28 import org.opendaylight.controller.sal.core.Capabilities;
29 import org.opendaylight.controller.sal.core.ConstructionException;
30 import org.opendaylight.controller.sal.core.ContainerFlow;
31 import org.opendaylight.controller.sal.core.IContainerListener;
32 import org.opendaylight.controller.sal.core.Node;
33 import org.opendaylight.controller.sal.core.Node.NodeIDType;
34 import org.opendaylight.controller.sal.core.NodeConnector;
35 import org.opendaylight.controller.sal.core.Property;
36 import org.opendaylight.controller.sal.core.Tables;
37 import org.opendaylight.controller.sal.core.TimeStamp;
38 import org.opendaylight.controller.sal.core.UpdateType;
39 import org.opendaylight.controller.sal.utils.GlobalConstants;
40 import org.openflow.protocol.OFMessage;
41 import org.openflow.protocol.OFPortStatus;
42 import org.openflow.protocol.OFPortStatus.OFPortReason;
43 import org.openflow.protocol.OFType;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * The class describes a shim layer that bridges inventory events from Openflow
49 * core to various listeners. The notifications are filtered based on container
54 public class InventoryServiceShim implements IContainerListener,
55 IMessageListener, ISwitchStateListener {
56 protected static final Logger logger = LoggerFactory
57 .getLogger(InventoryServiceShim.class);
58 private IController controller = null;
59 private ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
60 private List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
61 private ConcurrentMap<NodeConnector, List<String>> containerMap = new ConcurrentHashMap<NodeConnector, List<String>>();
63 void setController(IController s) {
67 void unsetController(IController s) {
68 if (this.controller == s) {
69 this.controller = null;
73 void setInventoryShimInternalListener(Map<?, ?> props,
74 IInventoryShimInternalListener s) {
76 logger.error("setInventoryShimInternalListener property is null");
79 String containerName = (String) props.get("containerName");
80 if (containerName == null) {
81 logger.error("setInventoryShimInternalListener containerName not supplied");
84 if ((this.inventoryShimInternalListeners != null)
85 && !this.inventoryShimInternalListeners.containsValue(s)) {
86 this.inventoryShimInternalListeners.put(containerName, s);
88 "Added inventoryShimInternalListener for container {}",
93 void unsetInventoryShimInternalListener(Map<?, ?> props,
94 IInventoryShimInternalListener s) {
96 logger.error("unsetInventoryShimInternalListener property is null");
99 String containerName = (String) props.get("containerName");
100 if (containerName == null) {
101 logger.error("unsetInventoryShimInternalListener containerName not supplied");
104 if ((this.inventoryShimInternalListeners != null)
105 && this.inventoryShimInternalListeners.get(containerName) != null
106 && this.inventoryShimInternalListeners.get(containerName)
108 this.inventoryShimInternalListeners.remove(containerName);
110 "Removed inventoryShimInternalListener for container {}",
115 void setInventoryShimExternalListener(IInventoryShimExternalListener s) {
116 logger.trace("Set inventoryShimExternalListener");
117 if ((this.inventoryShimExternalListeners != null)
118 && !this.inventoryShimExternalListeners.contains(s)) {
119 this.inventoryShimExternalListeners.add(s);
123 void unsetInventoryShimExternalListener(IInventoryShimExternalListener s) {
124 if ((this.inventoryShimExternalListeners != null)
125 && this.inventoryShimExternalListeners.contains(s)) {
126 this.inventoryShimExternalListeners.remove(s);
131 * Function called by the dependency manager when all the required
132 * dependencies are satisfied
136 this.controller.addMessageListener(OFType.PORT_STATUS, this);
137 this.controller.addSwitchStateListener(this);
141 * Function called after registering the service in OSGi service registry.
144 /* Start with existing switches */
149 * Function called by the dependency manager when at least one dependency
150 * become unsatisfied or when the component is shutting down because for
151 * example bundle is being stopped.
155 this.controller.removeMessageListener(OFType.PACKET_IN, this);
156 this.controller.removeSwitchStateListener(this);
158 this.inventoryShimInternalListeners.clear();
159 this.containerMap.clear();
160 this.controller = null;
164 public void receive(ISwitch sw, OFMessage msg) {
166 if (msg instanceof OFPortStatus) {
167 handlePortStatusMessage(sw, (OFPortStatus) msg);
169 } catch (ConstructionException e) {
175 protected void handlePortStatusMessage(ISwitch sw, OFPortStatus m)
176 throws ConstructionException {
177 Node node = new Node(NodeIDType.OPENFLOW, sw.getId());
178 NodeConnector nodeConnector = PortConverter.toNodeConnector(m.getDesc()
179 .getPortNumber(), node);
180 UpdateType type = null;
182 if (m.getReason() == (byte) OFPortReason.OFPPR_ADD.ordinal()) {
183 type = UpdateType.ADDED;
184 } else if (m.getReason() == (byte) OFPortReason.OFPPR_DELETE.ordinal()) {
185 type = UpdateType.REMOVED;
186 } else if (m.getReason() == (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
187 type = UpdateType.CHANGED;
190 logger.trace("handlePortStatusMessage {} type {}", nodeConnector, type);
193 // get node connector properties
194 Set<Property> props = InventoryServiceHelper.OFPortToProps(m
196 notifyInventoryShimListener(nodeConnector, type, props);
201 public void switchAdded(ISwitch sw) {
205 // Add all the nodeConnectors of this switch
206 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
207 .OFSwitchToProps(sw);
208 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps.entrySet()) {
209 notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED,
218 public void switchDeleted(ISwitch sw) {
226 public void containerModeUpdated(UpdateType t) {
231 public void tagUpdated(String containerName, Node n, short oldTag,
232 short newTag, UpdateType t) {
236 public void containerFlowUpdated(String containerName,
237 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
241 public void nodeConnectorUpdated(String containerName, NodeConnector p,
243 if (this.containerMap == null) {
244 logger.error("containerMap is NULL");
247 List<String> containers = this.containerMap.get(p);
248 if (containers == null) {
249 containers = new CopyOnWriteArrayList<String>();
251 boolean updateMap = false;
254 if (!containers.contains(containerName)) {
255 containers.add(containerName);
260 if (containers.contains(containerName)) {
261 containers.remove(containerName);
269 if (containers.isEmpty()) {
270 // Do cleanup to reduce memory footprint if no
271 // elements to be tracked
272 this.containerMap.remove(p);
274 this.containerMap.put(p, containers);
278 // notify InventoryService
279 notifyInventoryShimInternalListener(containerName, p, t, null);
282 private void notifyInventoryShimExternalListener(Node node,
283 UpdateType type, Set<Property> props) {
284 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
285 s.updateNode(node, type, props);
289 private void notifyInventoryShimExternalListener(
290 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
291 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
292 s.updateNodeConnector(nodeConnector, type, props);
296 private void notifyInventoryShimInternalListener(String container,
297 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
298 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
300 if (inventoryShimInternalListener != null) {
301 inventoryShimInternalListener.updateNodeConnector(nodeConnector,
304 "notifyInventoryShimInternalListener {} type {} for container {}",
305 nodeConnector, type, container);
310 * Notify all internal and external listeners
312 private void notifyInventoryShimListener(NodeConnector nodeConnector,
313 UpdateType type, Set<Property> props) {
314 // Always notify default InventoryService. Store properties in default
316 notifyInventoryShimInternalListener(GlobalConstants.DEFAULT.toString(),
317 nodeConnector, type, props);
319 // Now notify other containers
320 List<String> containers = containerMap.get(nodeConnector);
321 if (containers != null) {
322 for (String container : containers) {
323 // no property stored in container components.
324 notifyInventoryShimInternalListener(container, nodeConnector,
329 // Notify DiscoveryService
330 notifyInventoryShimExternalListener(nodeConnector, type, props);
334 * Notify all internal and external listeners
336 private void notifyInventoryShimListener(Node node, UpdateType type,
337 Set<Property> props) {
340 // Notify only the default Inventory Service
341 IInventoryShimInternalListener inventoryShimDefaultListener = inventoryShimInternalListeners
342 .get(GlobalConstants.DEFAULT.toString());
343 if (inventoryShimDefaultListener != null) {
344 inventoryShimDefaultListener.updateNode(node, type, props);
348 // Notify all Inventory Service containers
349 for (IInventoryShimInternalListener inventoryShimInternalListener : inventoryShimInternalListeners
351 inventoryShimInternalListener.updateNode(node, type, null);
358 // Notify external listener
359 notifyInventoryShimExternalListener(node, type, props);
362 private void addNode(ISwitch sw) {
365 node = new Node(NodeIDType.OPENFLOW, sw.getId());
366 } catch (ConstructionException e) {
367 logger.error("{}", e.getMessage());
371 UpdateType type = UpdateType.ADDED;
373 Set<Property> props = new HashSet<Property>();
374 Long sid = (Long) node.getID();
376 Date connectedSince = controller.getSwitches().get(sid)
378 Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
380 props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
382 byte tables = sw.getTables();
383 Tables t = new Tables(tables);
387 int cap = sw.getCapabilities();
388 Capabilities c = new Capabilities(cap);
392 int act = sw.getActions();
393 Actions a = new Actions(act);
397 int buffers = sw.getBuffers();
398 Buffers b = new Buffers(buffers);
402 // Notify all internal and external listeners
403 notifyInventoryShimListener(node, type, props);
406 private void removeNode(ISwitch sw) {
409 node = new Node(NodeIDType.OPENFLOW, sw.getId());
410 } catch (ConstructionException e) {
411 logger.error("{}", e.getMessage());
415 UpdateType type = UpdateType.REMOVED;
417 // Notify all internal and external listeners
418 notifyInventoryShimListener(node, type, null);
421 private void startService() {
422 // Get a snapshot of all the existing switches
423 Map<Long, ISwitch> switches = this.controller.getSwitches();
424 for (ISwitch sw : switches.values()) {