First Plugin integration with the Library starting with ConnectionService and Invento...
[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     public void init() {
41         nodeProps = new ConcurrentHashMap<Node, Map<String, Property>>();
42         nodeConnectorProps = new ConcurrentHashMap<NodeConnector, Map<String, Property>>();
43         Node.NodeIDType.registerIDType("OVS", String.class);
44         NodeConnector.NodeConnectorIDType.registerIDType("OVS", String.class, "OVS");
45     }
46
47     /**
48      * Function called by the dependency manager when at least one dependency
49      * become unsatisfied or when the component is shutting down because for
50      * example bundle is being stopped.
51      *
52      */
53     public void destroy() {
54     }
55
56     /**
57      * Function called by dependency manager after "init ()" is called and after
58      * the services provided by the class are registered in the service registry
59      *
60      */
61     public void start() {
62     }
63
64     /**
65      * Function called by the dependency manager before the services exported by
66      * the component are unregistered, this will be followed by a "destroy ()"
67      * calls
68      *
69      */
70     public void stop() {
71     }
72
73     /**
74      * Retrieve nodes from openflow
75      */
76     @Override
77     public ConcurrentMap<Node, Map<String, Property>> getNodeProps() {
78         return nodeProps;
79     }
80
81     /**
82      * Retrieve nodeConnectors from openflow
83      */
84     @Override
85     public ConcurrentMap<NodeConnector, Map<String, Property>> getNodeConnectorProps(
86             Boolean refresh) {
87         return nodeConnectorProps;
88     }
89
90
91     @Override
92     public Map<String, Map<String, Table<?>>> getCache(Node n) {
93         NodeDB<Table<?>> db = dbCache.get(n);
94         if (db == null) return null;
95         return db.getTableCache();
96     }
97
98
99     @Override
100     public Map<String, Table<?>> getTableCache(Node n, String tableName) {
101         NodeDB<Table<?>> db = dbCache.get(n);
102         if (db == null) return null;
103         return db.getTableCache(tableName);
104     }
105
106
107     @Override
108     public Table<?> getRow(Node n, String tableName, String uuid) {
109         NodeDB<Table<?>> db = dbCache.get(n);
110         if (db == null) return null;
111         return db.getRow(tableName, uuid);
112     }
113
114
115     @Override
116     public void updateRow(Node n, String tableName, String uuid, Table<?> row) {
117         NodeDB<Table<?>> db = dbCache.get(n);
118         if (db == null) {
119             db = new NodeDB<Table<?>>();
120             dbCache.put(n, db);
121         }
122         db.updateRow(tableName, uuid, row);
123     }
124
125     @Override
126     public void removeRow(Node n, String tableName, String uuid) {
127         NodeDB<Table<?>> db = dbCache.get(n);
128         if (db != null) db.removeRow(tableName, uuid);
129     }
130
131     @Override
132     public void processTableUpdates(Node n, TableUpdates tableUpdates) {
133         NodeDB<Table<?>> db = dbCache.get(n);
134         if (db == null) {
135             db = new NodeDB<Table<?>>();
136             dbCache.put(n, db);
137         }
138
139         Set<Table.Name> available = tableUpdates.availableUpdates();
140         for (Table.Name name : available) {
141             System.out.println(name.getName() +":"+ tableUpdates.getUpdate(name).toString());
142             TableUpdate tableUpdate = tableUpdates.getUpdate(name);
143             Collection<TableUpdate.Row<?>> rows = tableUpdate.getRows();
144             for (Row<?> row : rows) {
145                 String uuid = row.getId();
146                 Table<?> newRow = (Table<?>)row.getNew();
147                 Table<?> oldRow = (Table<?>)row.getOld();
148                 if (newRow != null) {
149                     db.updateRow(name.getName(), uuid, newRow);
150                 } else if (oldRow != null) {
151                     db.removeRow(name.getName(), uuid);
152                 }
153             }
154         }
155     }
156
157     @Override
158     public void printCache(Node n) {
159         NodeDB<Table<?>> db = dbCache.get(n);
160         if (db != null) db.printTableCache();
161     }
162 }