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.internal;
12 import java.util.Collection;
14 import java.util.concurrent.ConcurrentMap;
16 import org.apache.commons.collections.MapUtils;
17 import org.opendaylight.ovsdb.lib.notation.Column;
18 import org.opendaylight.ovsdb.lib.notation.Row;
20 import com.google.common.collect.Maps;
22 public class NodeDatabase {
23 ConcurrentMap<String, TableDB> dbCache = Maps.newConcurrentMap();
25 public ConcurrentMap<String, ConcurrentMap<String, Row>> getDatabase(String dbName) {
26 TableDB tdb = dbCache.get(dbName);
27 if (tdb == null) return null;
28 return tdb.getTableCache();
31 public ConcurrentMap<String, Row> getTableCache(String dbName, String tableName) {
32 ConcurrentMap<String, ConcurrentMap<String,Row>> tdbMap = getDatabase(dbName);
33 if (tdbMap == null) return null;
34 return tdbMap.get(tableName);
37 private void setDBCache(String dbName, TableDB table) {
38 dbCache.put(dbName, table);
41 public Row getRow (String dbName, String tableName, String uuid) {
42 ConcurrentMap<String, Row> tdb = this.getTableCache(dbName, tableName);
43 if (tdb == null) return null;
47 public void updateRow(String dbName, String tableName, String uuid, Row row) {
48 TableDB db = dbCache.get(dbName);
51 setDBCache(dbName, db);
53 db.updateRow(tableName, uuid, row);
56 public void removeRow(String dbName, String tableName, String uuid) {
57 TableDB db = dbCache.get(dbName);
58 if (db == null) return;
59 db.removeRow(tableName, uuid);
62 public void printTableCache() {
63 for (String dbName : dbCache.keySet()) {
64 System.out.println("Database "+dbName);
65 ConcurrentMap<String, ConcurrentMap<String,Row>> tableDB = this.getDatabase(dbName);
66 if (tableDB == null) continue;
67 for (String tableName : tableDB.keySet()) {
68 ConcurrentMap<String, Row> tableRows = this.getTableCache(dbName, tableName);
69 System.out.println("\t Table "+tableName);
70 for (String uuid : tableRows.keySet()) {
71 Row row = tableRows.get(uuid);
72 Collection<Column> columns = row.getColumns();
73 System.out.print("\t\t"+uuid+ "==");
74 for (Column column : columns) {
75 if (column.getData() != null) System.out.print(column.getSchema().getName()+" : "+ column.getData()+" ");
77 System.out.println("");
79 System.out.println("-----------------------------------------------------------");
84 public class TableDB {
85 ConcurrentMap<String, ConcurrentMap<String, Row>> cache = Maps.newConcurrentMap();
87 public ConcurrentMap<String, ConcurrentMap<String, Row>> getTableCache() {
91 public ConcurrentMap<String, Row> getTableCache(String tableName) {
92 return cache.get(tableName);
95 private void setTableCache(String tableName, ConcurrentMap<String, Row> tableCache) {
96 cache.put(tableName, tableCache);
99 public Row getRow (String tableName, String uuid) {
100 Map<String, Row> tableCache = getTableCache(tableName);
101 if (tableCache != null) {
102 return tableCache.get(uuid);
107 public void updateRow(String tableName, String uuid, Row row) {
108 ConcurrentMap<String, Row> tableCache = getTableCache(tableName);
109 if (tableCache == null) {
110 tableCache = Maps.newConcurrentMap();
111 setTableCache(tableName, tableCache);
113 tableCache.put(uuid, row);
116 public void removeRow(String tableName, String uuid) {
117 Map<String, Row> tableCache = getTableCache(tableName);
118 if (tableCache != null) {
119 tableCache.remove(uuid);
123 public void printTableCache() {
124 MapUtils.debugPrint(System.out, null, cache);