Bump upstream dependencies to Ca
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / crossconnect / CrossConnectImpl221.java
index 8941e829ebc3a6c6201b1716dacadc348cacfaa4..86f1a9ece121271633f609d3db0679b26f657b15 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.transportpce.common.crossconnect;
 
 import com.google.common.util.concurrent.FluentFuture;
-import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -17,7 +16,7 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.api.MountPoint;
-import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
+import org.opendaylight.mdsal.binding.api.RpcService;
 import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.Timeouts;
@@ -28,21 +27,24 @@ 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;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.GetConnectionPortTrail;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.GetConnectionPortTrailInputBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.GetConnectionPortTrailOutput;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OduConnection.Direction;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OrgOpenroadmDeviceService;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OrgOpenroadmDeviceData;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.connection.DestinationBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.connection.SourceBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.get.connection.port.trail.output.Ports;
 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.org.openroadm.device.container.org.openroadm.device.OduConnection;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnectionBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnectionKey;
 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.rev220926.otn.renderer.nodes.Nodes;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Decimal64;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -50,6 +52,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) {
@@ -64,7 +68,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,
@@ -72,7 +76,7 @@ public class CrossConnectImpl221 {
     }
 
     public Optional<String> postCrossConnect(String deviceId, String srcTp, String destTp,
-            SpectrumInformation spectrumInformation) {
+                                             SpectrumInformation spectrumInformation) {
         String connectionNumber = spectrumInformation.getIdentifierFromParams(srcTp, destTp);
         RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder()
                 .setConnectionName(connectionNumber)
@@ -84,22 +88,23 @@ public class CrossConnectImpl221 {
                         .setDstIf(spectrumInformation.getIdentifierFromParams(destTp,"nmc"))
                         .build());
 
-        InstanceIdentifier<RoadmConnections> rdmConnectionIID =
-                InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                    .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionName()));
+        InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier
+            .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
+            .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionName()))
+            .build();
 
         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
         DeviceTransaction deviceTx;
         try {
             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
             if (deviceTxOpt.isPresent()) {
-                deviceTx = deviceTxOpt.get();
+                deviceTx = deviceTxOpt.orElseThrow();
             } 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();
         }
 
