Added controller is-connected code
[netvirt.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) return null;
28         return tdb.getTableCache();
29     }
30
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);
35     }
36
37     private void setDBCache(String dbName,  TableDB table) {
38         dbCache.put(dbName, table);
39     }
40
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;
44         return tdb.get(uuid);
45     }
46
47     public void updateRow(String dbName, String tableName, String uuid, Row row) {
48         TableDB db = dbCache.get(dbName);
49         if (db == null) {
50             db = new TableDB();
51             setDBCache(dbName, db);
52         }
53         db.updateRow(tableName, uuid, row);
54     }
55
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);
60     }
61
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("\tTable "+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()+" ");
76                     }
77                     System.out.println("");
78                 }
79                 System.out.println("-----------------------------------------------------------");
80             }
81         }
82     }
83
84     public class TableDB {
85         ConcurrentMap<String, ConcurrentMap<String, Row>> cache = Maps.newConcurrentMap();
86
87         public ConcurrentMap<String, ConcurrentMap<String, Row>> getTableCache() {
88             return cache;
89         }
90
91         public ConcurrentMap<String, Row> getTableCache(String tableName) {
92             return cache.get(tableName);
93         }
94
95         private void setTableCache(String tableName,  ConcurrentMap<String, Row> tableCache) {
96             cache.put(tableName, tableCache);
97         }
98
99         public Row getRow (String tableName, String uuid) {
100             Map<String, Row> tableCache = getTableCache(tableName);
101             if (tableCache != null) {
102                 return tableCache.get(uuid);
103             }
104             return null;
105         }
106
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);
112             }
113             tableCache.put(uuid, row);
114         }
115
116         public void removeRow(String tableName, String uuid) {
117             Map<String, Row> tableCache = getTableCache(tableName);
118             if (tableCache != null) {
119                 tableCache.remove(uuid);
120             }
121         }
122
123         public void printTableCache() {
124             MapUtils.debugPrint(System.out, null, cache);
125         }
126     }
127 }