Merge "Keep AvailFreqMaps when netconf session restores"
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / crossconnect / CrossConnectImpl221.java
index 681b1f08062964915e8a5ed0403d5231c8ae295c..5dffcf234ea20492297eb116f4abee9d86cacf85 100644 (file)
@@ -24,6 +24,7 @@ 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.fixedflex.GridConstant;
+import org.opendaylight.transportpce.common.fixedflex.SpectrumInformation;
 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.OpticalControlMode;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.PowerDBm;
@@ -40,7 +41,7 @@ import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.open
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev201211.otn.renderer.input.Nodes;
+import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210930.otn.renderer.nodes.Nodes;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
@@ -49,6 +50,8 @@ import org.slf4j.LoggerFactory;
 public class CrossConnectImpl221 {
 
     private static final Logger LOG = LoggerFactory.getLogger(CrossConnectImpl221.class);
+    private static final String DEV_TRANSACTION_NOT_FOUND = "Device transaction for device {} was not found!";
+    private static final String UNABLE_DEV_TRANSACTION = "Unable to obtain device transaction for device {}!";
     private final DeviceTransactionManager deviceTransactionManager;
 
     public CrossConnectImpl221(DeviceTransactionManager deviceTransactionManager) {
@@ -63,7 +66,7 @@ public class CrossConnectImpl221 {
     }
 
     public Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org
-        .openroadm.device.OduConnection> getOtnCrossConnect(String deviceId, String connectionNumber) {
+            .openroadm.device.OduConnection> getOtnCrossConnect(String deviceId, String connectionNumber) {
         //TODO Change it to Operational later for real device
         return deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.CONFIGURATION,
                 generateOduConnectionIID(connectionNumber), Timeouts.DEVICE_READ_TIMEOUT,
@@ -71,30 +74,21 @@ public class CrossConnectImpl221 {
     }
 
     public Optional<String> postCrossConnect(String deviceId, String srcTp, String destTp,
-            int lowerSpectralSlotNumber, int higherSpectralSlotNumber) {
-        String spectralSlotName = String.join(GridConstant.SPECTRAL_SLOT_SEPARATOR,
-                String.valueOf(lowerSpectralSlotNumber),
-                String.valueOf(higherSpectralSlotNumber));
-        String connectionNumber = generateConnectionName(srcTp, destTp, spectralSlotName);
+                                             SpectrumInformation spectrumInformation) {
+        String connectionNumber = spectrumInformation.getIdentifierFromParams(srcTp, destTp);
         RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder()
                 .setConnectionName(connectionNumber)
                 .setOpticalControlMode(OpticalControlMode.Off)
                 .setSource(new SourceBuilder()
-                        .setSrcIf(String.join(GridConstant.NAME_PARAMETERS_SEPARATOR,
-                                srcTp,
-                                "nmc",
-                                spectralSlotName))
+                        .setSrcIf(spectrumInformation.getIdentifierFromParams(srcTp,"nmc"))
                         .build())
                 .setDestination(new DestinationBuilder()
-                        .setDstIf(String.join(GridConstant.NAME_PARAMETERS_SEPARATOR,
-                                destTp,
-                                "nmc",
-                                spectralSlotName))
+                        .setDstIf(spectrumInformation.getIdentifierFromParams(destTp,"nmc"))
                         .build());
 
         InstanceIdentifier<RoadmConnections> rdmConnectionIID =
                 InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                    .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionName()));
+                        .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionName()));
 
         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
         DeviceTransaction deviceTx;
@@ -103,22 +97,23 @@ public class CrossConnectImpl221 {
             if (deviceTxOpt.isPresent()) {
                 deviceTx = deviceTxOpt.get();
             } else {
-                LOG.error("Device transaction for device {} was not found!", deviceId);
+                LOG.error(DEV_TRANSACTION_NOT_FOUND, deviceId);
                 return Optional.empty();
             }
         } catch (InterruptedException | ExecutionException e) {
-            LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
+            LOG.error(UNABLE_DEV_TRANSACTION, deviceId, e);
             return Optional.empty();
         }
 
         // post the cross connect on the device
-        deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
+        deviceTx.merge(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
         FluentFuture<? extends @NonNull CommitInfo> commit =
                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
         try {
             commit.get();
-            LOG.info("Roadm-connection successfully created: {}-{}-{}-{}", srcTp, destTp, lowerSpectralSlotNumber,
-                    higherSpectralSlotNumber);
+            LOG.info("Roadm-connection successfully created: {}-{}-{}-{}", srcTp, destTp,
+                    spectrumInformation.getLowerSpectralSlotNumber(),
+                    spectrumInformation.getHigherSpectralSlotNumber());
             return Optional.of(connectionNumber);
         } catch (InterruptedException | ExecutionException e) {
             LOG.warn("Failed to post {}. Exception: ", rdmConnBldr.build(), e);
@@ -131,7 +126,7 @@ public class CrossConnectImpl221 {
         List<String> interfList = new ArrayList<>();
         Optional<RoadmConnections> xc = getCrossConnect(deviceId, connectionName);
         Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org
-            .openroadm.device.OduConnection> otnXc = getOtnCrossConnect(deviceId, connectionName);
+                .openroadm.device.OduConnection> otnXc = getOtnCrossConnect(deviceId, connectionName);
         //Check if cross connect exists before delete
         if (xc.isPresent()) {
             interfList.add(xc.get().getSource().getSrcIf());
@@ -152,11 +147,11 @@ public class CrossConnectImpl221 {
             if (deviceTxOpt.isPresent()) {
                 deviceTx = deviceTxOpt.get();
             } else {
-                LOG.error("Device transaction for device {} was not found!", deviceId);
+                LOG.error(DEV_TRANSACTION_NOT_FOUND, deviceId);
                 return null;
             }
         } catch (InterruptedException | ExecutionException e) {
-            LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
+            LOG.error(UNABLE_DEV_TRANSACTION, deviceId, e);
             return null;
         }
 
@@ -180,7 +175,7 @@ public class CrossConnectImpl221 {
 
 
     public List<Ports> getConnectionPortTrail(String nodeId, String srcTp, String destTp,
-            int lowerSpectralSlotNumber, int higherSpectralSlotNumber)
+                                              int lowerSpectralSlotNumber, int higherSpectralSlotNumber)
             throws OpenRoadmInterfaceException {
         String spectralSlotName = String.join(GridConstant.SPECTRAL_SLOT_SEPARATOR,
                 String.valueOf(lowerSpectralSlotNumber),
@@ -256,9 +251,9 @@ public class CrossConnectImpl221 {
             // post the cross connect on the device
             InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
                     .child(RoadmConnections.class, new RoadmConnectionsKey(ctName));
-            deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
+            deviceTx.merge(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
             FluentFuture<? extends @NonNull CommitInfo> commit =
-                deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
+                    deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
             try {
                 commit.get();
                 LOG.info("Roadm connection power level successfully set ");
@@ -279,10 +274,10 @@ public class CrossConnectImpl221 {
     }
 
     private InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
-        .container.org.openroadm.device.OduConnection> generateOduConnectionIID(String connectionNumber) {
+            .container.org.openroadm.device.OduConnection> generateOduConnectionIID(String connectionNumber) {
         return InstanceIdentifier.create(OrgOpenroadmDevice.class).child(org.opendaylight.yang.gen.v1.http.org
-            .openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnection.class,
-            new OduConnectionKey(connectionNumber));
+                    .openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnection.class,
+                new OduConnectionKey(connectionNumber));
     }
 
     private String generateConnectionName(String srcTp, String destTp, String spectralSlotName) {
@@ -294,20 +289,20 @@ public class CrossConnectImpl221 {
         String srcTp = createdOduInterfaces.get(0);
         String dstTp = createdOduInterfaces.get(1);
         OduConnectionBuilder oduConnectionBuilder = new OduConnectionBuilder()
-            .setConnectionName(srcTp + "-x-" + dstTp)
-            .setDestination(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.odu.connection
-                .DestinationBuilder().setDstIf(dstTp).build())
-            .setSource(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.odu.connection
-                .SourceBuilder().setSrcIf(srcTp).build())
-            .setDirection(Direction.Bidirectional);
+                .setConnectionName(srcTp + "-x-" + dstTp)
+                .setDestination(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.odu.connection
+                        .DestinationBuilder().setDstIf(dstTp).build())
+                .setSource(new org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.odu.connection
+                        .SourceBuilder().setSrcIf(srcTp).build())
+                .setDirection(Direction.Bidirectional);
 
         InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
                 .container.org.openroadm.device.OduConnection> oduConnectionIID =
-            InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                .child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
-                        .container.org.openroadm.device.OduConnection.class,
-                    new OduConnectionKey(oduConnectionBuilder.getConnectionName())
-                );
+                InstanceIdentifier.create(OrgOpenroadmDevice.class)
+                        .child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
+                                        .container.org.openroadm.device.OduConnection.class,
+                                new OduConnectionKey(oduConnectionBuilder.getConnectionName())
+                        );
 
         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
         DeviceTransaction deviceTx;
@@ -316,11 +311,11 @@ public class CrossConnectImpl221 {
             if (deviceTxOpt.isPresent()) {
                 deviceTx = deviceTxOpt.get();
             } else {
-                LOG.error("Device transaction for device {} was not found!", deviceId);
+                LOG.error(DEV_TRANSACTION_NOT_FOUND, deviceId);
                 return Optional.empty();
             }
         } catch (InterruptedException | ExecutionException e) {
-            LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
+            LOG.error(UNABLE_DEV_TRANSACTION, deviceId, e);
             return Optional.empty();
         }
 
@@ -337,4 +332,4 @@ public class CrossConnectImpl221 {
         }
         return Optional.empty();
     }
-}
+}
\ No newline at end of file