Generalize supporting interface (B100G) in port-mapping
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / openroadminterfaces / OpenRoadmInterfacesImpl221.java
index dab2a43224b11e31ed44d4bbae3159ea4ad950bc..ab79254e944a33ca1c6424e211af5cfa4aa64fb0 100755 (executable)
@@ -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.PortMappingVersion221;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220114.mapping.Mapping;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.Ports;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.PortsKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacks;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksKey;
@@ -26,6 +31,7 @@ import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfac
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.port.Interfaces;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.AdminStates;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.States;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOdu;
@@ -37,6 +43,7 @@ import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181
 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.odu.container.OduBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.otu.container.OtuBuilder;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -45,9 +52,14 @@ public class OpenRoadmInterfacesImpl221 {
     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl221.class);
 
     private final DeviceTransactionManager deviceTransactionManager;
+    private final PortMapping portMapping;
+    private final PortMappingVersion221 portMapping221;
 
-    public OpenRoadmInterfacesImpl221(DeviceTransactionManager deviceTransactionManager) {
+    public OpenRoadmInterfacesImpl221(DeviceTransactionManager deviceTransactionManager,
+            PortMapping portMapping, PortMappingVersion221 portMapping221) {
         this.deviceTransactionManager = deviceTransactionManager;
+        this.portMapping = portMapping;
+        this.portMapping221 = portMapping221;
     }
 
     public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
@@ -70,12 +82,33 @@ public class OpenRoadmInterfacesImpl221 {
             Interface.class, new InterfaceKey(ifBuilder.getName()));
         LOG.info("POST INTERF for {} : InterfaceBuilder : name = {} \t type = {}", nodeId, ifBuilder.getName(),
             ifBuilder.getType().toString());
-        deviceTx.put(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
+        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);
@@ -91,7 +124,7 @@ public class OpenRoadmInterfacesImpl221 {
     }
 
 
-    public void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
+    public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
         LOG.info("deleting interface {} on device221 {}", interfaceName, nodeId);
         Optional<Interface> intf2DeleteOpt;
         try {
@@ -113,7 +146,7 @@ public class OpenRoadmInterfacesImpl221 {
                     odu.setMaintTestsignal(maintSignalBuilder.build());
                 }
                 oduBuilder.setOdu(odu.build());
-                ifBuilder.addAugmentation(Interface1.class, oduBuilder.build());
+                ifBuilder.addAugmentation(oduBuilder.build());
             } else if (ifBuilder.getType() == OtnOtu.class) {
                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1Builder
                     otuBuilder =
@@ -128,9 +161,7 @@ public class OpenRoadmInterfacesImpl221 {
                     otu.setMaintLoopback(maintLoopBackBuilder.build());
                 }
                 otuBuilder.setOtu(otu.build());
-                ifBuilder.addAugmentation(
-                    org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1.class,
-                    otuBuilder.build());
+                ifBuilder.addAugmentation(otuBuilder.build());
             }
             ifBuilder.setAdministrativeState(AdminStates.OutOfService);
             // post interface with updated admin state
@@ -174,6 +205,10 @@ public class OpenRoadmInterfacesImpl221 {
             if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
                 StringConstants.NETWORK_TOKEN)) {
                 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
+                Mapping oldmapping = this.portMapping.getMapping(nodeId, intf2Delete.getSupportingCircuitPackName(),
+                    intf2Delete.getSupportingPort());
+                this.portMapping.deleteMapping(nodeId, oldmapping.getLogicalConnectionPoint());
+                this.portMapping221.updateMapping(nodeId, oldmapping);
             }
 
         } else {
@@ -198,13 +233,13 @@ public class OpenRoadmInterfacesImpl221 {
         CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
         boolean change = false;
         if (activate) {
-            if (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().getName()
-                .equals(States.NotReservedInuse)) {
+            if (cpBldr.getEquipmentState() != null
+                    && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
                 cpBldr.setEquipmentState(States.NotReservedInuse);
                 change = true;
             }
-        } else if ((cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().getName()
-            .equals(States.NotReservedAvailable))) {
+        } else if ((cpBldr.getEquipmentState() != null
+                && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
             cpBldr.setEquipmentState(States.NotReservedAvailable);
             change = true;
         }
@@ -224,7 +259,7 @@ public class OpenRoadmInterfacesImpl221 {
                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
                     nodeId), e);
             }
-            deviceTx.put(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
+            deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
             FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
             try {
@@ -237,4 +272,35 @@ public class OpenRoadmInterfacesImpl221 {
         }
     }
 
+    public String getSupportedInterface(String nodeId, String interf) {
+        Optional<Interface> supInterfOpt;
+        try {
+            supInterfOpt = getInterface(nodeId, interf);
+            if (supInterfOpt.isPresent()) {
+                return supInterfOpt.get().getSupportingInterface();
+            } 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;
+    }
 }