3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.protocol_plugin.openflow.internal;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.Dictionary;
15 import java.util.HashMap;
16 import java.util.HashSet;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
22 import org.apache.felix.dm.Component;
23 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimInternalListener;
24 import org.opendaylight.controller.protocol_plugin.openflow.IStatisticsListener;
25 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
26 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
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.Description;
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.inventory.IPluginInInventoryService;
40 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
41 import org.opendaylight.controller.sal.utils.GlobalConstants;
42 import org.openflow.protocol.statistics.OFDescriptionStatistics;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * The class describes inventory service protocol plugin. One instance per
48 * container of the network. Each instance gets container specific inventory
49 * events from InventoryServiceShim. It interacts with SAL to pass inventory
50 * data to the upper application.
54 public class InventoryService implements IInventoryShimInternalListener,
55 IPluginInInventoryService, IStatisticsListener {
56 protected static final Logger logger = LoggerFactory
57 .getLogger(InventoryService.class);
58 private Set<IPluginOutInventoryService> pluginOutInventoryServices = Collections
59 .synchronizedSet(new HashSet<IPluginOutInventoryService>());
60 private IController controller = null;
61 private ConcurrentMap<Node, Map<String, Property>> nodeProps; // properties are maintained in default container only
62 private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in default container only
63 private boolean isDefaultContainer = false;
65 void setController(IController s) {
69 void unsetController(IController s) {
70 if (this.controller == s) {
71 this.controller = null;
76 * Function called by the dependency manager when all the required
77 * dependencies are satisfied
80 @SuppressWarnings("rawtypes")
81 void init(Component c) {
82 logger.trace("INIT called!");
84 Dictionary props = c.getServiceProperties();
86 String containerName = (String) props.get("containerName");
87 isDefaultContainer = containerName.equals(GlobalConstants.DEFAULT
91 nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
92 nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
96 * Function called by the dependency manager when at least one dependency
97 * become unsatisfied or when the component is shutting down because for
98 * example bundle is being stopped.
102 logger.trace("DESTROY called!");
106 * Function called by dependency manager after "init ()" is called and after
107 * the services provided by the class are registered in the service registry
111 logger.trace("START called!");
115 * Function called by the dependency manager before the services exported by
116 * the component are unregistered, this will be followed by a "destroy ()"
121 logger.trace("STOP called!");
124 public void setPluginOutInventoryServices(IPluginOutInventoryService service) {
125 logger.trace("Got a service set request {}", service);
126 if (this.pluginOutInventoryServices != null) {
127 this.pluginOutInventoryServices.add(service);
131 public void unsetPluginOutInventoryServices(
132 IPluginOutInventoryService service) {
133 logger.trace("Got a service UNset request");
134 if (this.pluginOutInventoryServices != null) {
135 this.pluginOutInventoryServices.remove(service);
139 protected Node OFSwitchToNode(ISwitch sw) {
141 Object id = sw.getId();
144 node = new Node(NodeIDType.OPENFLOW, id);
145 } catch (ConstructionException e) {
153 * Retrieve nodes from openflow
156 public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
157 if (nodeProps == null)
159 Map<Long, ISwitch> switches = controller.getSwitches();
160 for (Map.Entry<Long, ISwitch> entry : switches.entrySet()) {
161 ISwitch sw = entry.getValue();
162 Node node = OFSwitchToNode(sw);
163 Map<String, Property> propMap = null;
164 if (isDefaultContainer) {
165 propMap = new HashMap<String, Property>();
166 byte tables = sw.getTables();
167 Tables t = new Tables(tables);
169 propMap.put(Tables.TablesPropName,t);
171 int cap = sw.getCapabilities();
172 Capabilities c = new Capabilities(cap);
174 propMap.put(Capabilities.CapabilitiesPropName, c);
176 int act = sw.getActions();
177 Actions a = new Actions(act);
179 propMap.put(Actions.ActionsPropName,a);
181 int buffers = sw.getBuffers();
182 Buffers b = new Buffers(buffers);
184 propMap.put(Buffers.BuffersPropName,b);
186 Date connectedSince = sw.getConnectedDate();
187 Long connectedSinceTime = (connectedSince == null) ? 0
188 : connectedSince.getTime();
189 TimeStamp timeStamp = new TimeStamp(connectedSinceTime,
191 propMap.put(TimeStamp.TimeStampPropName, timeStamp);
192 nodeProps.put(node, propMap);
199 public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
201 if (nodeConnectorProps == null)
204 if (isDefaultContainer && refresh) {
205 Map<Long, ISwitch> switches = controller.getSwitches();
206 for (ISwitch sw : switches.values()) {
207 Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
208 .OFSwitchToProps(sw);
209 for (Map.Entry<NodeConnector, Set<Property>> entry : ncProps
211 updateNodeConnector(entry.getKey(), UpdateType.ADDED, entry
217 return nodeConnectorProps;
221 public void updateNodeConnector(NodeConnector nodeConnector,
222 UpdateType type, Set<Property> props) {
223 logger.trace("NodeConnector id " + nodeConnector.getID()
224 + " type " + nodeConnector.getType() + " "
225 + type.getName() + " for Node id "
226 + nodeConnector.getNode().getID());
228 if (nodeConnectorProps == null) {
233 Map<String, Property> propMap = nodeConnectorProps
239 propMap = new HashMap<String, Property>();
242 for (Property prop : props) {
243 propMap.put(prop.getName(), prop);
246 nodeConnectorProps.put(nodeConnector, propMap);
249 nodeConnectorProps.remove(nodeConnector);
255 // update sal and discovery
256 synchronized (pluginOutInventoryServices) {
257 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
258 service.updateNodeConnector(nodeConnector, type, props);
263 private void addNode(Node node, Set<Property> props) {
264 logger.trace("{} added", node);
265 if (nodeProps == null)
268 // update local cache
269 Map<String, Property> propMap = new HashMap<String, Property>();
270 for (Property prop : props) {
271 propMap.put(prop.getName(), prop);
273 nodeProps.put(node, propMap);
276 synchronized (pluginOutInventoryServices) {
277 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
278 service.updateNode(node, UpdateType.ADDED, props);
283 private void removeNode(Node node) {
284 logger.trace("{} removed", node);
285 if (nodeProps == null)
288 // update local cache
289 nodeProps.remove(node);
291 Set<NodeConnector> removeSet = new HashSet<NodeConnector>();
292 for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) {
293 if (nodeConnector.getNode().equals(node)) {
294 removeSet.add(nodeConnector);
297 for (NodeConnector nodeConnector : removeSet) {
298 nodeConnectorProps.remove(nodeConnector);
302 synchronized (pluginOutInventoryServices) {
303 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
304 service.updateNode(node, UpdateType.REMOVED, null);
310 private void updateSwitchProperty(Long switchId, Set<Property> propSet) {
311 // update local cache
312 Node node = OFSwitchToNode(controller.getSwitch(switchId));
313 Map<String, Property> propMap = nodeProps.get(node);
314 if (propMap == null) {
315 propMap = new HashMap<String, Property>();
318 boolean change = false;
319 for (Property prop : propSet) {
320 String propertyName = prop.getName();
321 Property currentProp = propMap.get(propertyName);
322 if (!prop.equals(currentProp)) {
324 propMap.put(propertyName, prop);
327 nodeProps.put(node, propMap);
329 // Update sal if any of the properties has changed
331 synchronized (pluginOutInventoryServices) {
332 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
333 service.updateNode(node, UpdateType.CHANGED, propSet);
340 public void updateNode(Node node, UpdateType type, Set<Property> props) {
343 addNode(node, props);
354 public void descriptionRefreshed(Long switchId,
355 OFDescriptionStatistics descriptionStats) {
357 Set<Property> propSet = new HashSet<Property>(1);
359 new Description(descriptionStats.getDatapathDescription());
361 this.updateSwitchProperty(switchId, propSet);