Remove Physical Switch by Listening on DS 10/31110/7
authorPeng Zhang <pzhang@ctbri.com.cn>
Thu, 10 Dec 2015 04:35:37 +0000 (12:35 +0800)
committerPeng Zhang <pzhang@ctbri.com.cn>
Thu, 17 Dec 2015 12:08:32 +0000 (20:08 +0800)
PatchSet 2:
1. add mutation to Global table
2. remove a unused import

PatchSet 3:
1. fix typos in postman collection

Change-Id: I7a13208f16feb0a0f3af7a09f2a3a263dcc1f9cf
Signed-off-by: Peng Zhang <pzhang@ctbri.com.cn>
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/PhysicalSwitchRemoveCommand.java
resources/commons/Ovsdb-HwvtepSouthbound-Collection.json.postman_collection

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;
+    }
 }
index 29d00df9ce49116bd317df0924f9d734d01fb43b..e6831da3dce0dd52b9e79e8d9a78f3c519df0f64 100755 (executable)
             "time": 1447909152443,
             "preRequestScript": "",
             "tests": "",
-            "rawModeData": "{\n  \"network-topology:node\": [\n        {\n            \"node-id\": \"hwvtep://{{hwvtepNodeIp}}:6640/physicalswitch/ps0\",\n            \"hwvtep-node-name\": \"ps0\",\n            \"hwvtep-node-description\": \"\",\n            \"management-ips\": [\n              {\n                \"management-ips-key\": \"{{managementIp}}\"\n              }\n            ],\n            \"tunnel-ips\": [\n              {\n                \"tunnel-ips-key\": \"{{tunnleIp}}\"\n              }\n            ],\n            \"managed-by\": \"/network-topology:network-topology/network-topology:topology[network-topology:topology-id='hwvtep:1']/network-topology:node[network-topology:node-id='hwvtep://{{hwvtepNodeIp}}:6640']\"             \n        }\n    ]\n}"
+            "rawModeData": "{\n  \"network-topology:node\": [\n        {\n            \"node-id\": \"hwvtep://{{hwvtepNodeIp}}:6640/physicalswitch/br0\",\n            \"hwvtep-node-name\": \"br0\",\n            \"hwvtep-node-description\": \"\",\n            \"management-ips\": [\n              {\n                \"management-ips-key\": \"{{managementIp}}\"\n              }\n            ],\n            \"tunnel-ips\": [\n              {\n                \"tunnel-ips-key\": \"{{tunnelIp}}\"\n              }\n            ],\n            \"managed-by\": \"/network-topology:network-topology/network-topology:topology[network-topology:topology-id='hwvtep:1']/network-topology:node[network-topology:node-id='hwvtep://{{hwvtepNodeIp}}:6640']\"             \n        }\n    ]\n}"
         },
         {
             "collectionId": "19f6b1a8-4d54-62f8-6bd6-f52e0b6e40b8",
             "id": "e25f67e4-3a3f-5da7-f7c8-41b371156719",
             "headers": "Authorization: Basic YWRtaW46YWRtaW4=\nContent-Type: application/json\n",
-            "url": "http://{{controllerHost}}:8181/restconf/config/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%phsicalswitch%2Fps0",
+            "url": "http://{{controllerHost}}:8181/restconf/config/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%2Fphysicalswitch%2Fbr0",
             "preRequestScript": "",
             "pathVariables": {},
             "method": "PUT",
             "time": 1448552632013,
             "name": "Update Specific Config Physical Switch",
             "description": "Fire this Restconf request if you want to update a exsiting physical switch.",
-            "rawModeData": "{\n  \"network-topology:node\": [\n        {\n            \"node-id\": \"hwvtep://{{hwvtepNodeIp}}:6640/physicalswitch/ps0\",\n            \"hwvtep-node-name\": \"ps0\",\n            \"hwvtep-node-description\": \"\",\n            \"management-ips\": [\n              {\n                \"management-ips-key\": \"{{managementIp}}\"\n              }\n            ],\n            \"tunnel-ips\": [\n              {\n                \"tunnel-ips-key\": \"{{tunnleIp}}\"\n              }\n            ],\n            \"managed-by\": \"/network-topology:network-topology/network-topology:topology[network-topology:topology-id='hwvtep:1']/network-topology:node[network-topology:node-id='hwvtep://{{hwvtepNodeIp}}:6640']\"             \n        }\n    ]\n}"
+            "rawModeData": "{\n  \"network-topology:node\": [\n        {\n            \"node-id\": \"hwvtep://{{hwvtepNodeIp}}:6640/physicalswitch/br0\",\n            \"hwvtep-node-name\": \"br0\",\n            \"hwvtep-node-description\": \"\",\n            \"management-ips\": [\n              {\n                \"management-ips-key\": \"{{managementIp}}\"\n              }\n            ],\n            \"tunnel-ips\": [\n              {\n                \"tunnel-ips-key\": \"{{tunnelIp}}\"\n              }\n            ],\n            \"managed-by\": \"/network-topology:network-topology/network-topology:topology[network-topology:topology-id='hwvtep:1']/network-topology:node[network-topology:node-id='hwvtep://{{hwvtepNodeIp}}:6640']\"             \n        }\n    ]\n}"
         },
         {
             "collectionId": "19f6b1a8-4d54-62f8-6bd6-f52e0b6e40b8",
             "id": "f7d33d50-7c8e-69e2-6d60-5c6155c5d210",
             "headers": "Authorization: Basic YWRtaW46YWRtaW4=\nContent-Type: application/json\n",
-            "url": "http://{{controllerHost}}:8181/restconf/config/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%phsicalswitch%2Fps0",
+            "url": "http://{{controllerHost}}:8181/restconf/config/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%2Fphysicalswitch%2Fbr0",
             "preRequestScript": "",
             "pathVariables": {},
             "method": "GET",
             "collectionId": "19f6b1a8-4d54-62f8-6bd6-f52e0b6e40b8",
             "id": "233b5ab5-8177-4416-6912-2477520a0654",
             "headers": "Authorization: Basic YWRtaW46YWRtaW4=\nContent-Type: application/json\n",
-            "url": "http://{{controllerHost}}:8181/restconf/config/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%phsicalswitch%2Fps0",
+            "url": "http://{{controllerHost}}:8181/restconf/config/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%2Fphysicalswitch%2Fbr0",
             "preRequestScript": "",
             "pathVariables": {},
             "method": "DELETE",
             "id": "a13e6877-997e-84e1-c8e8-e83ef5e9a002",
             "name": "Get Specific Operational Physical Switch",
             "description": "",
-            "url": "http://{{controllerHost}}:8181/restconf/operational/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%2Fphysicalswitch%2Fps0",
+            "url": "http://{{controllerHost}}:8181/restconf/operational/network-topology:network-topology/topology/hwvtep:1/node/hwvtep:%2F%2F{{hwvtepNodeIp}}:6640%2Fphysicalswitch%2Fbr0",
             "method": "GET",
             "headers": "Authorization: Basic YWRtaW46YWRtaW4=\n",
             "data": [],