Remove Physical Switch by Listening on DS
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / PhysicalSwitchRemoveCommand.java
index 29777c1a04b0cd1292bb834b3595215d4c1ca211..e728135053fb0d810f230bdeeafc64d5e3d5185b 100644 (file)
@@ -8,14 +8,30 @@
 
 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
 
+import static org.opendaylight.ovsdb.lib.operations.Operations.op;
+
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
 
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+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.hardwarevtep.Global;
+import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
 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.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
+
 public class PhysicalSwitchRemoveCommand extends AbstractTransactCommand {
     private static final Logger LOG = LoggerFactory.getLogger(PhysicalSwitchRemoveCommand.class);
 
@@ -26,7 +42,57 @@ public class PhysicalSwitchRemoveCommand extends AbstractTransactCommand {
 
     @Override
     public void execute(TransactionBuilder transaction) {
-        //TODO
+        Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> removeds =
+                extractRemoved(getChanges(),PhysicalSwitchAugmentation.class);
+        if (!removeds.isEmpty()) {
+            for (Entry<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> removed:
+                removeds.entrySet()) {
+                removePhysicalSwitch(transaction,  removed.getKey(), removed.getValue());
+            }
+        }
     }
 
+    private void removePhysicalSwitch(TransactionBuilder transaction,
+            InstanceIdentifier<Node> iid, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
+        LOG.debug("Removing a physical switch named: {}", physicalSwitchAugmentation.getHwvtepNodeName().getValue());
+        Optional<PhysicalSwitchAugmentation> operationalPhysicalSwitchOptional =
+                getOperationalState().getPhysicalSwitchAugmentation(iid);
+        PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalSwitch.class, null);
+        if (operationalPhysicalSwitchOptional.isPresent() &&
+                operationalPhysicalSwitchOptional.get().getPhysicalSwitchUuid() != null) {
+            UUID physicalSwitchUuid = new UUID(operationalPhysicalSwitchOptional.get().getPhysicalSwitchUuid().getValue());
+            Global global = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
+                    Global.class, null);
+            transaction.add(op.delete(physicalSwitch.getSchema())
+                    .where(physicalSwitch.getUuidColumn().getSchema().opEqual(physicalSwitchUuid)).build());
+            transaction.add(op.comment("Physical Switch: Deleting " + physicalSwitchAugmentation.getHwvtepNodeName().getValue()));
+            transaction.add(op.mutate(global.getSchema())
+                    .addMutation(global.getSwitchesColumn().getSchema(), Mutator.DELETE,
+                            Sets.newHashSet(physicalSwitchUuid)));
+            transaction.add(op.comment("Global: Mutating " + physicalSwitchAugmentation.getHwvtepNodeName().getValue() + " " + physicalSwitchUuid));
+        } else {
+            LOG.warn("Unable to delete physical switch {} because it was not found in the operational store",
+                    physicalSwitchAugmentation.getHwvtepNodeName().getValue());
+        }
+    }
+
+    private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractRemoved(
+            Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
+        Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result
+            = new HashMap<InstanceIdentifier<Node>, PhysicalSwitchAugmentation>();
+        if (changes != null && !changes.isEmpty()) {
+            for (DataTreeModification<Node> change : changes) {
+                final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
+                final DataObjectModification<Node> mod = change.getRootNode();
+                Node removed = TransactUtils.getRemoved(mod);
+                if (removed != null) {
+                    PhysicalSwitchAugmentation physicalSwitch = removed.getAugmentation(PhysicalSwitchAugmentation.class);
+                    if (physicalSwitch != null) {
+                        result.put(key, physicalSwitch);
+                    }
+                }
+            }
+        }
+        return result;
+    }
 }