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.impl;
12 import java.net.InetAddress;
15 import java.util.concurrent.ConcurrentMap;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.ScheduledExecutorService;
20 import org.opendaylight.ovsdb.lib.message.TableUpdate;
21 import org.opendaylight.ovsdb.lib.message.TableUpdates;
22 import org.opendaylight.ovsdb.lib.notation.Row;
23 import org.opendaylight.ovsdb.lib.notation.UUID;
24 import org.opendaylight.ovsdb.plugin.internal.NodeDatabase;
25 import org.opendaylight.ovsdb.plugin.api.OvsVswitchdSchemaConstants;
26 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
27 import org.opendaylight.ovsdb.plugin.api.OvsdbInventoryListener;
28 import org.opendaylight.ovsdb.plugin.api.OvsdbInventoryService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import com.google.common.collect.Sets;
33 import com.google.common.collect.Maps;
36 * Stub Implementation for IPluginInReadService used by SAL
40 public class InventoryServiceImpl implements OvsdbInventoryService {
41 private ConcurrentMap<Node, NodeDatabase> dbCache = Maps.newConcurrentMap();
42 private ScheduledExecutorService executor;
43 private OvsdbConfigurationService ovsdbConfigurationService;
45 private Set<OvsdbInventoryListener> ovsdbInventoryListeners = Sets.newCopyOnWriteArraySet();
48 * Function called by the dependency manager when all the required
49 * dependencies are satisfied
53 this.executor = Executors.newSingleThreadScheduledExecutor();
57 * Function called by the dependency manager when at least one dependency
58 * become unsatisfied or when the component is shutting down because for
59 * example bundle is being stopped.
62 public void destroy() {
66 * Function called by dependency manager after "init ()" is called and after
67 * the services provided by the class are registered in the service registry
74 * Function called by the dependency manager before the services exported by
75 * the component are unregistered, this will be followed by a "destroy ()"
80 this.executor.shutdownNow();
83 public void setOvsdbConfigurationService(OvsdbConfigurationService service) {
84 ovsdbConfigurationService = service;
87 public void unsetConfigurationService(OvsdbConfigurationService service) {
88 ovsdbConfigurationService = null;
92 public ConcurrentMap<String, ConcurrentMap<String, Row>> getCache(Node n, String databaseName) {
93 NodeDatabase db = dbCache.get(n);
97 return db.getDatabase(databaseName);
102 public ConcurrentMap<String, Row> getTableCache(Node n, String databaseName, String tableName) {
103 NodeDatabase db = dbCache.get(n);
107 return db.getTableCache(databaseName, tableName);
112 public Row getRow(Node n, String databaseName, String tableName, String uuid) {
113 NodeDatabase db = dbCache.get(n);
117 return db.getRow(databaseName, tableName, uuid);
121 public void updateRow(Node n, String databaseName, String tableName, String uuid, Row row) {
122 NodeDatabase db = dbCache.get(n);
124 db = new NodeDatabase();
127 db.updateRow(databaseName, tableName, uuid, row);
131 public void removeRow(Node n, String databaseName, String tableName, String uuid) {
132 NodeDatabase db = dbCache.get(n);
134 db.removeRow(databaseName, tableName, uuid);
139 public void processTableUpdates(Node n, String databaseName, TableUpdates tableUpdates) {
140 NodeDatabase db = dbCache.get(n);
142 db = new NodeDatabase();
146 for (String tableName : tableUpdates.getUpdates().keySet()) {
147 Map<String, Row> tCache = db.getTableCache(databaseName, tableName);
148 TableUpdate update = tableUpdates.getUpdates().get(tableName);
149 for (UUID uuid : (Set<UUID>)update.getRows().keySet()) {
151 if (update.getNew(uuid) != null) {
152 boolean isNewRow = (tCache == null || tCache.get(uuid.toString()) == null) ? true : false;
153 db.updateRow(databaseName, tableName, uuid.toString(), update.getNew(uuid));
155 this.handleOpenVSwitchSpecialCase(n, databaseName, tableName, uuid);
156 if (!ovsdbInventoryListeners.isEmpty()) {
157 for (OvsdbInventoryListener listener : ovsdbInventoryListeners) {
158 listener.rowAdded(n, tableName, uuid.toString(), update.getNew(uuid));
162 if (!ovsdbInventoryListeners.isEmpty()) {
163 for (OvsdbInventoryListener listener : ovsdbInventoryListeners) {
164 listener.rowUpdated(n, tableName, uuid.toString(), update.getOld(uuid), update.getNew(uuid));
168 } else if (update.getOld(uuid) != null){
169 if (tCache != null) {
170 if (!ovsdbInventoryListeners.isEmpty()) {
171 for (OvsdbInventoryListener listener : ovsdbInventoryListeners) {
172 listener.rowRemoved(n, tableName, uuid.toString(), update.getOld(uuid), update.getNew(uuid));
176 db.removeRow(databaseName, tableName, uuid.toString());
182 private void handleOpenVSwitchSpecialCase(final Node node, final String databaseName, final String tableName, final UUID uuid) {
183 if (OvsVswitchdSchemaConstants.shouldConfigureController(databaseName, tableName)) {
184 Runnable updateControllerRunnable = new Runnable() {
188 if (ovsdbConfigurationService != null) {
189 ovsdbConfigurationService.setOFController(node, uuid.toString());
191 } catch (InterruptedException | ExecutionException e) {
196 executor.execute(updateControllerRunnable);
201 public void printCache(Node n) {
202 if ((dbCache != null) && (!dbCache.isEmpty())) {
203 NodeDatabase db = dbCache.get(n);
205 db.printTableCache();
211 public void notifyNodeAdded(Node node, InetAddress address, int port) {
212 if (!ovsdbInventoryListeners.isEmpty()) {
213 for (OvsdbInventoryListener listener : ovsdbInventoryListeners) {
214 listener.nodeAdded(node, address, port);
220 public void removeNode(Node node) {
221 if (!ovsdbInventoryListeners.isEmpty()) {
222 for (OvsdbInventoryListener listener : ovsdbInventoryListeners) {
223 listener.nodeRemoved(node);
227 dbCache.remove(node);
230 private void listenerAdded(OvsdbInventoryListener listener) {
231 this.ovsdbInventoryListeners.add(listener);
234 private void listenerRemoved(OvsdbInventoryListener listener) {
235 this.ovsdbInventoryListeners.remove(listener);