Fix race condition in initial connection.
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / OvsdbConnectionInstance.java
index 4c5548879056daf81d4acec9eb580ece8e153c25..757ebd02479ff643a628e744b7bd9735a6ee7e77 100644 (file)
@@ -35,6 +35,7 @@ import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactCommand;
 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactInvoker;
 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactInvokerImpl;
+import org.opendaylight.ovsdb.southbound.transactions.md.OvsdbNodeCreateCommand;
 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
 import org.slf4j.Logger;
@@ -50,12 +51,13 @@ public class OvsdbConnectionInstance implements OvsdbClient {
     private TransactionInvoker txInvoker;
     private Map<DatabaseSchema,TransactInvoker> transactInvokers = new HashMap<DatabaseSchema,TransactInvoker>();
     private MonitorCallBack callback;
+    private ConnectionInfo key;
 
     OvsdbConnectionInstance(ConnectionInfo key,OvsdbClient client,TransactionInvoker txInvoker) {
         this.connectionInfo = key;
         this.client = client;
         this.txInvoker = txInvoker;
-        registerCallBack();
+        this.key = key;
     }
 
     public void transact(TransactCommand command) {
@@ -64,25 +66,28 @@ public class OvsdbConnectionInstance implements OvsdbClient {
         }
     }
 
-    private void registerCallBack() {
-        this.callback = new OvsdbMonitorCallback(connectionInfo,txInvoker);
-        try {
-            List<String> databases = getDatabases().get();
-            if (databases != null) {
-                for (String database : databases) {
-                    DatabaseSchema dbSchema = getSchema(database).get();
-                    if (dbSchema != null) {
-                        transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
-                        monitorAllTables(database, dbSchema);
-                    } else {
-                        LOG.warn("No schema reported for database {} for key {}",database,connectionInfo);
+    public void init() {
+        if ( this.callback == null) {
+            txInvoker.invoke(new OvsdbNodeCreateCommand(key, null,null));
+            this.callback = new OvsdbMonitorCallback(connectionInfo,txInvoker);
+            try {
+                List<String> databases = getDatabases().get();
+                if (databases != null) {
+                    for (String database : databases) {
+                        DatabaseSchema dbSchema = getSchema(database).get();
+                        if (dbSchema != null) {
+                            transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
+                            monitorAllTables(database, dbSchema);
+                        } else {
+                            LOG.warn("No schema reported for database {} for key {}",database,connectionInfo);
+                        }
                     }
+                } else {
+                    LOG.warn("No databases reported from {}",connectionInfo);
                 }
-            } else {
-                LOG.warn("No databases reported from {}",connectionInfo);
+            } catch (InterruptedException | ExecutionException e) {
+                LOG.warn("Exception attempting to initialize {}: {}",connectionInfo,e);
             }
-        } catch (InterruptedException | ExecutionException e) {
-            LOG.warn("Exception attempting to initialize {}: {}",connectionInfo,e);
         }
     }