Introducing AbstractTransactionCommand to make it easier to do commands 78/15678/1
authorEd Warnicke <eaw@cisco.com>
Tue, 24 Feb 2015 18:25:59 +0000 (11:25 -0700)
committerEd Warnicke <eaw@cisco.com>
Tue, 24 Feb 2015 18:25:59 +0000 (11:25 -0700)
I found myself writing a lot of repetative code for TransactionCommands,
and so pulled it up into an AbstractTransactionCommand

Change-Id: Iddd6995146e9ddc4002b7e8c6791b1332198c361
Signed-off-by: Ed Warnicke <eaw@cisco.com>
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/AbstractTransactionCommand.java [new file with mode: 0644]
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeRemovedCommand.java
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeUpdateCommand.java
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbNodeCreateCommand.java
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbNodeRemoveCommand.java

diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/AbstractTransactionCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/AbstractTransactionCommand.java
new file mode 100644 (file)
index 0000000..7ab28cd
--- /dev/null
@@ -0,0 +1,35 @@
+package org.opendaylight.ovsdb.southbound.transactions.md;
+
+import org.opendaylight.ovsdb.lib.message.TableUpdates;
+import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
+import org.opendaylight.ovsdb.southbound.OvsdbClientKey;
+
+public abstract class AbstractTransactionCommand implements TransactionCommand {
+
+    private TableUpdates updates;
+    private DatabaseSchema dbSchema;
+    private OvsdbClientKey key;
+
+    public TableUpdates getUpdates() {
+        return updates;
+    }
+
+    public DatabaseSchema getDbSchema() {
+        return dbSchema;
+    }
+
+    public OvsdbClientKey getKey() {
+        return key;
+    }
+
+    protected AbstractTransactionCommand() {
+        // NO OP
+    }
+
+    public AbstractTransactionCommand(OvsdbClientKey key,TableUpdates updates, DatabaseSchema dbSchema) {
+        this.updates = updates;
+        this.dbSchema = dbSchema;
+        this.key = key;
+    }
+
+}
\ No newline at end of file
index cdf383dff95451831b38f9502ee1b7aa215d638a..ebedd32bc0b72f24e1bd9b3237cd59a9cc889fbd 100644 (file)
@@ -19,27 +19,22 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class OvsdbBridgeRemovedCommand implements TransactionCommand {
+public class OvsdbBridgeRemovedCommand extends AbstractTransactionCommand {
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
 
-    private TableUpdates updates;
-    private DatabaseSchema dbSchema;
-
-    private OvsdbClientKey key;
-
-    public OvsdbBridgeRemovedCommand(OvsdbClientKey key,TableUpdates updates, DatabaseSchema dbSchema) {
-        this.updates = updates;
-        this.dbSchema = dbSchema;
-        this.key = key;
+    public OvsdbBridgeRemovedCommand(OvsdbClientKey key, TableUpdates updates,
+            DatabaseSchema dbSchema) {
+        super(key,updates,dbSchema);
     }
+
     @Override
     public void execute(ReadWriteTransaction transaction) {
-        List<TypedBaseTable<?>> removedRows = TransactionUtils.extractRowsRemoved(Bridge.class, updates, dbSchema);
+        List<TypedBaseTable<?>> removedRows = TransactionUtils.extractRowsRemoved(Bridge.class, getUpdates(), getDbSchema());
         for(TypedBaseTable<?> removedRow : removedRows) {
             if(removedRow instanceof Bridge) {
                 Bridge bridge = (Bridge)removedRow;
-                InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(key, bridge.getUuid());
-                InstanceIdentifier<ManagedNodeEntry> mnIid = SouthboundMapper.createInstanceIndentifier(key)
+                InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getKey(), bridge.getUuid());
+                InstanceIdentifier<ManagedNodeEntry> mnIid = SouthboundMapper.createInstanceIndentifier(getKey())
                         .augmentation(OvsdbNodeAugmentation.class)
                         .child(ManagedNodeEntry.class, new ManagedNodeEntryKey(new OvsdbBridgeRef(bridgeIid)));
                 // TODO handle removal of reference to managed node from model
index 5670f8de5f718a984c001aabc250fc3b20602236..24ac432230233070d7f164fae69c61128025abac 100644 (file)
@@ -30,26 +30,21 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
 
-public class OvsdbBridgeUpdateCommand implements TransactionCommand {
+public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
 
-    private TableUpdates updates;
-    private DatabaseSchema dbSchema;
-
-    private OvsdbClientKey key;
-
-    public OvsdbBridgeUpdateCommand(OvsdbClientKey key,TableUpdates updates, DatabaseSchema dbSchema) {
-        this.updates = updates;
-        this.dbSchema = dbSchema;
-        this.key = key;
+    public OvsdbBridgeUpdateCommand(OvsdbClientKey key, TableUpdates updates,
+            DatabaseSchema dbSchema) {
+        super(key,updates,dbSchema);
     }
+
     @Override
     public void execute(ReadWriteTransaction transaction) {
-        List<TypedBaseTable<?>> updatedRows = TransactionUtils.extractRowsUpdated(Bridge.class, updates, dbSchema);
+        List<TypedBaseTable<?>> updatedRows = TransactionUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
         for(TypedBaseTable<?> updatedRow : updatedRows) {
             if(updatedRow instanceof Bridge) {
                 Bridge bridge = (Bridge)updatedRow;
-                final InstanceIdentifier<Node> nodePath = key.toInstanceIndentifier();
+                final InstanceIdentifier<Node> nodePath = getKey().toInstanceIndentifier();
                 Optional<Node> node = Optional.absent();
                 try{
                     node = transaction.read(LogicalDatastoreType.OPERATIONAL, nodePath).checkedGet();
@@ -59,7 +54,7 @@ public class OvsdbBridgeUpdateCommand implements TransactionCommand {
                 if(node.isPresent()){
                     LOG.info("Node {} is present",node);
                     NodeBuilder managedNodeBuilder = new NodeBuilder();
-                    NodeId manageNodeId = SouthboundMapper.createManagedNodeId(key, bridge.getUuid());
+                    NodeId manageNodeId = SouthboundMapper.createManagedNodeId(getKey(), bridge.getUuid());
                     managedNodeBuilder.setNodeId(manageNodeId);
 
                     OvsdbManagedNodeAugmentationBuilder ovsdbManagedNodeBuilder = new OvsdbManagedNodeAugmentationBuilder();
@@ -75,7 +70,7 @@ public class OvsdbBridgeUpdateCommand implements TransactionCommand {
 
                     //Update node with managed node reference
                     NodeBuilder nodeBuilder = new NodeBuilder();
-                    nodeBuilder.setNodeId(SouthboundMapper.createNodeId(key.getIp(),key.getPort()));
+                    nodeBuilder.setNodeId(SouthboundMapper.createNodeId(getKey().getIp(),getKey().getPort()));
 
                     OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
                     List<ManagedNodeEntry> managedNodes = new ArrayList<ManagedNodeEntry>();
index 08c36a238f1b160a05394b8c2d21cbd90077bec0..5d8cc32180e6f9fc2c3c593c84572b6ab29a7f2b 100644 (file)
@@ -7,18 +7,16 @@ import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.southbound.OvsdbClientKey;
 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
 
-public class OvsdbNodeCreateCommand implements TransactionCommand {
-
-    private OvsdbClientKey key;
+public class OvsdbNodeCreateCommand extends AbstractTransactionCommand {
 
     public OvsdbNodeCreateCommand(OvsdbClientKey key,TableUpdates updates,DatabaseSchema dbSchema) {
-        this.key = key;
+        super(key,updates,dbSchema);
     }
 
     @Override
     public void execute(ReadWriteTransaction transaction) {
-        transaction.put(LogicalDatastoreType.OPERATIONAL, key.toInstanceIndentifier(),
-                SouthboundMapper.createNode(key));
+        transaction.put(LogicalDatastoreType.OPERATIONAL, getKey().toInstanceIndentifier(),
+                SouthboundMapper.createNode(getKey()));
     }
 
 }
index 884fd8f9c2e284ddf4ee0da7fd78a536fc55b52c..1d64f2cba1d814366d306cc56127c6c5deddadbe 100644 (file)
@@ -6,7 +6,6 @@ import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.ovsdb.lib.message.TableUpdates;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.southbound.OvsdbClientKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
@@ -16,18 +15,16 @@ import org.slf4j.LoggerFactory;
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.CheckedFuture;
 
-public class OvsdbNodeRemoveCommand implements TransactionCommand {
+public class OvsdbNodeRemoveCommand extends AbstractTransactionCommand {
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbNodeRemoveCommand.class);
 
-    private OvsdbClientKey key;
-
     public OvsdbNodeRemoveCommand(OvsdbClientKey key,TableUpdates updates,DatabaseSchema dbSchema) {
-        this.key = key;
+        super(key,updates,dbSchema);
     }
 
     @Override
     public void execute(ReadWriteTransaction transaction) {
-        CheckedFuture<Optional<Node>, ReadFailedException> ovsdbNodeFuture = transaction.read(LogicalDatastoreType.OPERATIONAL, key.toInstanceIndentifier());
+        CheckedFuture<Optional<Node>, ReadFailedException> ovsdbNodeFuture = transaction.read(LogicalDatastoreType.OPERATIONAL, getKey().toInstanceIndentifier());
         Optional<Node> ovsdbNodeOptional;
         try {
             ovsdbNodeOptional = ovsdbNodeFuture.get();
@@ -37,7 +34,7 @@ public class OvsdbNodeRemoveCommand implements TransactionCommand {
                 for(ManagedNodeEntry managedNode: ovsdbNodeAugmentation.getManagedNodeEntry()) {
                     transaction.delete(LogicalDatastoreType.OPERATIONAL, managedNode.getBridgeRef().getValue());
                 }
-                transaction.delete(LogicalDatastoreType.OPERATIONAL, key.toInstanceIndentifier());
+                transaction.delete(LogicalDatastoreType.OPERATIONAL, getKey().toInstanceIndentifier());
             }
         } catch (Exception e) {
             LOG.warn("Failure to delete ovsdbNode {}",e);