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