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.IStatisticsListener;
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.Node;
35 import org.opendaylight.controller.sal.core.Node.NodeIDType;
36 import org.opendaylight.controller.sal.core.NodeConnector;
37 import org.opendaylight.controller.sal.core.Property;
38 import org.opendaylight.controller.sal.core.Tables;
39 import org.opendaylight.controller.sal.core.TimeStamp;
40 import org.opendaylight.controller.sal.core.UpdateType;
41 import org.opendaylight.controller.sal.utils.GlobalConstants;
42 import org.openflow.protocol.OFMessage;
43 import org.openflow.protocol.OFPortStatus;
44 import org.openflow.protocol.OFPortStatus.OFPortReason;
45 import org.openflow.protocol.OFType;
46 import org.openflow.protocol.statistics.OFDescriptionStatistics;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * The class describes a shim layer that bridges inventory events from Openflow
52 * core to various listeners. The notifications are filtered based on container
57 public class InventoryServiceShim implements IContainerListener,
58 IMessageListener, ISwitchStateListener, IStatisticsListener {
59 protected static final Logger logger = LoggerFactory
60 .getLogger(InventoryServiceShim.class);
61 private IController controller = null;
62 private ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
63 private List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
64 private ConcurrentMap<NodeConnector, List<String>> containerMap = new ConcurrentHashMap<NodeConnector, List<String>>();
66 void setController(IController s) {
70 void unsetController(IController s) {
71 if (this.controller == s) {
72 this.controller = null;
76 void setInventoryShimInternalListener(Map<?, ?> props,
77 IInventoryShimInternalListener s) {
79 logger.error("setInventoryShimInternalListener property is null");
82 String containerName = (String) props.get("containerName");
83 if (containerName == null) {
84 logger.error("setInventoryShimInternalListener containerName not supplied");
87 if ((this.inventoryShimInternalListeners != null)
88 && !this.inventoryShimInternalListeners.containsValue(s)) {
89 this.inventoryShimInternalListeners.put(containerName, s);
91 "Added inventoryShimInternalListener for container {}",
96 void unsetInventoryShimInternalListener(Map<?, ?> props,
97 IInventoryShimInternalListener s) {
99 logger.error("unsetInventoryShimInternalListener property is null");
102 String containerName = (String) props.get("containerName");
103 if (containerName == null) {
104 logger.error("unsetInventoryShimInternalListener containerName not supplied");
107 if ((this.inventoryShimInternalListeners != null)
108 && this.inventoryShimInternalListeners.get(containerName) != null
109 && this.inventoryShimInternalListeners.get(containerName)
111 this.inventoryShimInternalListeners.remove(containerName);
113 "Removed inventoryShimInternalListener for container {}",
118 void setInventoryShimExternalListener(IInventoryShimExternalListener s) {
119 logger.trace("Set inventoryShimExternalListener");
120 if ((this.inventoryShimExternalListeners != null)
121 && !this.inventoryShimExternalListeners.contains(s)) {
122 this.inventoryShimExternalListeners.add(s);
126 void unsetInventoryShimExternalListener(IInventoryShimExternalListener s) {
127 if ((this.inventoryShimExternalListeners != null)
128 && this.inventoryShimExternalListeners.contains(s)) {
129 this.inventoryShimExternalListeners.remove(s);
134 * Function called by the dependency manager when all the required
135 * dependencies are satisfied
139 this.controller.addMessageListener(OFType.PORT_STATUS, this);
140 this.controller.addSwitchStateListener(this);
144 * Function called after registering the service in OSGi service registry.
147 /* Start with existing switches */
152 * Function called by the dependency manager when at least one dependency
153 * become unsatisfied or when the component is shutting down because for
154 * example bundle is being stopped.
158 this.controller.removeMessageListener(OFType.PACKET_IN, this);
159 this.controller.removeSwitchStateListener(this);
161 this.inventoryShimInternalListeners.clear();
162 this.containerMap.clear();
163 this.controller = null;
167 public void receive(ISwitch sw, OFMessage msg) {
169 if (msg instanceof OFPortStatus) {
170 handlePortStatusMessage(sw, (OFPortStatus) msg);
172 } catch (ConstructionException e) {
178 protected void handlePortStatusMessage(ISwitch sw, OFPortStatus m)
179 throws ConstructionException {
180 Node node = new Node(NodeIDType.OPENFLOW, sw.getId());
181 NodeConnector nodeConnector = PortConverter.toNodeConnector(m.getDesc()
182 .getPortNumber(), node);
183 UpdateType type = null;
185 if (m.getReason() == (byte) OFPortReason.OFPPR_ADD.ordinal()) {
186 type = UpdateType.ADDED;
187 } else if (m.getReason() == (byte) OFPortReason.OFPPR_DELETE.ordinal()) {
188 type = UpdateType.REMOVED;
189 } else if (m.getReason() == (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
190 type = UpdateType.CHANGED;
193 logger.trace("handlePortStatusMessage {} type {}", nodeConnector, type);
196 // get node connector properties
197 Set<Property> props = InventoryServiceHelper.OFPortToProps(m
199 notifyInventoryShimListener(nodeConnector, type, props);
204 public void switchAdded(ISwitch sw) {
208 // Add all the nodeConnectors of this switch
209 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
210 .OFSwitchToProps(sw);
211 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps.entrySet()) {
212 notifyInventoryShimListener(entry.getKey(), UpdateType.ADDED,
221 public void switchDeleted(ISwitch sw) {
229 public void containerModeUpdated(UpdateType t) {
234 public void tagUpdated(String containerName, Node n, short oldTag,
235 short newTag, UpdateType t) {
239 public void containerFlowUpdated(String containerName,
240 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
244 public void nodeConnectorUpdated(String containerName, NodeConnector p,
246 if (this.containerMap == null) {
247 logger.error("containerMap is NULL");
250 List<String> containers = this.containerMap.get(p);
251 if (containers == null) {
252 containers = new CopyOnWriteArrayList<String>();
254 boolean updateMap = false;
257 if (!containers.contains(containerName)) {
258 containers.add(containerName);
263 if (containers.contains(containerName)) {
264 containers.remove(containerName);
272 if (containers.isEmpty()) {
273 // Do cleanup to reduce memory footprint if no
274 // elements to be tracked
275 this.containerMap.remove(p);
277 this.containerMap.put(p, containers);
281 // notify InventoryService
282 notifyInventoryShimInternalListener(containerName, p, t, null);
285 private void notifyInventoryShimExternalListener(Node node,
286 UpdateType type, Set<Property> props) {
287 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
288 s.updateNode(node, type, props);
292 private void notifyInventoryShimExternalListener(
293 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
294 for (IInventoryShimExternalListener s : this.inventoryShimExternalListeners) {
295 s.updateNodeConnector(nodeConnector, type, props);
299 private void notifyInventoryShimInternalListener(String container,
300 NodeConnector nodeConnector, UpdateType type, Set<Property> props) {
301 IInventoryShimInternalListener inventoryShimInternalListener = inventoryShimInternalListeners
303 if (inventoryShimInternalListener != null) {
304 inventoryShimInternalListener.updateNodeConnector(nodeConnector,
307 "notifyInventoryShimInternalListener {} type {} for container {}",
308 nodeConnector, type, container);
313 * Notify all internal and external listeners
315 private void notifyInventoryShimListener(NodeConnector nodeConnector,
316 UpdateType type, Set<Property> props) {
317 // Always notify default InventoryService. Store properties in default
319 notifyInventoryShimInternalListener(GlobalConstants.DEFAULT.toString(),
320 nodeConnector, type, props);
322 // Now notify other containers
323 List<String> containers = containerMap.get(nodeConnector);
324 if (containers != null) {
325 for (String container : containers) {
326 // no property stored in container components.
327 notifyInventoryShimInternalListener(container, nodeConnector,
332 // Notify DiscoveryService
333 notifyInventoryShimExternalListener(nodeConnector, type, props);
337 * Notify all internal and external listeners
339 private void notifyInventoryShimListener(Node node, UpdateType type,
340 Set<Property> props) {
343 // Notify only the default Inventory Service
344 IInventoryShimInternalListener inventoryShimDefaultListener = inventoryShimInternalListeners
345 .get(GlobalConstants.DEFAULT.toString());
346 if (inventoryShimDefaultListener != null) {
347 inventoryShimDefaultListener.updateNode(node, type, props);
351 // Notify all Inventory Service containers
352 for (IInventoryShimInternalListener inventoryShimInternalListener : inventoryShimInternalListeners
354 inventoryShimInternalListener.updateNode(node, type, null);
358 // Notify only the default Inventory Service
359 inventoryShimDefaultListener = inventoryShimInternalListeners
360 .get(GlobalConstants.DEFAULT.toString());
361 if (inventoryShimDefaultListener != null) {
362 inventoryShimDefaultListener.updateNode(node, type, props);
369 // Notify external listener
370 notifyInventoryShimExternalListener(node, type, props);
373 private void addNode(ISwitch sw) {
376 node = new Node(NodeIDType.OPENFLOW, sw.getId());
377 } catch (ConstructionException e) {
378 logger.error("{}", e.getMessage());
382 UpdateType type = UpdateType.ADDED;
384 Set<Property> props = new HashSet<Property>();
385 Long sid = (Long) node.getID();
387 Date connectedSince = controller.getSwitches().get(sid)
389 Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
391 props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
393 byte tables = sw.getTables();
394 Tables t = new Tables(tables);
398 int cap = sw.getCapabilities();
399 Capabilities c = new Capabilities(cap);
403 int act = sw.getActions();
404 Actions a = new Actions(act);
408 int buffers = sw.getBuffers();
409 Buffers b = new Buffers(buffers);
413 // Notify all internal and external listeners
414 notifyInventoryShimListener(node, type, props);
417 private void removeNode(ISwitch sw) {
420 node = new Node(NodeIDType.OPENFLOW, sw.getId());
421 } catch (ConstructionException e) {
422 logger.error("{}", e.getMessage());
426 UpdateType type = UpdateType.REMOVED;
428 // Notify all internal and external listeners
429 notifyInventoryShimListener(node, type, null);
432 private void startService() {
433 // Get a snapshot of all the existing switches
434 Map<Long, ISwitch> switches = this.controller.getSwitches();
435 for (ISwitch sw : switches.values()) {
441 public void descriptionRefreshed(Long switchId,
442 OFDescriptionStatistics descriptionStats) {
445 node = new Node(NodeIDType.OPENFLOW, switchId);
446 } catch (ConstructionException e) {
447 logger.error("{}", e.getMessage());
451 Set<Property> properties = new HashSet<Property>(1);
452 Description desc = new Description(
453 descriptionStats.getDatapathDescription());
454 properties.add(desc);
456 // Notify all internal and external listeners
457 notifyInventoryShimListener(node, UpdateType.CHANGED, properties);