Plugin migration to use the new Schema independent Library.
[ovsdb.git] / plugin / src / main / java / org / opendaylight / ovsdb / plugin / NodeDB.java
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
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
7  *
8  * Authors : Madhu Venugopal, Brent Salisbury
9  */
10 package org.opendaylight.ovsdb.plugin;
11
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentMap;
14
15 import org.apache.commons.collections.MapUtils;
16 import org.opendaylight.ovsdb.lib.notation.Row;
17
18 import com.google.common.collect.Maps;
19
20 public class NodeDB {
21     ConcurrentMap<String, ConcurrentMap<String, TableDB>> dbCache = Maps.newConcurrentMap();
22
23     public ConcurrentMap<String, ConcurrentMap<String,Row>> getDatabase(String dbName) {
24         ConcurrentMap<String, TableDB> tdbMap = dbCache.get(dbName);
25         if (tdbMap == null) return null;
26         ConcurrentMap<String, ConcurrentMap<String,Row>> retMap = Maps.newConcurrentMap();
27         for (String tableName : tdbMap.keySet()) {
28             TableDB tdb = tdbMap.get(tableName);
29             retMap.put(tableName, tdb.getTableCache(tableName));
30         }
31         return retMap;
32     }
33
34     public ConcurrentMap<String, Row> getTableCache(String dbName, String tableName) {
35         ConcurrentMap<String, ConcurrentMap<String,Row>> tdbMap = getDatabase(dbName);
36         if (tdbMap == null) return null;
37         return tdbMap.get(tableName);
38     }
39
40     private void setDBCache(String dbName,  ConcurrentMap<String, TableDB> table) {
41         dbCache.put(dbName, table);
42     }
43
44     public Row getRow (String dbName, String tableName, String uuid) {
45         ConcurrentMap<String, ConcurrentMap<String, Row>> db = getDatabase(dbName);
46         if (db == null) return null;
47         ConcurrentMap<String, Row> tdb = db.get(tableName);
48         if (tdb == null) return null;
49         return tdb.get(uuid);
50     }
51
52     public void updateRow(String dbName, String tableName, String uuid, Row row) {
53         ConcurrentMap<String, TableDB> db = dbCache.get(dbName);
54         if (db == null) {
55             db = Maps.newConcurrentMap();
56             setDBCache(dbName, db);
57         }
58         TableDB tdb = db.get(tableName);
59         if (tdb == null) {
60             tdb = new TableDB();
61             db.put(tableName, tdb);
62         }
63         tdb.updateRow(tableName, uuid, row);
64     }
65
66     public void removeRow(String dbName, String tableName, String uuid) {
67         ConcurrentMap<String, TableDB> db = dbCache.get(dbName);
68         if (db == null) return;
69         TableDB tdb = db.get(tableName);
70         if (tdb == null) return;
71         tdb.removeRow(tableName, uuid);
72     }
73
74     public void printTableCache() {
75         MapUtils.debugPrint(System.out, null, dbCache);
76     }
77
78     public class TableDB {
79         ConcurrentMap<String, ConcurrentMap<String, Row>> cache = Maps.newConcurrentMap();
80
81         public ConcurrentMap<String, ConcurrentMap<String, Row>> getTableCache() {
82             return cache;
83         }
84
85         public ConcurrentMap<String, Row> getTableCache(String tableName) {
86             return cache.get(tableName);
87         }
88
89         private void setTableCache(String tableName,  ConcurrentMap<String, Row> tableCache) {
90             cache.put(tableName, tableCache);
91         }
92
93         public Row getRow (String tableName, String uuid) {
94             Map<String, Row> tableCache = getTableCache(tableName);
95             if (tableCache != null) {
96                 return tableCache.get(uuid);
97             }
98             return null;
99         }
100
101         public void updateRow(String tableName, String uuid, Row row) {
102             ConcurrentMap<String, Row> tableCache = getTableCache(tableName);
103             if (tableCache == null) {
104                 tableCache = Maps.newConcurrentMap();
105                 setTableCache(tableName, tableCache);
106             }
107             tableCache.put(uuid, row);
108         }
109
110         public void removeRow(String tableName, String uuid) {
111             Map<String, Row> tableCache = getTableCache(tableName);
112             if (tableCache != null) {
113                 tableCache.remove(uuid);
114             }
115         }
116
117         public void printTableCache() {
118             MapUtils.debugPrint(System.out, null, cache);
119         }
120     }
121 }