912d0ee0d39d6ca67400c889a8237262e214f48b
[ovsdb.git] / ovsdb / 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 com.google.common.collect.Maps;
16
17 import org.apache.commons.collections.MapUtils;
18 import org.opendaylight.ovsdb.lib.database.DatabaseSchema;
19 import org.opendaylight.ovsdb.lib.table.internal.Table;
20
21 public class NodeDB {
22     private DatabaseSchema schema;
23     ConcurrentMap<String, ConcurrentMap<String, Table<?>>> cache = Maps.newConcurrentMap();
24
25     public DatabaseSchema getSchema() {
26         return schema;
27     }
28
29     public void setSchema(DatabaseSchema schema) {
30         this.schema = schema;
31     }
32
33     public ConcurrentMap<String, ConcurrentMap<String, Table<?>>> getTableCache() {
34         return cache;
35     }
36
37     public ConcurrentMap<String, Table<?>> getTableCache(String tableName) {
38         return cache.get(tableName);
39     }
40
41     private void setTableCache(String tableName,  ConcurrentMap<String, Table<?>> tableCache) {
42         cache.put(tableName, tableCache);
43     }
44
45     public Table<?> getRow (String tableName, String uuid) {
46         Map<String, Table<?>> tableCache = getTableCache(tableName);
47         if (tableCache != null) {
48             return tableCache.get(uuid);
49         }
50         return null;
51     }
52
53     public void updateRow(String tableName, String uuid, Table<?> row) {
54         ConcurrentMap<String, Table<?>> tableCache = getTableCache(tableName);
55         if (tableCache == null) {
56             tableCache = Maps.newConcurrentMap();
57             setTableCache(tableName, tableCache);
58         }
59         tableCache.put(uuid, row);
60     }
61
62     public void removeRow(String tableName, String uuid) {
63         Map<String, Table<?>> tableCache = getTableCache(tableName);
64         if (tableCache != null) {
65             tableCache.remove(uuid);
66         }
67     }
68
69     public void printTableCache() {
70         MapUtils.debugPrint(System.out, null, schema.getTables());
71         MapUtils.debugPrint(System.out, null, cache);
72     }
73 }