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.IStatisticsListener;
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.Description;
31 import org.opendaylight.controller.sal.core.Node;
32 import org.opendaylight.controller.sal.core.Node.NodeIDType;
33 import org.opendaylight.controller.sal.core.NodeConnector;
34 import org.opendaylight.controller.sal.core.Property;
35 import org.opendaylight.controller.sal.core.Tables;
36 import org.opendaylight.controller.sal.core.TimeStamp;
37 import org.opendaylight.controller.sal.core.UpdateType;
38 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
39 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
40 import org.opendaylight.controller.sal.utils.GlobalConstants;
41 import org.openflow.protocol.statistics.OFDescriptionStatistics;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The class describes inventory service protocol plugin. One instance per
47 * container of the network. Each instance gets container specific inventory
48 * events from InventoryServiceShim. It interacts with SAL to pass inventory
49 * data to the upper application.
53 public class InventoryService implements IInventoryShimInternalListener,
54 IPluginInInventoryService, IStatisticsListener {
55 protected static final Logger logger = LoggerFactory
56 .getLogger(InventoryService.class);
57 private Set<IPluginOutInventoryService> pluginOutInventoryServices = Collections
58 .synchronizedSet(new HashSet<IPluginOutInventoryService>());
59 private IController controller = null;
60 private ConcurrentMap<Node, Map<String, Property>> nodeProps; // properties are maintained in global container only
61 private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in global container only
62 private boolean isDefaultContainer = false;
64 void setController(IController s) {
68 void unsetController(IController s) {
69 if (this.controller == s) {
70 this.controller = null;
75 * Function called by the dependency manager when all the required
76 * dependencies are satisfied
79 @SuppressWarnings("rawtypes")
80 void init(Component c) {
81 logger.trace("INIT called!");
83 Dictionary props = c.getServiceProperties();
85 String containerName = (String) props.get("containerName");
86 isDefaultContainer = containerName.equals(GlobalConstants.DEFAULT
90 nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
91 nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
95 * Function called by the dependency manager when at least one dependency
96 * become unsatisfied or when the component is shutting down because for
97 * example bundle is being stopped.
101 logger.trace("DESTROY called!");
105 * Function called by dependency manager after "init ()" is called and after
106 * the services provided by the class are registered in the service registry
110 logger.trace("START called!");
114 * Function called by the dependency manager before the services exported by
115 * the component are unregistered, this will be followed by a "destroy ()"
120 logger.trace("STOP called!");
123 public void setPluginOutInventoryServices(IPluginOutInventoryService service) {
124 logger.trace("Got a service set request {}", service);
125 if (this.pluginOutInventoryServices != null) {
126 this.pluginOutInventoryServices.add(service);
130 public void unsetPluginOutInventoryServices(
131 IPluginOutInventoryService service) {
132 logger.trace("Got a service UNset request");
133 if (this.pluginOutInventoryServices != null) {
134 this.pluginOutInventoryServices.remove(service);
138 protected Node OFSwitchToNode(ISwitch sw) {
140 Object id = sw.getId();
143 node = new Node(NodeIDType.OPENFLOW, id);
144 } catch (ConstructionException e) {
152 * Retrieve nodes from openflow
155 public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
156 if (nodeProps == null)
158 Map<Long, ISwitch> switches = controller.getSwitches();
159 for (Map.Entry<Long, ISwitch> entry : switches.entrySet()) {
160 ISwitch sw = entry.getValue();
161 Node node = OFSwitchToNode(sw);
162 Map<String, Property> propMap = null;
163 if (isDefaultContainer) {
164 propMap = new HashMap<String, Property>();
165 byte tables = sw.getTables();
166 Tables t = new Tables(tables);
168 propMap.put(Tables.TablesPropName, t);
170 int cap = sw.getCapabilities();
171 Capabilities c = new Capabilities(cap);
173 propMap.put(Capabilities.CapabilitiesPropName, c);
175 int act = sw.getActions();
176 Actions a = new Actions(act);
178 propMap.put(Actions.ActionsPropName, a);
180 int buffers = sw.getBuffers();
181 Buffers b = new Buffers(buffers);
183 propMap.put(Buffers.BuffersPropName, b);
185 Date connectedSince = sw.getConnectedDate();
186 Long connectedSinceTime = (connectedSince == null) ? 0
187 : connectedSince.getTime();
188 TimeStamp timeStamp = new TimeStamp(connectedSinceTime,
190 propMap.put(TimeStamp.TimeStampPropName, timeStamp);
191 nodeProps.put(node, propMap);
198 public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
200 if (nodeConnectorProps == null)
203 if (isDefaultContainer && refresh) {
204 Map<Long, ISwitch> switches = controller.getSwitches();
205 for (ISwitch sw : switches.values()) {
206 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
207 .OFSwitchToProps(sw);
208 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps
210 updateNodeConnector(entry.getKey(), UpdateType.ADDED,
216 return nodeConnectorProps;
220 public void updateNodeConnector(NodeConnector nodeConnector,
221 UpdateType type, Set<Property> props) {
222 logger.trace("updateNodeConnector {} type {}", nodeConnector,
224 if (nodeConnectorProps == null) {
225 logger.trace("nodeConnectorProps is null");
229 Map<String, Property> propMap = nodeConnectorProps.get(nodeConnector);
234 propMap = new HashMap<String, Property>();
237 for (Property prop : props) {
238 propMap.put(prop.getName(), prop);
241 nodeConnectorProps.put(nodeConnector, propMap);
244 nodeConnectorProps.remove(nodeConnector);
250 // update sal and discovery
251 synchronized (pluginOutInventoryServices) {
252 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
253 service.updateNodeConnector(nodeConnector, type, props);
258 private void addNode(Node node, Set<Property> props) {
259 logger.trace("{} added", node);
260 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 updateSwitchProperty(Long switchId, Set<Property> propSet) {
305 // update local cache
306 Node node = OFSwitchToNode(controller.getSwitch(switchId));
307 Map<String, Property> propMap = nodeProps.get(node);
308 if (propMap == null) {
309 propMap = new HashMap<String, Property>();
312 boolean change = false;
313 for (Property prop : propSet) {
314 String propertyName = prop.getName();
315 Property currentProp = propMap.get(propertyName);
316 if (!prop.equals(currentProp)) {
318 propMap.put(propertyName, prop);
321 nodeProps.put(node, propMap);
323 // Update sal if any of the properties has changed
325 synchronized (pluginOutInventoryServices) {
326 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
327 service.updateNode(node, UpdateType.CHANGED, propSet);
334 public void updateNode(Node node, UpdateType type, Set<Property> props) {
337 addNode(node, props);
348 public void descriptionRefreshed(Long switchId,
349 OFDescriptionStatistics descriptionStats) {
351 Set<Property> propSet = new HashSet<Property>(1);
352 Description desc = new Description(
353 descriptionStats.getDatapathDescription());
355 this.updateSwitchProperty(switchId, propSet);