From: Ed Warnicke Date: Sun, 12 Apr 2015 03:18:52 +0000 (-0700) Subject: Cleaned up BridgeCreateCommand X-Git-Tag: release/lithium~157^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F60%2F18160%2F4;p=ovsdb.git Cleaned up BridgeCreateCommand 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 --- diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/BridgeCreateCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/BridgeCreateCommand.java index dfbad83eb..378c80b9a 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/BridgeCreateCommand.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/BridgeCreateCommand.java @@ -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, 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 externalIds = new HashMap(); - 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 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 iid) { + // Set the iid external_id + Map externalIds = new HashMap(); + externalIds.put(SouthboundConstants.IID_EXTERNAL_ID_KEY, + SouthboundUtil.serializeInstanceIdentifier(iid)); + bridge.setExternalIds(externalIds); + } + + + + private void setPort(TransactionBuilder transaction, Bridge bridge, + OvsdbBridgeAugmentation ovsdbManagedNode) { + + Insert 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 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 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()))); } }