@@ -124,16 +129,16 @@ 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());
-            interfList.add(xc.get().getDestination().getDstIf());
-            interfList.add(xc.get().getSource().getSrcIf().replace("nmc", "mc"));
-            interfList.add(xc.get().getDestination().getDstIf().replace("nmc", "mc"));
+            interfList.add(xc.orElseThrow().getSource().getSrcIf());
+            interfList.add(xc.orElseThrow().getDestination().getDstIf());
+            interfList.add(xc.orElseThrow().getSource().getSrcIf().replace("nmc", "mc"));
+            interfList.add(xc.orElseThrow().getDestination().getDstIf().replace("nmc", "mc"));
         } else if (otnXc.isPresent()) {
-            interfList.add(otnXc.get().getSource().getSrcIf());
-            interfList.add(otnXc.get().getDestination().getDstIf());
+            interfList.add(otnXc.orElseThrow().getSource().getSrcIf());
+            interfList.add(otnXc.orElseThrow().getDestination().getDstIf());
         } else {
             LOG.warn("Cross connect {} does not exist, halting delete", connectionName);
             return null;
@@ -143,13 +148,13 @@ public class CrossConnectImpl221 {
         try {
             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
             if (deviceTxOpt.isPresent()) {
-                deviceTx = deviceTxOpt.get();
+                deviceTx = deviceTxOpt.orElseThrow();
             } 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;
         }
 
@@ -173,7 +178,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),
@@ -183,20 +188,20 @@ public class CrossConnectImpl221 {
         List<Ports> ports = null;
         MountPoint mountPoint;
         if (mountPointOpt.isPresent()) {
-            mountPoint = mountPointOpt.get();
+            mountPoint = mountPointOpt.orElseThrow();
         } else {
             LOG.error("Failed to obtain mount point for device {}!", nodeId);
             return Collections.emptyList();
         }
-        final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
+        final Optional<RpcService> service = mountPoint.getService(RpcService.class);
         if (!service.isPresent()) {
             LOG.error("Failed to get RpcService for node {}", nodeId);
         }
-        final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
-        final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
-        portTrainInputBuilder.setConnectionName(connectionName);
-        final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
-                portTrainInputBuilder.build());
+        GetConnectionPortTrail rpcService = service.orElseThrow().getRpc(GetConnectionPortTrail.class);
+        final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.invoke(
+                new GetConnectionPortTrailInputBuilder()
+                    .setConnectionName(connectionName)
+                    .build());
         if (portTrailOutput != null) {
             try {
                 RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
@@ -220,10 +225,10 @@ public class CrossConnectImpl221 {
     }
 
 
-    public boolean setPowerLevel(String deviceId, OpticalControlMode mode, BigDecimal powerValue, String ctName) {
+    public boolean setPowerLevel(String deviceId, OpticalControlMode mode, Decimal64 powerValue, String ctName) {
         Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, ctName);
         if (rdmConnOpt.isPresent()) {
-            RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get());
+            RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.orElseThrow());
             rdmConnBldr.setOpticalControlMode(mode);
             if (powerValue != null) {
                 rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
@@ -236,7 +241,7 @@ public class CrossConnectImpl221 {
             try {
                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
                 if (deviceTxOpt.isPresent()) {
-                    deviceTx = deviceTxOpt.get();
+                    deviceTx = deviceTxOpt.orElseThrow();
                 } else {
                     LOG.error("Transaction for device {} was not found!", deviceId);
                     return false;
@@ -247,11 +252,13 @@ public class CrossConnectImpl221 {
             }
 
             // post the cross connect on the device
-            InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                    .child(RoadmConnections.class, new RoadmConnectionsKey(ctName));
+            InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier
+                .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
+                .child(RoadmConnections.class, new RoadmConnectionsKey(ctName))
+                .build();
             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 ");
@@ -267,15 +274,17 @@ public class CrossConnectImpl221 {
     }
 
     private InstanceIdentifier<RoadmConnections> generateRdmConnectionIID(String connectionNumber) {
-        return InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
+        return InstanceIdentifier
+            .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
+            .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber))
+            .build();
     }
 
-    private InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
-        .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));
+    private InstanceIdentifier<OduConnection> generateOduConnectionIID(String connectionNumber) {
+        return InstanceIdentifier
+            .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
+            .child(OduConnection.class, new OduConnectionKey(connectionNumber))
+            .build();
     }
 
     private String generateConnectionName(String srcTp, String destTp, String spectralSlotName) {
@@ -284,36 +293,37 @@ public class CrossConnectImpl221 {
 
     public Optional<String> postOtnCrossConnect(List<String> createdOduInterfaces, Nodes node) {
         String deviceId = node.getNodeId();
-        String srcTp = createdOduInterfaces.get(0);
-        String dstTp = createdOduInterfaces.get(1);
+        List<String> sortedCreatedOduInterfaces = new ArrayList<>(createdOduInterfaces);
+        sortedCreatedOduInterfaces.sort((s1,s2) -> s1.compareTo(s2));
+        String srcTp = sortedCreatedOduInterfaces.get(0);
+        String dstTp = sortedCreatedOduInterfaces.get(1);
+        // Strip the service name from the src and dst
+        String oduXConnectionName = srcTp.split(":")[0] + "-x-" + dstTp.split(":")[0];
         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(oduXConnectionName)
+                .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<OduConnection> oduConnectionIID = InstanceIdentifier
+            .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
+            .child(OduConnection.class, new OduConnectionKey(oduConnectionBuilder.getConnectionName()))
+            .build();
 
         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
         DeviceTransaction deviceTx;
         try {
             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
             if (deviceTxOpt.isPresent()) {
-                deviceTx = deviceTxOpt.get();
+                deviceTx = deviceTxOpt.orElseThrow();
             } 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();
         }
 
@@ -323,11 +333,11 @@ public class CrossConnectImpl221 {
                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
         try {
             commit.get();
-            LOG.info("Otn-connection successfully created: {}-{}", srcTp, dstTp);
-            return Optional.of(srcTp + "-x-" + dstTp);
+            LOG.info("Otn-connection successfully created: {}", oduXConnectionName);
+            return Optional.of(oduXConnectionName);
         } catch (InterruptedException | ExecutionException e) {
             LOG.warn("Failed to post {}.", oduConnectionBuilder.build(), e);
         }
         return Optional.empty();
     }
-}
+}
\ No newline at end of file