Update 7.1 port-mapping data after inf delete
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / openroadminterfaces / OpenRoadmInterfacesImpl710.java
index 358b0dd7ddb0386d87025a40b6b2bb4b506fa335..4152b2e38d7549abbee43b7d8d958277e5444740 100644 (file)
@@ -19,6 +19,11 @@ 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;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksKey;
@@ -26,9 +31,11 @@ import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfac
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.OrgOpenroadmDevice;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.port.Interfaces;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.States;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -37,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 {
@@ -65,9 +77,30 @@ public class OpenRoadmInterfacesImpl710 {
         deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
         FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
+        // TODO: instead of using this infinite loop coupled with this timeout,
+        // it would be better to use a notification mechanism from the device to be advertised
+        // that the new created interface is present in the device circuit-pack/port
+        final Thread current = Thread.currentThread();
+        Thread timer = new Thread() {
+            public void run() {
+                try {
+                    Thread.sleep(3000);
+                    current.interrupt();
+                } catch (InterruptedException e) {
+                    LOG.error("Timeout before the new created interface appears on the deivce circuit-pack port", e);
+                }
+            }
+        };
         try {
             txSubmitFuture.get();
             LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
+            boolean devicePortIsUptodated = false;
+            while (!devicePortIsUptodated) {
+                devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
+            }
+            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
                 .getName(), nodeId), e);
@@ -83,7 +116,7 @@ public class OpenRoadmInterfacesImpl710 {
     }
 
 
-    public void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
+    public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
         LOG.info("deleting interface {} on device71 {}", interfaceName, nodeId);
         Optional<Interface> intf2DeleteOpt;
         try {
@@ -94,11 +127,20 @@ public class OpenRoadmInterfacesImpl710 {
         }
         if (intf2DeleteOpt.isPresent()) {
             Interface intf2Delete = intf2DeleteOpt.get();
-            // State admin state to out of service
-            InterfaceBuilder ifBuilder = new InterfaceBuilder();
-            ifBuilder.setName(intf2Delete.getName());
-            ifBuilder.setType(intf2Delete.getType());
-            ifBuilder.setAdministrativeState(AdminStates.OutOfService);
+            // set the name and set the type. Having these two lines will post the interface with just
+            // name, type and admin-state, without all the default values such as maint-testsignal
+            //  delete the interfaces successfully
+            // just build a new Interface builder without the arguments for inter2Delete
+            InterfaceBuilder ifBuilder = new InterfaceBuilder()
+                .setAdministrativeState(AdminStates.OutOfService)
+                // 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());
+
             // post interface with updated admin state
             try {
                 postInterface(nodeId, ifBuilder);
@@ -140,6 +182,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 {
@@ -203,4 +250,35 @@ public class OpenRoadmInterfacesImpl710 {
         }
     }
 
+    public String getSupportedInterface(String nodeId, String interf) {
+        Optional<Interface> supInterfOpt;
+        try {
+            supInterfOpt = getInterface(nodeId, interf);
+            if (supInterfOpt.isPresent()) {
+                return supInterfOpt.get().getSupportingInterfaceList().get(0);
+            } else {
+                return null;
+            }
+        } catch (OpenRoadmInterfaceException e) {
+            LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
+            return null;
+        }
+    }
+
+    private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
+        KeyedInstanceIdentifier<Ports, PortsKey> portIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
+            .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()));
+        Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
+            portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
+        if (port.getInterfaces() == null) {
+            return false;
+        }
+        for (Interfaces interf : port.getInterfaces()) {
+            if (interf.getInterfaceName().equals(ifBuilder.getName())) {
+                return true;
+            }
+        }
+        return false;
+    }
 }