/* * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.ovsdb.plugin.internal; import java.util.Collection; import java.util.Map; import java.util.concurrent.ConcurrentMap; import org.apache.commons.collections.MapUtils; import org.opendaylight.ovsdb.lib.notation.Column; import org.opendaylight.ovsdb.lib.notation.Row; import com.google.common.collect.Maps; public class NodeDatabase { ConcurrentMap dbCache = Maps.newConcurrentMap(); public ConcurrentMap> getDatabase(String dbName) { TableDB tdb = dbCache.get(dbName); if (tdb == null) { return null; } return tdb.getTableCache(); } public ConcurrentMap getTableCache(String dbName, String tableName) { ConcurrentMap> tdbMap = getDatabase(dbName); if (tdbMap == null) { return null; } return tdbMap.get(tableName); } private void setDBCache(String dbName, TableDB table) { dbCache.put(dbName, table); } public Row getRow (String dbName, String tableName, String uuid) { ConcurrentMap tdb = this.getTableCache(dbName, tableName); if (tdb == null) { return null; } return tdb.get(uuid); } public void updateRow(String dbName, String tableName, String uuid, Row row) { TableDB db = dbCache.get(dbName); if (db == null) { db = new TableDB(); setDBCache(dbName, db); } db.updateRow(tableName, uuid, row); } public void removeRow(String dbName, String tableName, String uuid) { TableDB db = dbCache.get(dbName); if (db == null) { return; } db.removeRow(tableName, uuid); } public void printTableCache() { for (String dbName : dbCache.keySet()) { System.out.println("Database "+dbName); ConcurrentMap> tableDB = this.getDatabase(dbName); if (tableDB == null) { continue; } for (String tableName : tableDB.keySet()) { ConcurrentMap tableRows = this.getTableCache(dbName, tableName); System.out.println("\tTable "+tableName); for (String uuid : tableRows.keySet()) { Row row = tableRows.get(uuid); Collection columns = row.getColumns(); System.out.print("\t\t"+uuid+ "=="); for (Column column : columns) { if (column.getData() != null) { System.out.print(column.getSchema().getName()+" : "+ column.getData()+" "); } } System.out.println(""); } System.out.println("-----------------------------------------------------------"); } } } public class TableDB { ConcurrentMap> cache = Maps.newConcurrentMap(); public ConcurrentMap> getTableCache() { return cache; } public ConcurrentMap getTableCache(String tableName) { return cache.get(tableName); } private void setTableCache(String tableName, ConcurrentMap tableCache) { cache.put(tableName, tableCache); } public Row getRow (String tableName, String uuid) { Map tableCache = getTableCache(tableName); if (tableCache != null) { return tableCache.get(uuid); } return null; } public void updateRow(String tableName, String uuid, Row row) { ConcurrentMap tableCache = getTableCache(tableName); if (tableCache == null) { tableCache = Maps.newConcurrentMap(); setTableCache(tableName, tableCache); } tableCache.put(uuid, row); } public void removeRow(String tableName, String uuid) { Map tableCache = getTableCache(tableName); if (tableCache != null) { tableCache.remove(uuid); } } public void printTableCache() { MapUtils.debugPrint(System.out, null, cache); } } }