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.Collections;
12 import java.util.Date;
13 import java.util.Dictionary;
14 import java.util.HashMap;
15 import java.util.HashSet;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
21 import org.apache.felix.dm.Component;
22 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimInternalListener;
23 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
24 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
25 import org.opendaylight.controller.sal.core.Actions;
26 import org.opendaylight.controller.sal.core.Buffers;
27 import org.opendaylight.controller.sal.core.Capabilities;
28 import org.opendaylight.controller.sal.core.ConstructionException;
29 import org.opendaylight.controller.sal.core.Node;
30 import org.opendaylight.controller.sal.core.Node.NodeIDType;
31 import org.opendaylight.controller.sal.core.NodeConnector;
32 import org.opendaylight.controller.sal.core.Property;
33 import org.opendaylight.controller.sal.core.Tables;
34 import org.opendaylight.controller.sal.core.TimeStamp;
35 import org.opendaylight.controller.sal.core.UpdateType;
36 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
37 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
38 import org.opendaylight.controller.sal.utils.GlobalConstants;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The class describes inventory service protocol plugin. One instance per
44 * container of the network. Each instance gets container specific inventory
45 * events from InventoryServiceShim. It interacts with SAL to pass inventory
46 * data to the upper application.
50 public class InventoryService implements IInventoryShimInternalListener,
51 IPluginInInventoryService {
52 protected static final Logger logger = LoggerFactory
53 .getLogger(InventoryService.class);
54 private Set<IPluginOutInventoryService> pluginOutInventoryServices = Collections
55 .synchronizedSet(new HashSet<IPluginOutInventoryService>());
56 private IController controller = null;
57 private ConcurrentMap<Node, Map<String, Property>> nodeProps; // properties are maintained in global container only
58 private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in global container only
59 private boolean isDefaultContainer = false;
61 void setController(IController s) {
65 void unsetController(IController s) {
66 if (this.controller == s) {
67 this.controller = null;
72 * Function called by the dependency manager when all the required
73 * dependencies are satisfied
76 @SuppressWarnings("rawtypes")
77 void init(Component c) {
78 logger.trace("INIT called!");
80 Dictionary props = c.getServiceProperties();
82 String containerName = (String) props.get("containerName");
83 isDefaultContainer = containerName.equals(GlobalConstants.DEFAULT
87 nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
88 nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
92 * Function called by the dependency manager when at least one dependency
93 * become unsatisfied or when the component is shutting down because for
94 * example bundle is being stopped.
98 logger.trace("DESTROY called!");
102 * Function called by dependency manager after "init ()" is called and after
103 * the services provided by the class are registered in the service registry
107 logger.trace("START called!");
111 * Function called by the dependency manager before the services exported by
112 * the component are unregistered, this will be followed by a "destroy ()"
117 logger.trace("STOP called!");
120 public void setPluginOutInventoryServices(IPluginOutInventoryService service) {
121 logger.trace("Got a service set request {}", service);
122 if (this.pluginOutInventoryServices != null) {
123 this.pluginOutInventoryServices.add(service);
127 public void unsetPluginOutInventoryServices(
128 IPluginOutInventoryService service) {
129 logger.trace("Got a service UNset request");
130 if (this.pluginOutInventoryServices != null) {
131 this.pluginOutInventoryServices.remove(service);
135 protected Node OFSwitchToNode(ISwitch sw) {
137 Object id = sw.getId();
140 node = new Node(NodeIDType.OPENFLOW, id);
141 } catch (ConstructionException e) {
149 * Retrieve nodes from openflow
152 public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
153 if (nodeProps == null)
155 Map<Long, ISwitch> switches = controller.getSwitches();
156 for (Map.Entry<Long, ISwitch> entry : switches.entrySet()) {
157 ISwitch sw = entry.getValue();
158 Node node = OFSwitchToNode(sw);
159 Map<String, Property> propMap = null;
160 if (isDefaultContainer) {
161 propMap = new HashMap<String, Property>();
162 byte tables = sw.getTables();
163 Tables t = new Tables(tables);
165 propMap.put(Tables.TablesPropName, t);
167 int cap = sw.getCapabilities();
168 Capabilities c = new Capabilities(cap);
170 propMap.put(Capabilities.CapabilitiesPropName, c);
172 int act = sw.getActions();
173 Actions a = new Actions(act);
175 propMap.put(Actions.ActionsPropName, a);
177 int buffers = sw.getBuffers();
178 Buffers b = new Buffers(buffers);
180 propMap.put(Buffers.BuffersPropName, b);
182 Date connectedSince = sw.getConnectedDate();
183 Long connectedSinceTime = (connectedSince == null) ? 0
184 : connectedSince.getTime();
185 TimeStamp timeStamp = new TimeStamp(connectedSinceTime,
187 propMap.put(TimeStamp.TimeStampPropName, timeStamp);
188 nodeProps.put(node, propMap);
195 public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
197 if (nodeConnectorProps == null) {
201 if (isDefaultContainer && refresh) {
202 Map<Long, ISwitch> switches = controller.getSwitches();
203 for (ISwitch sw : switches.values()) {
204 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
205 .OFSwitchToProps(sw);
206 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps
208 updateNodeConnector(entry.getKey(), UpdateType.ADDED,
214 return nodeConnectorProps;
218 public void updateNodeConnector(NodeConnector nodeConnector,
219 UpdateType type, Set<Property> props) {
220 logger.trace("updateNodeConnector {} type {}", nodeConnector,
222 if (nodeConnectorProps == null) {
223 logger.trace("nodeConnectorProps is null");
227 Map<String, Property> propMap = nodeConnectorProps.get(nodeConnector);
231 if (propMap == null) {
232 propMap = new HashMap<String, Property>();
235 for (Property prop : props) {
236 propMap.put(prop.getName(), prop);
239 nodeConnectorProps.put(nodeConnector, propMap);
242 nodeConnectorProps.remove(nodeConnector);
248 // update sal and discovery
249 synchronized (pluginOutInventoryServices) {
250 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
251 service.updateNodeConnector(nodeConnector, type, props);
256 private void addNode(Node node, Set<Property> props) {
257 logger.trace("{} added, props: {}", node, props);
258 if (nodeProps == null) {
262 // update local cache
263 Map<String, Property> propMap = new HashMap<String, Property>();
264 for (Property prop : props) {
265 propMap.put(prop.getName(), prop);
267 nodeProps.put(node, propMap);
270 synchronized (pluginOutInventoryServices) {
271 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
272 service.updateNode(node, UpdateType.ADDED, props);
277 private void removeNode(Node node) {
278 logger.trace("{} removed", node);
279 if (nodeProps == null)
282 // update local cache
283 nodeProps.remove(node);
285 Set<NodeConnector> removeSet = new HashSet<NodeConnector>();
286 for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) {
287 if (nodeConnector.getNode().equals(node)) {
288 removeSet.add(nodeConnector);
291 for (NodeConnector nodeConnector : removeSet) {
292 nodeConnectorProps.remove(nodeConnector);
296 synchronized (pluginOutInventoryServices) {
297 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
298 service.updateNode(node, UpdateType.REMOVED, null);
303 private void updateNode(Node node, Set<Property> properties) {
304 logger.trace("{} updated, props: {}", node, properties);
305 if (nodeProps == null || !nodeProps.containsKey(node) ||
306 properties == null || properties.isEmpty()) {
310 // Update local cache with new properties
311 Set<Property> newProperties = new HashSet<Property>(properties.size());
312 Map<String, Property> propertyMap = nodeProps.get(node);
313 for (Property property : properties) {
314 String name = property.getName();
315 Property currentProperty = propertyMap.get(name);
316 if (!property.equals(currentProperty)) {
317 propertyMap.put(name, property);
318 newProperties.add(property);
322 // Update SAL if we got new properties
323 if (!newProperties.isEmpty()) {
324 synchronized (pluginOutInventoryServices) {
325 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
326 service.updateNode(node, UpdateType.CHANGED, newProperties);
333 public void updateNode(Node node, UpdateType type, Set<Property> props) {
336 addNode(node, props);
342 updateNode(node, props);