From 61d1a558bde555d5bf8e5f15ce5560bf0c749dab Mon Sep 17 00:00:00 2001 From: Peng Zhang Date: Thu, 10 Dec 2015 12:35:37 +0800 Subject: [PATCH] Remove Physical Switch by Listening on DS 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 --- .../transact/PhysicalSwitchRemoveCommand.java | 68 ++++++++++++++++++- ...thbound-Collection.json.postman_collection | 12 ++-- 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/PhysicalSwitchRemoveCommand.java b/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/PhysicalSwitchRemoveCommand.java index 29777c1a0..e72813505 100644 --- a/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/PhysicalSwitchRemoveCommand.java +++ b/hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/transact/PhysicalSwitchRemoveCommand.java @@ -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, PhysicalSwitchAugmentation> removeds = + extractRemoved(getChanges(),PhysicalSwitchAugmentation.class); + if (!removeds.isEmpty()) { + for (Entry, PhysicalSwitchAugmentation> removed: + removeds.entrySet()) { + removePhysicalSwitch(transaction, removed.getKey(), removed.getValue()); + } + } } + private void removePhysicalSwitch(TransactionBuilder transaction, + InstanceIdentifier iid, PhysicalSwitchAugmentation physicalSwitchAugmentation) { + LOG.debug("Removing a physical switch named: {}", physicalSwitchAugmentation.getHwvtepNodeName().getValue()); + Optional 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, PhysicalSwitchAugmentation> extractRemoved( + Collection> changes, Class class1) { + Map, PhysicalSwitchAugmentation> result + = new HashMap, PhysicalSwitchAugmentation>(); + if (changes != null && !changes.isEmpty()) { + for (DataTreeModification change : changes) { + final InstanceIdentifier key = change.getRootPath().getRootIdentifier(); + final DataObjectModification 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; + } } diff --git a/resources/commons/Ovsdb-HwvtepSouthbound-Collection.json.postman_collection b/resources/commons/Ovsdb-HwvtepSouthbound-Collection.json.postman_collection index 29d00df9c..e6831da3d 100755 --- a/resources/commons/Ovsdb-HwvtepSouthbound-Collection.json.postman_collection +++ b/resources/commons/Ovsdb-HwvtepSouthbound-Collection.json.postman_collection @@ -264,13 +264,13 @@ "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", @@ -283,13 +283,13 @@ "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", @@ -307,7 +307,7 @@ "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", @@ -327,7 +327,7 @@ "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": [], -- 2.36.6