InventoryService caching and First-level Transaction Support
[netvirt.git] / ovsdb / src / main / java / org / opendaylight / ovsdb / plugin / InventoryService.java
1 package org.opendaylight.ovsdb.plugin;
2
3 import java.util.Collection;
4 import java.util.Map;
5 import java.util.Set;
6 import java.util.concurrent.ConcurrentHashMap;
7 import java.util.concurrent.ConcurrentMap;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.opendaylight.controller.sal.core.Node;
12 import org.opendaylight.controller.sal.core.NodeConnector;
13 import org.opendaylight.controller.sal.core.Property;
14 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService;
15 import org.opendaylight.ovsdb.lib.message.TableUpdate;
16 import org.opendaylight.ovsdb.lib.message.TableUpdate.Row;
17 import org.opendaylight.ovsdb.lib.message.TableUpdates;
18 import org.opendaylight.ovsdb.lib.table.internal.Table;
19
20 import com.google.common.collect.Maps;
21
22 /**
23  * Stub Implementation for IPluginInReadService used by SAL
24  *
25  *
26  */
27 public class InventoryService implements IPluginInInventoryService, InventoryServiceInternal {
28     private static final Logger logger = LoggerFactory
29             .getLogger(InventoryService.class);
30
31     private ConcurrentMap<Node, Map<String, Property>> nodeProps;
32     private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps;
33     private Map<Node, NodeDB<Table<?>>> dbCache = Maps.newHashMap();
34
35     /**
36      * Function called by the dependency manager when all the required
37      * dependencies are satisfied
38      *
39      */
40     void init() {
41         nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
42         nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
43         dbCache = new ConcurrentHashMap<Node, NodeDB<Table<?>>>();
44         Node.NodeIDType.registerIDType("OVS", String.class);
45         NodeConnector.NodeConnectorIDType.registerIDType("OVS", String.class, "OVS");
46     }
47
48     /**
49      * Function called by the dependency manager when at least one dependency
50      * become unsatisfied or when the component is shutting down because for
51      * example bundle is being stopped.
52      *
53      */
54     void destroy() {
55     }
56
57     /**
58      * Function called by dependency manager after "init ()" is called and after
59      * the services provided by the class are registered in the service registry
60      *
61      */
62     void start() {
63     }
64
65     /**
66      * Function called by the dependency manager before the services exported by
67      * the component are unregistered, this will be followed by a "destroy ()"
68      * calls
69      *
70      */
71     void stop() {
72     }
73
74     /**
75      * Retrieve nodes from openflow
76      */
77     @Override
78     public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
79         return nodeProps;
80     }
81
82     /**
83      * Retrieve nodeConnectors from openflow
84      */
85     @Override
86     public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
87             Boolean refresh) {
88         return nodeConnectorProps;
89     }
90
91
92     @Override
93     public Map<String, Map<String, Table<?>>> getCache(Node n) {
94         NodeDB<Table<?>> db = dbCache.get(n);
95         if (db == null) return null;
96         return db.getTableCache();
97     }
98
99
100     @Override
101     public Map<String, Table<?>> getTableCache(Node n, String tableName) {
102         NodeDB<Table<?>> db = dbCache.get(n);
103         if (db == null) return null;
104         return db.getTableCache(tableName);
105     }
106
107
108     @Override
109     public Table<?> getRow(Node n, String tableName, String uuid) {
110         NodeDB<Table<?>> db = dbCache.get(n);
111         if (db == null) return null;
112         return db.getRow(tableName, uuid);
113     }
114
115
116     @Override
117     public void updateRow(Node n, String tableName, String uuid, Table<?> row) {
118         NodeDB<Table<?>> db = dbCache.get(n);
119         if (db == null) {
120             db = new NodeDB<Table<?>>();
121             dbCache.put(n, db);
122         }
123         db.updateRow(tableName, uuid, row);
124     }
125
126     @Override
127     public void removeRow(Node n, String tableName, String uuid) {
128         NodeDB<Table<?>> db = dbCache.get(n);
129         if (db != null) db.removeRow(tableName, uuid);
130     }
131
132     @Override
133     public void processTableUpdates(Node n, TableUpdates tableUpdates) {
134         NodeDB<Table<?>> db = dbCache.get(n);
135         if (db == null) {
136             db = new NodeDB<Table<?>>();
137             dbCache.put(n, db);
138         }
139
140         Set<Table.Name> available = tableUpdates.availableUpdates();
141         for (Table.Name name : available) {
142             System.out.println(name.getName() +":"+ tableUpdates.getUpdate(name).toString());
143             TableUpdate tableUpdate = tableUpdates.getUpdate(name);
144             Collection<TableUpdate.Row<?>> rows = tableUpdate.getRows();
145             for (Row<?> row : rows) {
146                 String uuid = row.getId();
147                 Table<?> newRow = (Table<?>)row.getNew();
148                 Table<?> oldRow = (Table<?>)row.getOld();
149                 if (newRow != null) {
150                     db.updateRow(name.getName(), uuid, newRow);
151                 } else if (oldRow != null) {
152                     db.removeRow(name.getName(), uuid);
153                 }
154             }
155         }
156     }
157
158     @Override
159     public void printCache(Node n) {
160         NodeDB<Table<?>> db = dbCache.get(n);
161         if (db != null) db.printTableCache();
162     }
163 }