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.IOFInventoryService;
25 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
26 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import org.opendaylight.controller.sal.core.ConstructionException;
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.TimeStamp;
36 import org.opendaylight.controller.sal.core.Tables;
37 import org.opendaylight.controller.sal.core.Actions;
38 import org.opendaylight.controller.sal.core.Buffers;
39 import org.opendaylight.controller.sal.core.Capabilities;
40 import org.opendaylight.controller.sal.core.UpdateType;
41 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
42 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
43 import org.opendaylight.controller.sal.utils.GlobalConstants;
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, IOFInventoryService {
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 default container only
61 private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in default 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, entry
216 return nodeConnectorProps;
220 public void updateNodeConnector(NodeConnector nodeConnector,
221 UpdateType type, Set<Property> props) {
222 logger.trace("NodeConnector id " + nodeConnector.getID()
223 + " type " + nodeConnector.getType() + " "
224 + type.getName() + " for Node id "
225 + nodeConnector.getNode().getID());
227 if (nodeConnectorProps == null)
230 synchronized (nodeConnectorProps) {
231 Map<String, Property> propMap = nodeConnectorProps
237 propMap = new HashMap<String, Property>();
240 for (Property prop : props) {
241 propMap.put(prop.getName(), prop);
244 nodeConnectorProps.put(nodeConnector, propMap);
247 nodeConnectorProps.remove(nodeConnector);
254 // update sal and discovery
255 synchronized (pluginOutInventoryServices) {
256 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
257 service.updateNodeConnector(nodeConnector, type, props);
262 private void addNode(Node node, Set<Property> props) {
263 logger.trace("{} added", node);
264 if (nodeProps == null)
267 // update local cache
268 Map<String, Property> propMap = new HashMap<String, Property>();
269 for (Property prop : props) {
270 propMap.put(prop.getName(), prop);
272 nodeProps.put(node, propMap);
275 synchronized (pluginOutInventoryServices) {
276 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
277 service.updateNode(node, UpdateType.ADDED, props);
282 private void removeNode(Node node) {
283 logger.trace("{} removed", node);
284 if (nodeProps == null)
287 // update local cache
288 nodeProps.remove(node);
290 Set<NodeConnector> removeSet = new HashSet<NodeConnector>();
291 for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) {
292 if (nodeConnector.getNode().equals(node)) {
293 removeSet.add(nodeConnector);
296 for (NodeConnector nodeConnector : removeSet) {
297 nodeConnectorProps.remove(nodeConnector);
301 synchronized (pluginOutInventoryServices) {
302 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
303 service.updateNode(node, UpdateType.REMOVED, null);
309 * Function called by other protocol plugin modules to notify Inventory Service
310 * that a property has changed for the specified switch
313 public void updateSwitchProperty(Long switchId, Set<Property> propSet) {
314 // update local cache
315 Node node = OFSwitchToNode(controller.getSwitches().get(switchId));
316 Map<String, Property> propMap = nodeProps.get(node);
318 propMap = new HashMap<String, Property>();
319 for (Property prop : propSet) {
320 propMap.put(prop.getName(), prop);
322 nodeProps.put(node, propMap);
325 synchronized (pluginOutInventoryServices) {
326 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
327 service.updateNode(node, UpdateType.CHANGED, propSet);
333 public void updateNode(Node node, UpdateType type, Set<Property> props) {
336 addNode(node, props);