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);
30 return tdb.getTableCache();
33 public ConcurrentMap<String, Row> getTableCache(String dbName, String tableName) {
34 ConcurrentMap<String, ConcurrentMap<String,Row>> tdbMap = getDatabase(dbName);
38 return tdbMap.get(tableName);
41 private void setDBCache(String dbName, TableDB table) {
42 dbCache.put(dbName, table);
45 public Row getRow (String dbName, String tableName, String uuid) {
46 ConcurrentMap<String, Row> tdb = this.getTableCache(dbName, tableName);
53 public void updateRow(String dbName, String tableName, String uuid, Row row) {
54 TableDB db = dbCache.get(dbName);
57 setDBCache(dbName, db);
59 db.updateRow(tableName, uuid, row);
62 public void removeRow(String dbName, String tableName, String uuid) {
63 TableDB db = dbCache.get(dbName);
67 db.removeRow(tableName, uuid);
70 public void printTableCache() {
71 for (String dbName : dbCache.keySet()) {
72 System.out.println("Database "+dbName);
73 ConcurrentMap<String, ConcurrentMap<String,Row>> tableDB = this.getDatabase(dbName);
74 if (tableDB == null) {
77 for (String tableName : tableDB.keySet()) {
78 ConcurrentMap<String, Row> tableRows = this.getTableCache(dbName, tableName);
79 System.out.println("\tTable "+tableName);
80 for (String uuid : tableRows.keySet()) {
81 Row row = tableRows.get(uuid);
82 Collection<Column> columns = row.getColumns();
83 System.out.print("\t\t"+uuid+ "==");
84 for (Column column : columns) {
85 if (column.getData() != null) {
86 System.out.print(column.getSchema().getName()+" : "+ column.getData()+" ");
89 System.out.println("");
91 System.out.println("-----------------------------------------------------------");
96 public class TableDB {
97 ConcurrentMap<String, ConcurrentMap<String, Row>> cache = Maps.newConcurrentMap();
99 public ConcurrentMap<String, ConcurrentMap<String, Row>> getTableCache() {
103 public ConcurrentMap<String, Row> getTableCache(String tableName) {
104 return cache.get(tableName);
107 private void setTableCache(String tableName, ConcurrentMap<String, Row> tableCache) {
108 cache.put(tableName, tableCache);
111 public Row getRow (String tableName, String uuid) {
112 Map<String, Row> tableCache = getTableCache(tableName);
113 if (tableCache != null) {
114 return tableCache.get(uuid);
119 public void updateRow(String tableName, String uuid, Row row) {
120 ConcurrentMap<String, Row> tableCache = getTableCache(tableName);
121 if (tableCache == null) {
122 tableCache = Maps.newConcurrentMap();
123 setTableCache(tableName, tableCache);
125 tableCache.put(uuid, row);
128 public void removeRow(String tableName, String uuid) {
129 Map<String, Row> tableCache = getTableCache(tableName);
130 if (tableCache != null) {
131 tableCache.remove(uuid);
135 public void printTableCache() {
136 MapUtils.debugPrint(System.out, null, cache);