Break out ControllerUpdate and Remove into seperate commands
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / BridgeCreateCommand.java
index 89e517c90d4ea2b5a07f212ce2af3f62df9243a3..86591538a61347e1b6a34e79a54fda08406c8dc4 100644 (file)
@@ -9,52 +9,90 @@ package org.opendaylight.ovsdb.southbound.ovsdb.transact;
 
 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
 
+import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
-import org.opendaylight.ovsdb.lib.notation.Mutator;
 import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
-import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
+import org.opendaylight.ovsdb.schema.openvswitch.Interface;
+import org.opendaylight.ovsdb.schema.openvswitch.Port;
+import org.opendaylight.ovsdb.southbound.SouthboundConstants;
+import org.opendaylight.ovsdb.southbound.SouthboundMapper;
+import org.opendaylight.ovsdb.southbound.SouthboundUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeInternal;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.Sets;
 
-public class BridgeCreateCommand implements TransactCommand {
-    private AsyncDataChangeEvent<InstanceIdentifier<?>, OvsdbBridgeAugmentation> changes;
-    private static final Logger LOG = LoggerFactory.getLogger(BridgeCreateCommand.class);
+public class BridgeCreateCommand extends AbstractTransactCommand {
 
+    private static final Logger LOG = LoggerFactory.getLogger(BridgeCreateCommand.class);
 
-    public BridgeCreateCommand(AsyncDataChangeEvent<InstanceIdentifier<?>, OvsdbBridgeAugmentation> changes) {
-        this.changes = changes;
+    public BridgeCreateCommand(BridgeOperationalState state,
+            AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
+        super(state, changes);
     }
 
+
+
     @Override
     public void execute(TransactionBuilder transaction) {
-        Map<InstanceIdentifier<Node>, OvsdbBridgeAugmentation> created = TransactUtils.extractOvsdbManagedNodeCreate(changes);
-        for(OvsdbBridgeAugmentation ovsdbManagedNode: created.values()) {
+        Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> created =
+                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());
-            String namedUuid = "Bridge_" + ovsdbManagedNode.getBridgeName().getValue();
-            transaction.add(op.insert(bridge).withId(namedUuid));
-
-            // OpenVSwitchPart
-            OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class);
-            ovs.setBridges(Sets.newHashSet(new UUID(namedUuid)));
-            transaction.add(op.mutate(ovs).addMutation(ovs.getBridgesColumn().getSchema(),
-                    Mutator.INSERT,
-                    ovs.getBridgesColumn().getData())
-                    );
+            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));
+            if (SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode) != null
+                    && SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode).size() > 0) {
+                bridge.setProtocols(SouthboundMapper.createOvsdbBridgeProtocols(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));
         }
     }