Refactor common CrossConnectImpl
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / crossconnect / CrossConnectImpl121.java
index c84b75aad2054cb8587f45a862f67b1cbad6ee59..7b2ee2a1c231a8107ab9601f4ddd679297455027 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,19 +16,21 @@ 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;
 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.rev161014.OpticalControlMode;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrail;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailInputBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.GetConnectionPortTrailOutput;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceService;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceData;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.DestinationBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.SourceBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.get.connection.port.trail.output.Ports;
@@ -38,8 +39,8 @@ import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.open
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Decimal64;
 import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -58,62 +59,57 @@ public class CrossConnectImpl121 {
                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
     }
 
-    public Optional<String> postCrossConnect(String deviceId, Long waveNumber, String srcTp, String destTp,
-            int lowerSpectralSlotNumber, int higherSpectralSlotNumber) {
-        String spectralSlotName = String.join(GridConstant.SPECTRAL_SLOT_SEPARATOR,
-                String.valueOf(lowerSpectralSlotNumber),
-                String.valueOf(higherSpectralSlotNumber));
-        RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder();
-        String connectionNumber = generateConnectionNumber(srcTp, destTp, spectralSlotName);
-        rdmConnBldr.setConnectionNumber(connectionNumber);
-        rdmConnBldr.setWavelengthNumber(Uint32.valueOf(waveNumber));
-        rdmConnBldr.setOpticalControlMode(OpticalControlMode.Off);
-        rdmConnBldr.setSource(new SourceBuilder().setSrcIf(String.join(GridConstant.NAME_PARAMETERS_SEPARATOR,
-                srcTp, spectralSlotName)).build());
-        rdmConnBldr.setDestination(new DestinationBuilder()
-                .setDstIf(String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, destTp, spectralSlotName))
-                .build());
-        InstanceIdentifier<RoadmConnections> rdmConnectionIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                .child(RoadmConnections.class, new RoadmConnectionsKey(rdmConnBldr.getConnectionNumber()));
-
+    public Optional<String> postCrossConnect(
+            String deviceId, String srcTp, String destTp, SpectrumInformation spectrumInformation) {
         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
         DeviceTransaction deviceTx;
         try {
             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
-            if (deviceTxOpt.isPresent()) {
-                deviceTx = deviceTxOpt.get();
-            } else {
+            if (deviceTxOpt.isEmpty()) {
                 LOG.error("Device transaction for device {} was not found!", deviceId);
                 return Optional.empty();
             }
+            deviceTx = deviceTxOpt.orElseThrow();
         } catch (InterruptedException | ExecutionException e) {
             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
             return Optional.empty();
         }
-
+        String connectionNumber = spectrumInformation.getIdentifierFromParams(srcTp, destTp);
+        RoadmConnections rdmConn = new RoadmConnectionsBuilder()
+            .setConnectionNumber(connectionNumber)
+            .setWavelengthNumber(spectrumInformation.getWaveLength())
+            .setOpticalControlMode(OpticalControlMode.Off)
+            .setSource(
+                new SourceBuilder().setSrcIf(spectrumInformation.getIdentifierFromParams(srcTp)).build())
+            .setDestination(
+                new DestinationBuilder().setDstIf(spectrumInformation.getIdentifierFromParams(destTp)).build())
+            .build();
         // post the cross connect on the device
-        deviceTx.put(LogicalDatastoreType.CONFIGURATION, rdmConnectionIID, rdmConnBldr.build());
+        deviceTx.merge(
+            LogicalDatastoreType.CONFIGURATION,
+            InstanceIdentifier
+                .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
+                .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber))
+                .build(),
+            rdmConn);
         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);
