Merge "Fixed postman collection for create termination point restconf request"
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / BridgeCreateCommand.java
index f24ca7c6ba295215f7facaa04f49ae126c1a728d..24495a1c099e1a9364a3e300b981401f237dc860 100644 (file)
@@ -9,6 +9,7 @@ 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;
 
@@ -19,11 +20,15 @@ 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.Controller;
+import org.opendaylight.ovsdb.schema.openvswitch.Interface;
 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
+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;
@@ -31,41 +36,71 @@ import org.slf4j.LoggerFactory;
 import com.google.common.collect.Sets;
 
 public class BridgeCreateCommand implements TransactCommand {
-    private AsyncDataChangeEvent<InstanceIdentifier<?>, OvsdbBridgeAugmentation> changes;
+    private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes;
     private static final Logger LOG = LoggerFactory.getLogger(BridgeCreateCommand.class);
 
 
-    public BridgeCreateCommand(AsyncDataChangeEvent<InstanceIdentifier<?>, OvsdbBridgeAugmentation> changes) {
+    public BridgeCreateCommand(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
         this.changes = 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(changes,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())));
+            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())));
             }
-            if(SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode) != null
-                    && SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode).size() > 0){
+            bridge.setDatapathType(SouthboundMapper.createDatapathType(ovsdbManagedNode));
+            if (SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode) != null
+                    && SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode).size() > 0) {
                 bridge.setProtocols(SouthboundMapper.createOvsdbBridgeProtocols(ovsdbManagedNode));
             }
-            Map<UUID,Controller> controllerMap = SouthboundMapper.createOvsdbController(ovsdbManagedNode, transaction.getDatabaseSchema());
-            for(Entry<UUID,Controller >entry: controllerMap.entrySet()) {
+            Map<UUID,Controller> controllerMap = SouthboundMapper.createOvsdbController(
+                    ovsdbManagedNode, transaction.getDatabaseSchema());
+            for (Entry<UUID,Controller> entry: controllerMap.entrySet()) {
                 transaction.add(op.insert(entry.getValue()).withId(entry.getKey().toString()));
             }
-            if(!controllerMap.isEmpty()) {
+            if (!controllerMap.isEmpty()) {
                 bridge.setController(controllerMap.keySet());
             }
+            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);
 
-            String bridgeNamedUuid = "Bridge_" + ovsdbManagedNode.getBridgeName().getValue();
             transaction.add(op.insert(bridge).withId(bridgeNamedUuid));
 
             // OpenVSwitchPart
@@ -73,8 +108,7 @@ public class BridgeCreateCommand implements TransactCommand {
             ovs.setBridges(Sets.newHashSet(new UUID(bridgeNamedUuid)));
             transaction.add(op.mutate(ovs).addMutation(ovs.getBridgesColumn().getSchema(),
                     Mutator.INSERT,
-                    ovs.getBridgesColumn().getData())
-                    );
+                    ovs.getBridgesColumn().getData()));
         }
     }