Deprecated OvsdbClientKey and replaced with ConnectionInfo
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / OvsdbConnectionInstance.java
index 9ea46ce03f8c10933ef3e56e150be08ff11c7802..22abd55e798eed6ae769596a51e77b224d31869b 100644 (file)
@@ -7,7 +7,9 @@
  */
 package org.opendaylight.ovsdb.southbound;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
 
@@ -30,8 +32,12 @@ import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.lib.schema.TableSchema;
 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;
 import org.slf4j.LoggerFactory;
 
@@ -41,42 +47,50 @@ import com.google.common.util.concurrent.ListenableFuture;
 public class OvsdbConnectionInstance implements OvsdbClient {
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbConnectionInstance.class);
     private OvsdbClient client;
-    private OvsdbClientKey key;
+    private ConnectionInfo connectionInfo;
     private TransactionInvoker txInvoker;
+    private Map<DatabaseSchema,TransactInvoker> transactInvokers = new HashMap<DatabaseSchema,TransactInvoker>();
     private MonitorCallBack callback;
 
-    OvsdbConnectionInstance(OvsdbClientKey key,OvsdbClient client,TransactionInvoker txInvoker) {
-        this.key = key;
+    OvsdbConnectionInstance(ConnectionInfo key,OvsdbClient client,TransactionInvoker txInvoker) {
+        this.connectionInfo = key;
         this.client = client;
         this.txInvoker = txInvoker;
         txInvoker.invoke(new OvsdbNodeCreateCommand(key, null,null));
         registerCallBack();
     }
 
+    public void transact(TransactCommand command) {
+        for (TransactInvoker transactInvoker: transactInvokers.values()) {
+            transactInvoker.invoke(command);
+        }
+    }
+
     private void registerCallBack() {
-        this.callback = new OvsdbMonitorCallback(key,txInvoker);
+        this.callback = new OvsdbMonitorCallback(connectionInfo,txInvoker);
         try {
             List<String> databases = getDatabases().get();
-            if(databases != null) {
+            if (databases != null) {
                 for (String database : databases) {
                     DatabaseSchema dbSchema = getSchema(database).get();
-                    if(dbSchema != null) {
+                    if (dbSchema != null) {
+                        transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
                         monitorAllTables(database, dbSchema);
                     } else {
-                        LOG.warn("No schema reported for database {} for key {}",database,key);
+                        LOG.warn("No schema reported for database {} for key {}",database,connectionInfo);
                     }
                 }
             } else {
-                LOG.warn("No databases reported from {}",key);
+                LOG.warn("No databases reported from {}",connectionInfo);
             }
         } catch (InterruptedException | ExecutionException e) {
-            LOG.warn("Exception attempting to initialize {}: {}",key,e);
+            LOG.warn("Exception attempting to initialize {}: {}",connectionInfo,e);
         }
     }
 
     private void monitorAllTables(String database, DatabaseSchema dbSchema) {
         Set<String> tables = dbSchema.getTables();
-        if(tables != null) {
+        if (tables != null) {
             List<MonitorRequest<GenericTableSchema>> monitorRequests = Lists.newArrayList();
             for (String tableName : tables) {
                 GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
@@ -89,7 +103,7 @@ public class OvsdbConnectionInstance implements OvsdbClient {
             }
             this.callback.update(monitor(dbSchema, monitorRequests, callback),dbSchema);
         } else {
-            LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,key);
+            LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
         }
     }
 
@@ -141,10 +155,6 @@ public class OvsdbConnectionInstance implements OvsdbClient {
         client.stopEchoService();
     }
 
-    public OvsdbConnectionInfo getConnectionInfo() {
-        return client.getConnectionInfo();
-    }
-
     public boolean isActive() {
         return client.isActive();
     }
@@ -171,11 +181,15 @@ public class OvsdbConnectionInstance implements OvsdbClient {
         return client.getTypedRowWrapper(klazz, row);
     }
 
-    public OvsdbClientKey getKey() {
-        return key;
+    public OvsdbConnectionInfo getConnectionInfo() {
+        return client.getConnectionInfo();
+    }
+
+    public ConnectionInfo getMDConnectionInfo() {
+        return connectionInfo;
     }
 
-    public void setKey(OvsdbClientKey key) {
-        this.key = key;
+    public void setMDConnectionInfo(ConnectionInfo key) {
+        this.connectionInfo = key;
     }
 }