Slight simplification in OvsdbConnectionInstance
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / OvsdbConnectionInstance.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
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 package org.opendaylight.ovsdb.southbound;
9
10 import java.util.List;
11 import java.util.Set;
12 import java.util.concurrent.ExecutionException;
13
14 import org.opendaylight.ovsdb.lib.EchoServiceCallbackFilters;
15 import org.opendaylight.ovsdb.lib.LockAquisitionCallback;
16 import org.opendaylight.ovsdb.lib.LockStolenCallback;
17 import org.opendaylight.ovsdb.lib.MonitorCallBack;
18 import org.opendaylight.ovsdb.lib.MonitorHandle;
19 import org.opendaylight.ovsdb.lib.OvsdbClient;
20 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
21 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
22 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
23 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
24 import org.opendaylight.ovsdb.lib.message.TableUpdates;
25 import org.opendaylight.ovsdb.lib.notation.Row;
26 import org.opendaylight.ovsdb.lib.operations.Operation;
27 import org.opendaylight.ovsdb.lib.operations.OperationResult;
28 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
29 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
30 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
31 import org.opendaylight.ovsdb.lib.schema.TableSchema;
32 import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
33 import org.opendaylight.ovsdb.southbound.transactions.md.OvsdbNodeCreateCommand;
34 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import com.google.common.collect.Lists;
39 import com.google.common.util.concurrent.ListenableFuture;
40
41 public class OvsdbConnectionInstance implements OvsdbClient {
42     private static final Logger LOG = LoggerFactory.getLogger(OvsdbConnectionInstance.class);
43     private OvsdbClient client;
44     private OvsdbClientKey key;
45     private TransactionInvoker txInvoker;
46     private MonitorCallBack callback;
47
48     OvsdbConnectionInstance(OvsdbClientKey key,OvsdbClient client,TransactionInvoker txInvoker) {
49         this.key = key;
50         this.client = client;
51         this.txInvoker = txInvoker;
52         txInvoker.invoke(new OvsdbNodeCreateCommand(key, null,null));
53         registerCallBack();
54     }
55
56     private void registerCallBack() {
57         this.callback = new OvsdbMonitorCallback(key,txInvoker);
58         try {
59             List<String> databases = getDatabases().get();
60             if(databases != null) {
61                 for (String database : databases) {
62                     DatabaseSchema dbSchema = getSchema(database).get();
63                     if(dbSchema != null) {
64                         monitorAllTables(database, dbSchema);
65                     } else {
66                         LOG.warn("No schema reported for database {} for key {}",database,key);
67                     }
68                 }
69             } else {
70                 LOG.warn("No databases reported from {}",key);
71             }
72         } catch (InterruptedException | ExecutionException e) {
73             LOG.warn("Exception attempting to initialize {}: {}",key,e);
74         }
75     }
76
77     private void monitorAllTables(String database, DatabaseSchema dbSchema) {
78         Set<String> tables = dbSchema.getTables();
79         if(tables != null) {
80             List<MonitorRequest<GenericTableSchema>> monitorRequests = Lists.newArrayList();
81             for (String tableName : tables) {
82                 GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
83                 Set<String> columns = tableSchema.getColumns();
84                 MonitorRequestBuilder<GenericTableSchema> monitorBuilder = MonitorRequestBuilder.builder(tableSchema);
85                 for (String column : columns) {
86                     monitorBuilder.addColumn(column);
87                 }
88                 monitorRequests.add(monitorBuilder.with(new MonitorSelect(true, true, true, true)).build());
89             }
90             this.callback.update(monitor(dbSchema, monitorRequests, callback),dbSchema);
91         } else {
92             LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,key);
93         }
94     }
95
96     public ListenableFuture<List<String>> getDatabases() {
97         return client.getDatabases();
98     }
99
100     public ListenableFuture<DatabaseSchema> getSchema(String database) {
101         return client.getSchema(database);
102     }
103
104     public TransactionBuilder transactBuilder(DatabaseSchema dbSchema) {
105         return client.transactBuilder(dbSchema);
106     }
107
108     public ListenableFuture<List<OperationResult>> transact(
109             DatabaseSchema dbSchema, List<Operation> operations) {
110         return client.transact(dbSchema, operations);
111     }
112
113     public <E extends TableSchema<E>> TableUpdates monitor(
114             DatabaseSchema schema, List<MonitorRequest<E>> monitorRequests,
115             MonitorCallBack callback) {
116         return client.monitor(schema, monitorRequests, callback);
117     }
118
119     public void cancelMonitor(MonitorHandle handler) {
120         client.cancelMonitor(handler);
121     }
122
123     public void lock(String lockId, LockAquisitionCallback lockedCallBack,
124             LockStolenCallback stolenCallback) {
125         client.lock(lockId, lockedCallBack, stolenCallback);
126     }
127
128     public ListenableFuture<Boolean> steal(String lockId) {
129         return client.steal(lockId);
130     }
131
132     public ListenableFuture<Boolean> unLock(String lockId) {
133         return client.unLock(lockId);
134     }
135
136     public void startEchoService(EchoServiceCallbackFilters callbackFilters) {
137         client.startEchoService(callbackFilters);
138     }
139
140     public void stopEchoService() {
141         client.stopEchoService();
142     }
143
144     public OvsdbConnectionInfo getConnectionInfo() {
145         return client.getConnectionInfo();
146     }
147
148     public boolean isActive() {
149         return client.isActive();
150     }
151
152     public void disconnect() {
153         client.disconnect();
154     }
155
156     public DatabaseSchema getDatabaseSchema(String dbName) {
157         return client.getDatabaseSchema(dbName);
158     }
159
160     public <T extends TypedBaseTable<?>> T createTypedRowWrapper(Class<T> klazz) {
161         return client.createTypedRowWrapper(klazz);
162     }
163
164     public <T extends TypedBaseTable<?>> T createTypedRowWrapper(
165             DatabaseSchema dbSchema, Class<T> klazz) {
166         return client.createTypedRowWrapper(dbSchema, klazz);
167     }
168
169     public <T extends TypedBaseTable<?>> T getTypedRowWrapper(Class<T> klazz,
170             Row<GenericTableSchema> row) {
171         return client.getTypedRowWrapper(klazz, row);
172     }
173
174     public OvsdbClientKey getKey() {
175         return key;
176     }
177
178     public void setKey(OvsdbClientKey key) {
179         this.key = key;
180     }
181 }