+                    spectrumInformation.getLowerSpectralSlotNumber(),
+                    spectrumInformation.getHigherSpectralSlotNumber());
             return Optional.of(connectionNumber);
         } catch (InterruptedException | ExecutionException e) {
-            LOG.warn("Failed to post {}. Exception: ", rdmConnBldr.build(), e);
+            LOG.warn("Failed to post {}. Exception: ", rdmConn, e);
         }
         return Optional.empty();
     }
 
     public List<String> deleteCrossConnect(String deviceId, String connectionNumber) {
-        List<String> interfList = new ArrayList<>();
         Optional<RoadmConnections> xc = getCrossConnect(deviceId, connectionNumber);
         //Check if cross connect exists before delete
-        if (xc.isPresent()) {
-            interfList.add(xc.get().getSource().getSrcIf());
-            interfList.add(xc.get().getDestination().getDstIf());
-        } else {
+        if (xc.isEmpty()) {
             LOG.warn("Cross connect does not exist, halting delete");
             return null;
         }
@@ -121,17 +117,15 @@ public class CrossConnectImpl121 {
         DeviceTransaction deviceTx;
         try {
             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
-            if (deviceTxOpt.isPresent()) {
-                deviceTx = deviceTxOpt.get();
-            } else {
+            if (deviceTxOpt.isEmpty()) {
                 LOG.error("Device transaction for device {} was not found!", deviceId);
                 return null;
             }
+            deviceTx = deviceTxOpt.orElseThrow();
         } catch (InterruptedException | ExecutionException e) {
             LOG.error("Unable to obtain device transaction for device {}!", deviceId, e);
             return null;
         }
-
         // post the cross connect on the device
         deviceTx.delete(LogicalDatastoreType.CONFIGURATION, generateRdmConnectionIID(connectionNumber));
         FluentFuture<? extends @NonNull CommitInfo> commit =
@@ -139,7 +133,9 @@ public class CrossConnectImpl121 {
         try {
             commit.get();
             LOG.info("Roadm connection successfully deleted ");
-            return interfList;
+            return new ArrayList<>(List.of(
+                xc.orElseThrow().getSource().getSrcIf(),
+                xc.orElseThrow().getDestination().getDstIf()));
         } catch (InterruptedException | ExecutionException e) {
             LOG.warn("Failed to delete {}", connectionNumber, e);
         }
@@ -149,103 +145,100 @@ public class CrossConnectImpl121 {
 
     public List<Ports> getConnectionPortTrail(String nodeId, String srcTp, String destTp,
             int lowerSpectralSlotNumber, int higherSpectralSlotNumber) throws OpenRoadmInterfaceException {
-        String spectralSlotName = String.join(GridConstant.SPECTRAL_SLOT_SEPARATOR,
-                String.valueOf(lowerSpectralSlotNumber), String.valueOf(higherSpectralSlotNumber));
-        String connectionName = generateConnectionNumber(srcTp, destTp, spectralSlotName);
         Optional<MountPoint> mountPointOpt = deviceTransactionManager.getDeviceMountPoint(nodeId);
-        List<Ports> ports = null;
-        MountPoint mountPoint;
-        if (mountPointOpt.isPresent()) {
-            mountPoint = mountPointOpt.get();
-        } else {
+        if (mountPointOpt.isEmpty()) {
             LOG.error("Failed to obtain mount point for device {}!", nodeId);
             return Collections.emptyList();
         }
-        final Optional<RpcConsumerRegistry> service = mountPoint.getService(RpcConsumerRegistry.class);
-        if (!service.isPresent()) {
+        String connectionName = generateConnectionNumber(srcTp, destTp,
+            String.join(GridConstant.SPECTRAL_SLOT_SEPARATOR,
+                String.valueOf(lowerSpectralSlotNumber), String.valueOf(higherSpectralSlotNumber)));
+        MountPoint mountPoint = mountPointOpt.orElseThrow();
+        final Optional<RpcService> service = mountPoint.getService(RpcService.class);
+        if (service.isEmpty()) {
             LOG.error("Failed to get RpcService for node {}", nodeId);
         }
-        final OrgOpenroadmDeviceService rpcService = service.get().getRpcService(OrgOpenroadmDeviceService.class);
-        final GetConnectionPortTrailInputBuilder portTrainInputBuilder = new GetConnectionPortTrailInputBuilder();
-        portTrainInputBuilder.setConnectionNumber(connectionName);
-        final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.getConnectionPortTrail(
-                portTrainInputBuilder.build());
-        if (portTrailOutput != null) {
-            try {
-                RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
-                GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
-                if (connectionPortTrailOutput == null) {
-                    throw new OpenRoadmInterfaceException(String.format("RPC get connection port trail called on"
-                            + " node %s returned null!", nodeId));
-                }
-                LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
-                ports = connectionPortTrailOutput.getPorts();
-                for (Ports port : ports) {
-                    LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
-                }
-            } catch (InterruptedException | ExecutionException e) {
-                LOG.warn("Exception caught", e);
-            }
-        } else {
+        final GetConnectionPortTrail rpcService = service.orElseThrow().getRpc(GetConnectionPortTrail.class);
+        final Future<RpcResult<GetConnectionPortTrailOutput>> portTrailOutput = rpcService.invoke(
+                new GetConnectionPortTrailInputBuilder().setConnectionNumber(connectionName).build());
+        if (portTrailOutput == null) {
             LOG.warn("Port trail is null in getConnectionPortTrail for nodeId {}", nodeId);
+            return Collections.emptyList();
+        }
+        List<Ports> ports = null;
+        try {
+            RpcResult<GetConnectionPortTrailOutput> connectionPortTrailOutputRpcResult = portTrailOutput.get();
+            GetConnectionPortTrailOutput connectionPortTrailOutput = connectionPortTrailOutputRpcResult.getResult();
+            if (connectionPortTrailOutput == null) {
+                throw new OpenRoadmInterfaceException(String.format(
+                    "RPC get connection port trail called on node %s returned null!", nodeId));
+            }
+            LOG.info("Getting port trail for node {}'s connection number {}", nodeId, connectionName);
+            ports = connectionPortTrailOutput.getPorts();
+            for (Ports port : ports) {
+                LOG.info("{} - Circuit pack {} - Port {}", nodeId, port.getCircuitPackName(), port.getPortName());
+            }
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.warn("Exception caught", e);
         }
         return ports != null ? ports : Collections.emptyList();
     }
 
     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 String generateConnectionNumber(String srcTp, String destTp, String spectralSlotName) {
         return String.join(GridConstant.NAME_PARAMETERS_SEPARATOR, srcTp, destTp, spectralSlotName);
     }
 
-    public boolean setPowerLevel(String deviceId, OpticalControlMode mode, BigDecimal powerValue, String ctNumber) {
-
+    public boolean setPowerLevel(String deviceId, OpticalControlMode mode, Decimal64 powerValue, String ctNumber) {
         Optional<RoadmConnections> rdmConnOpt = getCrossConnect(deviceId, ctNumber);
-        if (rdmConnOpt.isPresent()) {
-            RoadmConnectionsBuilder rdmConnBldr = new RoadmConnectionsBuilder(rdmConnOpt.get())
-                    .setOpticalControlMode(mode);
-            if (powerValue != null) {
-                rdmConnBldr.setTargetOutputPower(new PowerDBm(powerValue));
-            }
-            RoadmConnections newRdmConn = rdmConnBldr.build();
-
-            Future<Optional<DeviceTransaction>> deviceTxFuture =
-                    deviceTransactionManager.getDeviceTransaction(deviceId);
-            DeviceTransaction deviceTx;
-            try {
-                Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
-                if (deviceTxOpt.isPresent()) {
-                    deviceTx = deviceTxOpt.get();
-                } else {
-                    LOG.error("Transaction for device {} was not found!", deviceId);
-                    return false;
-                }
-            } catch (InterruptedException | ExecutionException e) {
-                LOG.error("Unable to get transaction for device {}!", deviceId, e);
+        if (rdmConnOpt.isEmpty()) {
+            LOG.warn("Roadm-Connection is null in set power level ({})", ctNumber);
+            return false;
+        }
+        RoadmConnections newRdmConn =
+            powerValue == null
+                ? new RoadmConnectionsBuilder(rdmConnOpt.orElseThrow())
+                    .setOpticalControlMode(mode)
+                    .build()
+                : new RoadmConnectionsBuilder(rdmConnOpt.orElseThrow())
+                    .setOpticalControlMode(mode)
+                    .setTargetOutputPower(new PowerDBm(powerValue))
+                    .build();
+        Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(deviceId);
+        DeviceTransaction deviceTx;
+        try {
+            Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
+            if (deviceTxOpt.isEmpty()) {
+                LOG.error("Transaction for device {} was not found!", deviceId);
                 return false;
             }
-
-            // post the cross connect on the device
-            InstanceIdentifier<RoadmConnections> roadmConnIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                    .child(RoadmConnections.class, new RoadmConnectionsKey(ctNumber));
-            deviceTx.put(LogicalDatastoreType.CONFIGURATION, roadmConnIID, newRdmConn);
-            FluentFuture<? extends @NonNull CommitInfo> commit =
-                deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
-            try {
-                commit.get();
-                LOG.info("Roadm connection power level successfully set ");
-                return true;
-            } catch (InterruptedException | ExecutionException ex) {
-                LOG.warn("Failed to post {}", newRdmConn, ex);
-            }
-
-        } else {
-            LOG.warn("Roadm-Connection is null in set power level ({})", ctNumber);
+            deviceTx = deviceTxOpt.orElseThrow();
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.error("Unable to get transaction for device {}!", deviceId, e);
+            return false;
+        }
+        // post the cross connect on the device
+        deviceTx.merge(
+            LogicalDatastoreType.CONFIGURATION,
+            InstanceIdentifier
+                .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
+                .child(RoadmConnections.class, new RoadmConnectionsKey(ctNumber))
+                .build(),
+            newRdmConn);
+        FluentFuture<? extends @NonNull CommitInfo> commit =
+            deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
+        try {
+            commit.get();
+            LOG.info("Roadm connection power level successfully set ");
+            return true;
+        } catch (InterruptedException | ExecutionException ex) {
+            LOG.warn("Failed to post {}", newRdmConn, ex);
         }
         return false;
     }
-
 }