Fixing all but one sonar issues in NodeDatabase:Amended
[ovsdb.git] / plugin / src / main / java / org / opendaylight / ovsdb / plugin / internal / NodeDatabase.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.internal;
11
12 import java.util.Collection;
13 import java.util.Map;
14 import java.util.concurrent.ConcurrentMap;
15
16 import org.apache.commons.collections.MapUtils;
17 import org.opendaylight.ovsdb.lib.notation.Column;
18 import org.opendaylight.ovsdb.lib.notation.Row;
19
20 import com.google.common.collect.Maps;
21
22 public class NodeDatabase {
23     ConcurrentMap<String, TableDB> dbCache = Maps.newConcurrentMap();
24
25     public ConcurrentMap<String, ConcurrentMap<String, Row>> getDatabase(String dbName) {
26         TableDB tdb = dbCache.get(dbName);
27         if (tdb == null) {
28            return null;
29         }
30         return tdb.getTableCache();
31     }
32
33     public ConcurrentMap<String, Row> getTableCache(String dbName, String tableName) {
34         ConcurrentMap<String, ConcurrentMap<String,Row>> tdbMap = getDatabase(dbName);
35         if (tdbMap == null) {
36            return null;
37         }
38         return tdbMap.get(tableName);
39     }
40
41     private void setDBCache(String dbName,  TableDB table) {
42         dbCache.put(dbName, table);
43     }
44
45     public Row getRow (String dbName, String tableName, String uuid) {
46         ConcurrentMap<String, Row> tdb = this.getTableCache(dbName, tableName);
47         if (tdb == null) {
48            return null;
49         }
50         return tdb.get(uuid);
51     }
52
53     public void updateRow(String dbName, String tableName, String uuid, Row row) {
54         TableDB db = dbCache.get(dbName);
55         if (db == null) {
56             db = new TableDB();
57             setDBCache(dbName, db);
58         }
59         db.updateRow(tableName, uuid, row);
60     }
61
62     public void removeRow(String dbName, String tableName, String uuid) {
63         TableDB db = dbCache.get(dbName);
64         if (db == null) {
65            return;
66         }
67         db.removeRow(tableName, uuid);
68     }
69
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) {
75                continue;
76             }
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()+" ");
87                         }
88                     }
89                     System.out.println("");
90                 }
91                 System.out.println("-----------------------------------------------------------");
92             }
93         }
94     }
95
96     public class TableDB {
97         ConcurrentMap<String, ConcurrentMap<String, Row>> cache = Maps.newConcurrentMap();
98
99         public ConcurrentMap<String, ConcurrentMap<String, Row>> getTableCache() {
100             return cache;
101         }
102
103         public ConcurrentMap<String, Row> getTableCache(String tableName) {
104             return cache.get(tableName);
105         }
106
107         private void setTableCache(String tableName,  ConcurrentMap<String, Row> tableCache) {
108             cache.put(tableName, tableCache);
109         }
110
111         public Row getRow (String tableName, String uuid) {
112             Map<String, Row> tableCache = getTableCache(tableName);
113             if (tableCache != null) {
114                 return tableCache.get(uuid);
115             }
116             return null;
117         }
118
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);
124             }
125             tableCache.put(uuid, row);
126         }
127
128         public void removeRow(String tableName, String uuid) {
129             Map<String, Row> tableCache = getTableCache(tableName);
130             if (tableCache != null) {
131                 tableCache.remove(uuid);
132             }
133         }
134
135         public void printTableCache() {
136             MapUtils.debugPrint(System.out, null, cache);
137         }
138     }
139 }