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.IInventoryProvider;
23 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimInternalListener;
24 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
25 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
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.Node;
31 import org.opendaylight.controller.sal.core.Node.NodeIDType;
32 import org.opendaylight.controller.sal.core.NodeConnector;
33 import org.opendaylight.controller.sal.core.Property;
34 import org.opendaylight.controller.sal.core.Tables;
35 import org.opendaylight.controller.sal.core.TimeStamp;
36 import org.opendaylight.controller.sal.core.UpdateType;
37 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
38 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
39 import org.opendaylight.controller.sal.utils.GlobalConstants;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * The class describes inventory service protocol plugin. One instance per
45 * container of the network. Each instance gets container specific inventory
46 * events from InventoryServiceShim. It interacts with SAL to pass inventory
47 * data to the upper application.
51 public class InventoryService implements IInventoryShimInternalListener,
52 IPluginInInventoryService, IInventoryProvider {
53 protected static final Logger logger = LoggerFactory
54 .getLogger(InventoryService.class);
55 private Set<IPluginOutInventoryService> pluginOutInventoryServices = Collections
56 .synchronizedSet(new HashSet<IPluginOutInventoryService>());
57 private IController controller = null;
58 private ConcurrentMap<Node, Map<String, Property>> nodeProps; // properties are maintained in global container only
59 private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in global container only
60 private boolean isDefaultContainer = false;
62 void setController(IController s) {
66 void unsetController(IController s) {
67 if (this.controller == s) {
68 this.controller = null;
73 * Function called by the dependency manager when all the required
74 * dependencies are satisfied
77 @SuppressWarnings("rawtypes")
78 void init(Component c) {
79 logger.trace("INIT called!");
81 Dictionary props = c.getServiceProperties();
83 String containerName = (String) props.get("containerName");
84 isDefaultContainer = containerName.equals(GlobalConstants.DEFAULT
88 nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
89 nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
93 * Function called by the dependency manager when at least one dependency
94 * become unsatisfied or when the component is shutting down because for
95 * example bundle is being stopped.
99 logger.trace("DESTROY called!");
103 * Function called by dependency manager after "init ()" is called and after
104 * the services provided by the class are registered in the service registry
108 logger.trace("START called!");
112 * Function called by the dependency manager before the services exported by
113 * the component are unregistered, this will be followed by a "destroy ()"
118 logger.trace("STOP called!");
121 public void setPluginOutInventoryServices(IPluginOutInventoryService service) {
122 logger.trace("Got a service set request {}", service);
123 if (this.pluginOutInventoryServices != null) {
124 this.pluginOutInventoryServices.add(service);
128 public void unsetPluginOutInventoryServices(
129 IPluginOutInventoryService service) {
130 logger.trace("Got a service UNset request");
131 if (this.pluginOutInventoryServices != null) {
132 this.pluginOutInventoryServices.remove(service);
136 protected Node OFSwitchToNode(ISwitch sw) {
138 Object id = sw.getId();
141 node = new Node(NodeIDType.OPENFLOW, id);
142 } catch (ConstructionException e) {
150 * Retrieve nodes from openflow
153 public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
154 if (nodeProps == null)
156 Map<Long, ISwitch> switches = controller.getSwitches();
157 for (Map.Entry<Long, ISwitch> entry : switches.entrySet()) {
158 ISwitch sw = entry.getValue();
159 Node node = OFSwitchToNode(sw);
160 Map<String, Property> propMap = null;
161 if (isDefaultContainer) {
162 propMap = new HashMap<String, Property>();
163 byte tables = sw.getTables();
164 Tables t = new Tables(tables);
166 propMap.put(Tables.TablesPropName, t);
168 int cap = sw.getCapabilities();
169 Capabilities c = new Capabilities(cap);
171 propMap.put(Capabilities.CapabilitiesPropName, c);
173 int act = sw.getActions();
174 Actions a = new Actions(act);
176 propMap.put(Actions.ActionsPropName, a);
178 int buffers = sw.getBuffers();
179 Buffers b = new Buffers(buffers);
181 propMap.put(Buffers.BuffersPropName, b);
183 Date connectedSince = sw.getConnectedDate();
184 Long connectedSinceTime = (connectedSince == null) ? 0
185 : connectedSince.getTime();
186 TimeStamp timeStamp = new TimeStamp(connectedSinceTime,
188 propMap.put(TimeStamp.TimeStampPropName, timeStamp);
189 nodeProps.put(node, propMap);
196 public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
198 if (nodeConnectorProps == null) {
202 if (isDefaultContainer && refresh) {
203 Map<Long, ISwitch> switches = controller.getSwitches();
204 for (ISwitch sw : switches.values()) {
205 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
206 .OFSwitchToProps(sw);
207 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps
209 updateNodeConnector(entry.getKey(), UpdateType.ADDED,
215 return nodeConnectorProps;
219 public void updateNodeConnector(NodeConnector nodeConnector,
220 UpdateType type, Set<Property> props) {
221 logger.trace("updateNodeConnector {} type {}", nodeConnector,
223 if (nodeConnectorProps == null) {
224 logger.trace("nodeConnectorProps is null");
228 Map<String, Property> propMap = nodeConnectorProps.get(nodeConnector);
232 if (propMap == null) {
233 propMap = new HashMap<String, Property>();
236 for (Property prop : props) {
237 propMap.put(prop.getName(), prop);
240 nodeConnectorProps.put(nodeConnector, propMap);
243 nodeConnectorProps.remove(nodeConnector);
249 // update sal and discovery
250 synchronized (pluginOutInventoryServices) {
251 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
252 service.updateNodeConnector(nodeConnector, type, props);
257 private void addNode(Node node, Set<Property> props) {
258 logger.trace("{} added, props: {}", node, props);
259 if (nodeProps == null) {
263 // update local cache
264 Map<String, Property> propMap = new HashMap<String, Property>();
265 for (Property prop : props) {
266 propMap.put(prop.getName(), prop);
268 nodeProps.put(node, propMap);
271 synchronized (pluginOutInventoryServices) {
272 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
273 service.updateNode(node, UpdateType.ADDED, props);
278 private void removeNode(Node node) {
279 logger.trace("{} removed", node);
280 if (nodeProps == null)
283 // update local cache
284 nodeProps.remove(node);
286 Set<NodeConnector> removeSet = new HashSet<NodeConnector>();
287 for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) {
288 if (nodeConnector.getNode().equals(node)) {
289 removeSet.add(nodeConnector);
292 for (NodeConnector nodeConnector : removeSet) {
293 nodeConnectorProps.remove(nodeConnector);
297 synchronized (pluginOutInventoryServices) {
298 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
299 service.updateNode(node, UpdateType.REMOVED, null);
304 private void updateNode(Node node, Set<Property> properties) {
305 logger.trace("{} updated, props: {}", node, properties);
306 if (nodeProps == null || !nodeProps.containsKey(node) ||
307 properties == null || properties.isEmpty()) {
311 // Update local cache with new properties
312 Set<Property> newProperties = new HashSet<Property>(properties.size());
313 Map<String, Property> propertyMap = nodeProps.get(node);
314 for (Property property : properties) {
315 String name = property.getName();
316 Property currentProperty = propertyMap.get(name);
317 if (!property.equals(currentProperty)) {
318 propertyMap.put(name, property);
319 newProperties.add(property);
323 // Update SAL if we got new properties
324 if (!newProperties.isEmpty()) {
325 synchronized (pluginOutInventoryServices) {
326 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
327 service.updateNode(node, UpdateType.CHANGED, newProperties);
334 public void updateNode(Node node, UpdateType type, Set<Property> props) {
337 addNode(node, props);
343 updateNode(node, props);