Simplify delete operations
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / openroadminterfaces / OpenRoadmInterfacesImpl710.java
index 35fd0963fc5177efb2607334ea205a3954d43a3e..bd9e62ec0b1cd39aef34d302269011c7575bdbda 100644 (file)
@@ -19,6 +19,9 @@ import org.opendaylight.transportpce.common.StringConstants;
 import org.opendaylight.transportpce.common.Timeouts;
 import org.opendaylight.transportpce.common.device.DeviceTransaction;
 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
+import org.opendaylight.transportpce.common.mapping.PortMapping;
+import org.opendaylight.transportpce.common.mapping.PortMappingVersion710;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220114.mapping.Mapping;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.Ports;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.PortsKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
@@ -41,9 +44,14 @@ public class OpenRoadmInterfacesImpl710 {
     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl710.class);
 
     private final DeviceTransactionManager deviceTransactionManager;
+    private final PortMapping portMapping;
+    private final PortMappingVersion710 portMapping710;
 
-    public OpenRoadmInterfacesImpl710(DeviceTransactionManager deviceTransactionManager) {
+    public OpenRoadmInterfacesImpl710(DeviceTransactionManager deviceTransactionManager,
+            PortMapping portMapping, PortMappingVersion710 portMapping710) {
         this.deviceTransactionManager = deviceTransactionManager;
+        this.portMapping = portMapping;
+        this.portMapping710 = portMapping710;
     }
 
     public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
@@ -85,13 +93,17 @@ public class OpenRoadmInterfacesImpl710 {
         };
         try {
             txSubmitFuture.get();
-            LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
-            boolean devicePortIsUptodated = false;
-            while (!devicePortIsUptodated) {
-                devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
+            LOG.info("Successfully posted/deleted interface {} on node {}", ifBuilder.getName(), nodeId);
+            // this check is not needed during the delete operation
+            // during the delete operation, ifBuilder does not contain supporting-cp and supporting-port
+            if (ifBuilder.getSupportingCircuitPackName() != null && ifBuilder.getSupportingPort() != null) {
+                boolean devicePortIsUptodated = false;
+                while (!devicePortIsUptodated) {
+                    devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
+                }
+                LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
+                    ifBuilder.getName(), ifBuilder.getSupportingPort());
             }
-            LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
-                ifBuilder.getName(), ifBuilder.getSupportingPort());
             timer.interrupt();
         } catch (InterruptedException | ExecutionException e) {
             throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
@@ -128,10 +140,7 @@ public class OpenRoadmInterfacesImpl710 {
                 // Though these could be redundant, but 'when' statements are causing problem,
                 // when deleting the interfaces trying to be deleted
                 .setName(intf2Delete.getName())
-                .setType(intf2Delete.getType())
-                // CP name and the ports are needed, since the post interface is validated
-                .setSupportingCircuitPackName(intf2Delete.getSupportingCircuitPackName())
-                .setSupportingPort(intf2Delete.getSupportingPort());
+                .setType(intf2Delete.getType());
 
             // post interface with updated admin state
             try {
@@ -174,6 +183,11 @@ public class OpenRoadmInterfacesImpl710 {
             if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
                 StringConstants.NETWORK_TOKEN)) {
                 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
+                // Here we update the port-mapping data after the interface delete
+                Mapping oldMapping = this.portMapping.getMapping(
+                    nodeId, intf2Delete.getSupportingCircuitPackName(), intf2Delete.getSupportingPort());
+                this.portMapping.deleteMapping(nodeId, oldMapping.getLogicalConnectionPoint());
+                this.portMapping710.updateMapping(nodeId, oldMapping);
             }
 
         } else {