2 * Copyright (C) 2013 Red Hat, Inc.
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
8 * Authors : Madhu Venugopal, Brent Salisbury
10 package org.opendaylight.ovsdb.plugin;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.HashSet;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19 import java.util.concurrent.CopyOnWriteArraySet;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ScheduledExecutorService;
22 import java.util.concurrent.TimeUnit;
24 import org.opendaylight.controller.sal.core.ConstructionException;
25 import org.opendaylight.controller.sal.core.Description;
26 import org.opendaylight.controller.sal.core.Node;
27 import org.opendaylight.controller.sal.core.NodeConnector;
28 import org.opendaylight.controller.sal.core.Property;
29 import org.opendaylight.controller.sal.core.UpdateType;
30 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
31 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
32 import org.opendaylight.controller.sal.utils.HexEncode;
33 import org.opendaylight.controller.sal.utils.ServiceHelper;
34 import org.opendaylight.ovsdb.lib.database.DatabaseSchema;
35 import org.opendaylight.ovsdb.lib.message.TableUpdate;
36 import org.opendaylight.ovsdb.lib.message.TableUpdate.Row;
37 import org.opendaylight.ovsdb.lib.message.TableUpdates;
38 import org.opendaylight.ovsdb.lib.notation.OvsDBSet;
39 import org.opendaylight.ovsdb.lib.table.Bridge;
40 import org.opendaylight.ovsdb.lib.table.internal.Table;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import com.google.common.collect.Maps;
47 * Stub Implementation for IPluginInReadService used by SAL
51 public class InventoryService implements IPluginInInventoryService, InventoryServiceInternal {
52 private static final Logger logger = LoggerFactory
53 .getLogger(InventoryService.class);
54 private final Set<IPluginOutInventoryService> pluginOutInventoryServices =
55 new CopyOnWriteArraySet<IPluginOutInventoryService>();
56 private ConcurrentMap<Node, Map<String, Property>> nodeProps;
57 private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps;
58 private Map<Node, NodeDB> dbCache = Maps.newHashMap();
59 private ScheduledExecutorService executor;
62 * Function called by the dependency manager when all the required
63 * dependencies are satisfied
67 nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
68 nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
69 Node.NodeIDType.registerIDType("OVS", String.class);
70 NodeConnector.NodeConnectorIDType.registerIDType("OVS", String.class, "OVS");
71 this.executor = Executors.newSingleThreadScheduledExecutor();
75 * Function called by the dependency manager when at least one dependency
76 * become unsatisfied or when the component is shutting down because for
77 * example bundle is being stopped.
80 public void destroy() {
84 * Function called by dependency manager after "init ()" is called and after
85 * the services provided by the class are registered in the service registry
92 * Function called by the dependency manager before the services exported by
93 * the component are unregistered, this will be followed by a "destroy ()"
98 this.executor.shutdownNow();
101 public void setPluginOutInventoryServices(IPluginOutInventoryService service) {
102 if (this.pluginOutInventoryServices != null) {
103 this.pluginOutInventoryServices.add(service);
107 public void unsetPluginOutInventoryServices(
108 IPluginOutInventoryService service) {
109 if (this.pluginOutInventoryServices != null) {
110 this.pluginOutInventoryServices.remove(service);
115 * Retrieve nodes from openflow
118 public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
123 * Retrieve nodeConnectors from openflow
126 public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
128 return nodeConnectorProps;
133 public Map<String, Map<String, Table<?>>> getCache(Node n) {
134 NodeDB db = dbCache.get(n);
135 if (db == null) return null;
136 return db.getTableCache();
141 public Map<String, Table<?>> getTableCache(Node n, String tableName) {
142 NodeDB db = dbCache.get(n);
143 if (db == null) return null;
144 return db.getTableCache(tableName);
149 public Table<?> getRow(Node n, String tableName, String uuid) {
150 NodeDB db = dbCache.get(n);
151 if (db == null) return null;
152 return db.getRow(tableName, uuid);
156 public void updateRow(Node n, String tableName, String uuid, Table<?> row) {
157 NodeDB db = dbCache.get(n);
162 db.updateRow(tableName, uuid, row);
166 public void removeRow(Node n, String tableName, String uuid) {
167 NodeDB db = dbCache.get(n);
168 if (db != null) db.removeRow(tableName, uuid);
172 public void processTableUpdates(Node n, TableUpdates tableUpdates) {
173 NodeDB db = dbCache.get(n);
179 OVSDBInventoryListener inventoryListener = (OVSDBInventoryListener)ServiceHelper.getGlobalInstance(OVSDBInventoryListener.class, this);
180 Set<Table.Name> available = tableUpdates.availableUpdates();
181 for (Table.Name name : available) {
182 TableUpdate tableUpdate = tableUpdates.getUpdate(name);
183 Collection<TableUpdate.Row<?>> rows = tableUpdate.getRows();
184 for (Row<?> row : rows) {
185 String uuid = row.getId();
186 Table<?> newRow = (Table<?>)row.getNew();
187 Table<?> oldRow = (Table<?>)row.getOld();
188 if (newRow != null) {
189 db.updateRow(name.getName(), uuid, newRow);
190 if (name.getName().equalsIgnoreCase("bridge")) {
191 logger.debug("Received Bridge Table udpate for node {}", n);
192 // OVSDB has the Bridge name info while OpenFlow Spec is not
193 // Clear on that. From a user/manageability standpoint, it is easier
194 // to handle Bridge names compared to dpids.
195 // Updating the Openflow bridge name via the SAL Description update.
196 updateOFBridgeName(n, (Bridge)newRow);
198 if ((oldRow == null) && (inventoryListener != null)) {
199 inventoryListener.rowAdded(n, name.getName(), uuid, newRow);
200 } else if (inventoryListener != null) {
201 inventoryListener.rowUpdated(n, name.getName(), uuid, oldRow, newRow);
203 } else if (oldRow != null) {
204 if (inventoryListener != null) {
205 inventoryListener.rowRemoved(n, name.getName(), uuid, oldRow);
207 db.removeRow(name.getName(), uuid);
213 private void updateOFBridgeName(final Node node, final Bridge bridge) {
214 Runnable updateNameRunnable = new Runnable() {
217 OvsDBSet<String> dpids = bridge.getDatapath_id();
218 String bridgeName = bridge.getName();
219 if (dpids == null || bridgeName == null) return;
220 for (String dpid : dpids) {
221 Long dpidLong = Long.valueOf(HexEncode.stringToLong(dpid));
223 Node ofNode = new Node(Node.NodeIDType.OPENFLOW, dpidLong);
224 Description descProp = new Description(bridgeName);
225 Set<Property> props = new HashSet<Property>();
228 IPluginOutInventoryService salInventoryService = (IPluginOutInventoryService) ServiceHelper.getInstance(
229 IPluginOutInventoryService.class, "default", this);
230 if (salInventoryService != null) {
231 logger.debug("Updating Bridge Name {} on OF node {}", bridgeName, ofNode);
232 salInventoryService.updateNode(ofNode, UpdateType.CHANGED, props);
234 logger.error("Error accessing SAL Inventory plugin");
236 } catch (ConstructionException e) {
237 logger.error("Failed to construct node for " + dpid, e);
242 executor.execute(updateNameRunnable);
243 // Add a delay & re-execute to compensate for the current OpenFlow plugin bug of
244 // overriding the Description with a Null value after the first statistics timer.
245 // Hopefully this will be resolved in the newer version of the Openflow plugin.
246 executor.schedule(updateNameRunnable, 30, TimeUnit.SECONDS);
250 public void printCache(Node n) {
251 NodeDB db = dbCache.get(n);
252 if (db != null) db.printTableCache();
256 public void addNode(Node node, Set<Property> props) {
257 addNodeProperty(node, UpdateType.ADDED, props);
261 public void notifyNodeAdded(Node node) {
262 OVSDBInventoryListener inventoryListener = (OVSDBInventoryListener)ServiceHelper.getGlobalInstance(OVSDBInventoryListener.class, this);
263 if (inventoryListener != null) {
264 inventoryListener.nodeAdded(node);
269 public void addNodeProperty(Node node, UpdateType type, Set<Property> props) {
270 Map<String, Property> nProp = nodeProps.get(node);
271 if (nProp == null) nProp = new HashMap<String, Property>();
272 for (Property prop : props) {
273 nProp.put(prop.getName(), prop);
275 nodeProps.put(node, nProp);
276 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
277 service.updateNode(node, type, props);
282 public DatabaseSchema getDatabaseSchema(Node n) {
283 NodeDB db = dbCache.get(n);
284 if (db != null) return db.getSchema();
289 public void updateDatabaseSchema(Node n, DatabaseSchema schema) {
290 NodeDB db = dbCache.get(n);
295 db.setSchema(schema);
299 public void removeNode(Node node) {
300 OVSDBInventoryListener inventoryListener = (OVSDBInventoryListener)ServiceHelper.getGlobalInstance(OVSDBInventoryListener.class, this);
301 if (inventoryListener != null) {
302 inventoryListener.nodeRemoved(node);
305 for (IPluginOutInventoryService service : pluginOutInventoryServices) {
306 service.updateNode(node, UpdateType.REMOVED, null);
308 nodeProps.remove(node);
309 dbCache.remove(node);