delete UNI command 26/29226/8
authorMohamed El-Serngawy <melserngawy@inocybe.com>
Tue, 3 Nov 2015 23:10:15 +0000 (18:10 -0500)
committermelserngawy <melserngawy@inocybe.com>
Fri, 6 Nov 2015 19:41:24 +0000 (14:41 -0500)
Change-Id: I0b531a84ab9e0ac8c7bdab9ebc0013e1039efe13
Signed-off-by: Mohamed El-Serngawy <melserngawy@inocybe.com>
impl/src/main/java/org/opendaylight/unimgr/command/UniDeleteCommand.java

index 9aa7ab8520eea231dec1959d644a138cf68df107..c0e0ed8289ff4db56c2f4efacba2c39ecc3b8d18 100644 (file)
@@ -7,18 +7,27 @@
  */
 package org.opendaylight.unimgr.command;
 
-import java.util.Map;
 import java.util.Set;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.unimgr.impl.UnimgrConstants;
+import org.opendaylight.unimgr.impl.UnimgrMapper;
 import org.opendaylight.unimgr.impl.UnimgrUtils;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
+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.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
+
 public class UniDeleteCommand extends AbstractDeleteCommand {
 
     private static final Logger LOG = LoggerFactory.getLogger(UniDeleteCommand.class);
@@ -31,13 +40,54 @@ public class UniDeleteCommand extends AbstractDeleteCommand {
 
     @Override
     public void execute() {
-        Map<InstanceIdentifier<Uni>, Uni> originalUnis = UnimgrUtils.extractOriginal(changes, Uni.class);
-        Set<InstanceIdentifier<Uni>> removedUnis = UnimgrUtils.extractRemoved(changes, Uni.class);
+        Set<InstanceIdentifier<UniAugmentation>> removedUnis = UnimgrUtils.extractRemoved(changes, UniAugmentation.class);
+        Set<InstanceIdentifier<Node>> removedNodes = UnimgrUtils.extractRemoved(changes, Node.class);
         if (!removedUnis.isEmpty()) {
-            for (InstanceIdentifier<Uni> removedUniIid: removedUnis) {
-                LOG.info("Received a request to remove an UNI ", removedUniIid);
+            for (InstanceIdentifier<UniAugmentation> removedUniIid: removedUnis) {
+                UniAugmentation uniAug = UnimgrUtils.read(dataBroker, LogicalDatastoreType.CONFIGURATION, removedUniIid);
+                if(uniAug != null){
+                    LOG.info("Uni Augmentation present.");
+                    OvsdbNodeRef ovsNdRef = uniAug.getOvsdbNodeRef();
+                    InstanceIdentifier<Node> iidNode = (InstanceIdentifier<Node>) ovsNdRef.getValue();
+                    Optional<Node> optNode = UnimgrUtils.readNode(dataBroker, LogicalDatastoreType.CONFIGURATION, iidNode);
+                    if (optNode.isPresent()) {
+                        Node ovsdbNode = optNode.get();
+                        InstanceIdentifier<Node> iidBridgeNode = UnimgrMapper.getOvsdbBridgeNodeIID(ovsdbNode.getNodeId(), UnimgrConstants.DEFAULT_BRIDGE_NAME);
+                        deleteNode(iidBridgeNode);
+                        LOG.info("Received a request to remove a UNI BridgeNode ", iidBridgeNode);
+                    }
+                    deleteNode(iidNode);
+                    LOG.info("Received a request to remove an UNI ", removedUniIid);
+                }
             }
         }
+        else {LOG.info("Removed UNIs is empty.");}
+
+        if(!removedNodes.isEmpty()) {
+            for(InstanceIdentifier<Node> iidNode : removedNodes) {
+                Optional<Node> optNode = UnimgrUtils.readNode(dataBroker, LogicalDatastoreType.CONFIGURATION, iidNode);
+                if (optNode.isPresent()) {
+                    Node ovsdbNode = optNode.get();
+                    InstanceIdentifier<Node> iidBridgeNode = UnimgrMapper.getOvsdbBridgeNodeIID(ovsdbNode.getNodeId(), UnimgrConstants.DEFAULT_BRIDGE_NAME);
+                    deleteNode(iidBridgeNode);
+                    LOG.info("Received a request to remove a BridgeNode ", iidBridgeNode);
+                }
+                deleteNode(iidNode);
+                LOG.info("Received a request to remove a Node ", iidNode);
+           }
+        }
+        else {LOG.info("Removed Nodes is empty.");}
     }
 
+    private void deleteNode(InstanceIdentifier<Node> iidNode) {
+        WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
+        transaction.delete(LogicalDatastoreType.OPERATIONAL, iidNode);
+        transaction.delete(LogicalDatastoreType.CONFIGURATION, iidNode);
+        CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
+        try {
+            future.checkedGet();
+        } catch (TransactionCommitFailedException e) {
+            LOG.warn("Failed to delete iidNode {} {}", e.getMessage(), iidNode);
+        }
+    }
 }