Cleaned up BridgeCreateCommand 60/18160/4
authorEd Warnicke <eaw@cisco.com>
Sun, 12 Apr 2015 03:18:52 +0000 (20:18 -0700)
committerEd Warnicke <eaw@cisco.com>
Mon, 13 Apr 2015 17:49:21 +0000 (17:49 +0000)
Inspired by the good work of  Ryan Goulding in these patches:
https://git.opendaylight.org/gerrit/#/c/18135/
https://git.opendaylight.org/gerrit/#/c/18138/

I cleaned up BridgeCreateCommand... because I was getting lost
in there ;)

Change-Id: Ia4b4e95f99749e75a038cd8062ad74b790598179
Signed-off-by: Ed Warnicke <eaw@cisco.com>
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/BridgeCreateCommand.java

index dfbad83ebd3f7b584a456387e1ffcf73a1f0d045..378c80b9aee5bf89056e553f039ac751e4d7447c 100644 (file)
@@ -15,7 +15,9 @@ import java.util.Map.Entry;
 
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
 import org.opendaylight.ovsdb.lib.notation.UUID;
+import org.opendaylight.ovsdb.lib.operations.Insert;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
+import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
@@ -49,46 +51,74 @@ public class BridgeCreateCommand extends AbstractTransactCommand {
                 TransactUtils.extractCreated(getChanges(),OvsdbBridgeAugmentation.class);
         for (Entry<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> ovsdbManagedNodeEntry:
             created.entrySet()) {
-            OvsdbBridgeAugmentation ovsdbManagedNode = ovsdbManagedNodeEntry.getValue();
-            LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
-                        ovsdbManagedNode.getBridgeName(),
-                        ovsdbManagedNode.getBridgeUuid());
-
-            // Named UUIDs
-            String bridgeNamedUuid = "Bridge_" + SouthboundMapper.getRandomUUID();
-            String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
-            String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
-
-            // Interface part
-            Interface interfaceOvs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
-            interfaceOvs.setName(ovsdbManagedNode.getBridgeName().getValue());
-            interfaceOvs.setType(SouthboundMapper.createOvsdbInterfaceType(InterfaceTypeInternal.class));
-            transaction.add(op.insert(interfaceOvs).withId(interfaceNamedUuid));
-
-            // Port part
-            Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
-            port.setName(ovsdbManagedNode.getBridgeName().getValue());
-            port.setInterfaces(Sets.newHashSet(new UUID(interfaceNamedUuid)));
-            transaction.add(op.insert(port).withId(portNamedUuid));
-
-            // Bridge part
-            Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
-            bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
-            if (ovsdbManagedNode.getFailMode() != null
-                    && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null ) {
-                bridge.setFailMode(Sets.newHashSet(
-                        SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));
-            }
-            bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
-            bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
-
-            // Set the iid external_id
-            Map<String,String> externalIds = new HashMap<String,String>();
-            externalIds.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
-                    SouthboundUtil.serializeInstanceIdentifier(ovsdbManagedNodeEntry.getKey()));
-            bridge.setExternalIds(externalIds);
-
-            transaction.add(op.insert(bridge).withId(bridgeNamedUuid));
+            updateBridge(transaction,  ovsdbManagedNodeEntry.getKey(), ovsdbManagedNodeEntry.getValue());
+        }
+    }
+
+
+
+    private void updateBridge(
+            TransactionBuilder transaction,
+            InstanceIdentifier<OvsdbBridgeAugmentation> iid, OvsdbBridgeAugmentation ovsdbManagedNode) {
+        LOG.debug("Received request to create ovsdb bridge name: {} uuid: {}",
+                    ovsdbManagedNode.getBridgeName(),
+                    ovsdbManagedNode.getBridgeUuid());
+        Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
+        bridge.setName(ovsdbManagedNode.getBridgeName().getValue());
+        setFailMode(bridge, ovsdbManagedNode);
+        bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
+        setPort(transaction, bridge, ovsdbManagedNode);
+        setOpenDaylightIidExternalId(bridge, iid);
+        String bridgeNamedUuid = "Bridge_" + SouthboundMapper.getRandomUUID();
+        transaction.add(op.insert(bridge).withId(bridgeNamedUuid));
+    }
+
+
+
+    private void setOpenDaylightIidExternalId(Bridge bridge,
+            InstanceIdentifier<OvsdbBridgeAugmentation> iid) {
+        // Set the iid external_id
+        Map<String,String> externalIds = new HashMap<String,String>();
+        externalIds.put(SouthboundConstants.IID_EXTERNAL_ID_KEY,
+                SouthboundUtil.serializeInstanceIdentifier(iid));
+        bridge.setExternalIds(externalIds);
+    }
+
+
+
+    private void setPort(TransactionBuilder transaction, Bridge bridge,
+            OvsdbBridgeAugmentation ovsdbManagedNode) {
+
+        Insert<GenericTableSchema> interfaceInsert = setInterface(transaction,ovsdbManagedNode);
+        // Port part
+        String portNamedUuid = "Port_" + SouthboundMapper.getRandomUUID();
+        Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
+        port.setName(ovsdbManagedNode.getBridgeName().getValue());
+        port.setInterfaces(Sets.newHashSet(TransactUtils.extractNamedUuid(interfaceInsert)));
+        transaction.add(op.insert(port).withId(portNamedUuid));
+        bridge.setPorts(Sets.newHashSet(new UUID(portNamedUuid)));
+    }
+
+    private Insert<GenericTableSchema> setInterface(TransactionBuilder transaction,
+            OvsdbBridgeAugmentation ovsdbManagedNode) {
+        // Interface part
+        String interfaceNamedUuid = "Interface_" + SouthboundMapper.getRandomUUID();
+        Interface interfaceOvs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
+        interfaceOvs.setName(ovsdbManagedNode.getBridgeName().getValue());
+        interfaceOvs.setType(SouthboundMapper.createOvsdbInterfaceType(InterfaceTypeInternal.class));
+        Insert<GenericTableSchema> result = op.insert(interfaceOvs).withId(interfaceNamedUuid);
+        transaction.add(result);
+        return result;
+    }
+
+
+
+    private void setFailMode(Bridge bridge,
+            OvsdbBridgeAugmentation ovsdbManagedNode) {
+        if (ovsdbManagedNode.getFailMode() != null
+                && SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode()) != null ) {
+            bridge.setFailMode(Sets.newHashSet(
+                    SouthboundConstants.OVSDB_FAIL_MODE_MAP.get(ovsdbManagedNode.getFailMode())));
         }
     }