Migrate Optional.get() callers
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / openroadminterface / OpenRoadmInterface121.java
index d8d8549da743e8c2dbd6ec09346254ab9b601e30..14bcabb0bf6e54482b8ab54d864508824bb59ea4 100644 (file)
@@ -8,7 +8,6 @@
 
 package org.opendaylight.transportpce.renderer.openroadminterface;
 
-import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Optional;
@@ -21,8 +20,9 @@ import org.opendaylight.transportpce.common.fixedflex.SpectrumInformation;
 import org.opendaylight.transportpce.common.mapping.PortMapping;
 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mapping.Mapping;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.mapping.Mapping;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceData;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
@@ -40,6 +40,7 @@ import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.Opti
 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOdu;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOtu;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.OchAttributes;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.OchAttributes.ModulationFormat;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.R100G;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.och.container.OchBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces.rev161014.OtsAttributes;
@@ -52,14 +53,13 @@ import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161
 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.OtuAttributes;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.otu.container.OtuBuilder;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Decimal64;
 import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 
 public class OpenRoadmInterface121 {
-    private static final String MAPPING_MSG_ERROR =
-            "Unable to get mapping from PortMapping for node % and logical connection port %s";
     private final PortMapping portMapping;
     private final OpenRoadmInterfaces openRoadmInterfaces;
     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterface121.class);
@@ -73,34 +73,32 @@ public class OpenRoadmInterface121 {
             throws OpenRoadmInterfaceException {
         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
         if (portMap == null) {
-            throw new OpenRoadmInterfaceException(String.format(MAPPING_MSG_ERROR, nodeId, logicalConnPoint));
+            throw new OpenRoadmInterfaceException(
+                OpenRoadmInterfaceException.mapping_msg_err(nodeId, logicalConnPoint));
         }
-
-        // Ethernet interface specific data
-        EthernetBuilder ethIfBuilder = new EthernetBuilder()
-                .setAutoNegotiation(EthAttributes.AutoNegotiation.Enabled)
-                .setDuplex(EthAttributes.Duplex.Full)
-                .setFec(EthAttributes.Fec.Off)
-                .setSpeed(Uint32.valueOf(100000))
-                .setMtu(Uint32.valueOf(9000));
-
-        InterfaceBuilder ethInterfaceBldr = createGenericInterfaceBuilder(portMap, EthernetCsmacd.class,
-                                                                          logicalConnPoint + "-ETHERNET");
-
-        // Create Interface1 type object required for adding as augmentation
-        Interface1Builder ethIf1Builder = new Interface1Builder();
-        ethInterfaceBldr.addAugmentation(ethIf1Builder.setEthernet(ethIfBuilder.build()).build());
-
+        InterfaceBuilder ethInterfaceBldr =
+            createGenericInterfaceBuilder(portMap, EthernetCsmacd.VALUE, logicalConnPoint + "-ETHERNET")
+                .addAugmentation(
+                    // Create Interface1 type object required for adding as augmentation
+                    new Interface1Builder()
+                        .setEthernet(
+                            // Ethernet interface specific data
+                            new EthernetBuilder()
+                                .setAutoNegotiation(EthAttributes.AutoNegotiation.Enabled)
+                                .setDuplex(EthAttributes.Duplex.Full)
+                                .setFec(EthAttributes.Fec.Off)
+                                .setSpeed(Uint32.valueOf(100000))
+                                .setMtu(Uint32.valueOf(9000))
+                                .build())
+                        .build());
         // Post interface on the device
         this.openRoadmInterfaces.postInterface(nodeId, ethInterfaceBldr);
-
         // Post the equipment-state change on the device circuit-pack
         this.openRoadmInterfaces.postEquipmentState(nodeId, portMap.getSupportingCircuitPackName(), true);
-
         return ethInterfaceBldr.getName();
     }
 
-    private InterfaceBuilder createGenericInterfaceBuilder(Mapping portMap, Class<? extends InterfaceType> type,
+    private InterfaceBuilder createGenericInterfaceBuilder(Mapping portMap, InterfaceType type,
                                                            String key) {
         return new InterfaceBuilder()
                 .setDescription("  TBD   ")
@@ -127,26 +125,25 @@ public class OpenRoadmInterface121 {
             throws OpenRoadmInterfaceException {
         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
         if (portMap == null) {
-            throw new OpenRoadmInterfaceException(String.format(MAPPING_MSG_ERROR, nodeId, logicalConnPoint));
+            throw new OpenRoadmInterfaceException(
+                OpenRoadmInterfaceException.mapping_msg_err(nodeId, logicalConnPoint));
         }
         // Create generic interface
-        InterfaceBuilder otuInterfaceBldr = createGenericInterfaceBuilder(portMap, OtnOtu.class, logicalConnPoint
-                + "-OTU");
-        otuInterfaceBldr.setSupportingInterface(supportOchInterface);
-
-        // OTU interface specific data
-        OtuBuilder otuIfBuilder = new OtuBuilder()
-                .setFec(OtuAttributes.Fec.Scfec)
-                .setRate(OTU4.class);
-
-        // Create Interface1 type object required for adding as augmentation
-        // TODO look at imports of different versions of class
-        org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder otuIf1Builder =
-                new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder();
-        otuInterfaceBldr.addAugmentation(otuIf1Builder.setOtu(otuIfBuilder.build()).build());
-
+        InterfaceBuilder otuInterfaceBldr =
+            createGenericInterfaceBuilder(portMap, OtnOtu.VALUE, logicalConnPoint + "-OTU")
+                .setSupportingInterface(supportOchInterface)
+                .addAugmentation(
+                    new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder()
+                        .setOtu(
+                            // OTU interface specific data
+                            new OtuBuilder()
+                                .setFec(OtuAttributes.Fec.Scfec)
+                                .setRate(OTU4.VALUE)
+                                .build())
+                        .build());
         // Post interface on the device
         this.openRoadmInterfaces.postInterface(nodeId, otuInterfaceBldr);
+        this.portMapping.updateMapping(nodeId, portMap);
         return otuInterfaceBldr.getName();
     }
 
@@ -155,38 +152,39 @@ public class OpenRoadmInterface121 {
      *
      * @param nodeId node ID
      * @param logicalConnPoint logical Connection Point
-     * @param supportingOtuInterface supporting OTU Interface
      *
      * @return Name of the interface if successful, otherwise return null.
      * @throws OpenRoadmInterfaceException OpenRoadmInterfaceException
      */
 
-    public String createOpenRoadmOdu4Interface(String nodeId, String logicalConnPoint, String supportingOtuInterface)
+    public String createOpenRoadmOdu4Interface(String nodeId, String logicalConnPoint)
             throws OpenRoadmInterfaceException {
         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
         if (portMap == null) {
-            throw new OpenRoadmInterfaceException(String.format(MAPPING_MSG_ERROR, nodeId, logicalConnPoint));
+            throw new OpenRoadmInterfaceException(
+                OpenRoadmInterfaceException.mapping_msg_err(nodeId, logicalConnPoint));
         }
-        InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(portMap, OtnOdu.class, logicalConnPoint
-                + "-ODU");
-        oduInterfaceBldr.setSupportingInterface(supportingOtuInterface);
-
-        // ODU interface specific data
-        // Set Opu attributes
-        OpuBuilder opuBldr = new OpuBuilder()
-                .setPayloadType("07")
-                .setExpPayloadType("07");
-        OduBuilder oduIfBuilder = new OduBuilder()
-                .setRate(ODU4.class)
-                .setMonitoringMode(OduAttributes.MonitoringMode.Terminated)
-                .setOpu(opuBldr.build());
-
-        // Create Interface1 type object required for adding as augmentation
-        // TODO look at imports of different versions of class
-        org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1Builder oduIf1Builder =
-                new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1Builder();
-        oduInterfaceBldr.addAugmentation(oduIf1Builder.setOdu(oduIfBuilder.build()).build());
-
+        InterfaceBuilder oduInterfaceBldr =
+            createGenericInterfaceBuilder(portMap, OtnOdu.VALUE, logicalConnPoint + "-ODU");
+        if (portMap.getSupportingOtu4() != null) {
+            oduInterfaceBldr.setSupportingInterface(portMap.getSupportingOtu4());
+        }
+        oduInterfaceBldr.addAugmentation(
+            // Create Interface1 type object required for adding as augmentation
+            new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1Builder()
+                .setOdu(
+                    // ODU interface specific data
+                    new OduBuilder()
+                        .setRate(ODU4.VALUE)
+                        .setMonitoringMode(OduAttributes.MonitoringMode.Terminated)
+                        .setOpu(
+                            // Set Opu attributes
+                            new OpuBuilder()
+                                .setPayloadType("07")
+                                .setExpPayloadType("07")
+                                .build())
+                        .build())
+                .build());
         // Post interface on the device
         this.openRoadmInterfaces.postInterface(nodeId, oduInterfaceBldr);
         return oduInterfaceBldr.getName();
@@ -205,72 +203,65 @@ public class OpenRoadmInterface121 {
 
     public List<String> createOpenRoadmOchInterfaces(String nodeId, String logicalConnPoint,
             SpectrumInformation spectrumInformation)
-        throws OpenRoadmInterfaceException {
+            throws OpenRoadmInterfaceException {
         Mapping portMap = portMapping.getMapping(nodeId, logicalConnPoint);
         if (portMap == null) {
-            throw new OpenRoadmInterfaceException(String.format(
-                "Unable to get mapping from PortMapping for node %s and logical connection port %s",
-                    nodeId, logicalConnPoint));
+            throw new OpenRoadmInterfaceException(
+                OpenRoadmInterfaceException.mapping_msg_err(nodeId, logicalConnPoint));
         }
         // Create generic interface
-        InterfaceBuilder ochInterfaceBldr = createGenericInterfaceBuilder(portMap, OpticalChannel.class,
+        InterfaceBuilder ochInterfaceBldr =
+            createGenericInterfaceBuilder(
+                portMap, OpticalChannel.VALUE,
                 spectrumInformation.getIdentifierFromParams(logicalConnPoint));
-
-        // OCH interface specific data
-        OchBuilder ocIfBuilder = new OchBuilder().setWavelengthNumber(spectrumInformation.getWaveLength());
-
         // Add supporting OMS interface
         if (portMap.getSupportingOms() != null) {
             ochInterfaceBldr.setSupportingInterface(portMap.getSupportingOms());
         }
-        // Create Interface1 type object required for adding as augmentation
-        // TODO look at imports of different versions of class
-        org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.Interface1Builder
-                ochIf1Builder = new org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces
-                .rev161014.Interface1Builder();
-        ochInterfaceBldr.addAugmentation(ochIf1Builder.setOch(ocIfBuilder.build()).build());
-
-        List<String> interfacesCreated = new ArrayList<>();
+        ochInterfaceBldr.addAugmentation(
+            // Create Interface1 type object required for adding as augmentation
+            new org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.Interface1Builder()
+                .setOch(
+                    // OCH interface specific data
+                    new OchBuilder().setWavelengthNumber(spectrumInformation.getWaveLength()).build())
+                .build());
         // Post interface on the device
         openRoadmInterfaces.postInterface(nodeId, ochInterfaceBldr);
-        interfacesCreated.add(ochInterfaceBldr.getName());
-        return interfacesCreated;
+        return new ArrayList<String>(List.of(ochInterfaceBldr.getName()));
     }
 
     public String createOpenRoadmOchInterface(String nodeId, String logicalConnPoint,
             SpectrumInformation spectrumInformation) throws OpenRoadmInterfaceException {
         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
         if (portMap == null) {
-            throw new OpenRoadmInterfaceException(String.format(
-                "Unable to get mapping from PortMapping for node %s and logical connection port %s",
-                    nodeId, logicalConnPoint));
+            throw new OpenRoadmInterfaceException(
+                OpenRoadmInterfaceException.mapping_msg_err(nodeId, logicalConnPoint));
         }
-        OchAttributes.ModulationFormat modulationFormat = OchAttributes.ModulationFormat.DpQpsk;
-        Optional<OchAttributes.ModulationFormat> optionalModulationFormat = OchAttributes.ModulationFormat
-                .forName(spectrumInformation.getModulationFormat());
-        if (optionalModulationFormat.isPresent()) {
-            modulationFormat =  optionalModulationFormat.get();
+        ModulationFormat modulationFormat =
+            OchAttributes.ModulationFormat.forName(spectrumInformation.getModulationFormat());
+        if (modulationFormat == null) {
+            modulationFormat = OchAttributes.ModulationFormat.DpQpsk;
         }
-        // OCH interface specific data
-        OchBuilder ocIfBuilder = new OchBuilder()
-                .setWavelengthNumber(spectrumInformation.getWaveLength())
-                .setModulationFormat(modulationFormat)
-                .setRate(R100G.class)
-                .setTransmitPower(new PowerDBm(new BigDecimal("-5")));
-
-        // Create Interface1 type object required for adding as augmentation
-        // TODO look at imports of different versions of class
-        org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014
-                .Interface1Builder ochIf1Builder = new org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel
-                .interfaces.rev161014.Interface1Builder();
         // Create generic interface
-        InterfaceBuilder ochInterfaceBldr = createGenericInterfaceBuilder(portMap, OpticalChannel.class,
-                spectrumInformation.getIdentifierFromParams(logicalConnPoint));
-        ochInterfaceBldr.addAugmentation(ochIf1Builder.setOch(ocIfBuilder.build()).build());
-
+        InterfaceBuilder ochInterfaceBldr =
+            createGenericInterfaceBuilder(
+                    portMap, OpticalChannel.VALUE,
+                    spectrumInformation.getIdentifierFromParams(logicalConnPoint))
+                .addAugmentation(
+                    // Create Interface1 type object required for adding as augmentation
+                    new org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014
+                            .Interface1Builder()
+                        .setOch(
+                            // OCH interface specific data
+                            new OchBuilder()
+                                .setWavelengthNumber(spectrumInformation.getWaveLength())
+                                .setModulationFormat(modulationFormat)
+                                .setRate(R100G.VALUE)
+                                .setTransmitPower(new PowerDBm(Decimal64.valueOf("-5")))
+                                .build())
+                        .build());
         // Post interface on the device
         this.openRoadmInterfaces.postInterface(nodeId, ochInterfaceBldr);
-
         // Post the equipment-state change on the device circuit-pack if xpdr node
         if (portMap.getLogicalConnectionPoint().contains(StringConstants.NETWORK_TOKEN)) {
             this.openRoadmInterfaces.postEquipmentState(nodeId, portMap.getSupportingCircuitPackName(), true);
@@ -283,64 +274,66 @@ public class OpenRoadmInterface121 {
     }
 
     public String createOpenRoadmOmsInterface(String nodeId, Mapping mapping) throws OpenRoadmInterfaceException {
-        if (mapping.getSupportingOms() == null) {
-            // Create generic interface
-            InterfaceBuilder omsInterfaceBldr = createGenericInterfaceBuilder(mapping, OpenROADMOpticalMultiplex.class,
-                "OMS-" + mapping.getLogicalConnectionPoint());
-            if (mapping.getSupportingOts() != null) {
-                omsInterfaceBldr.setSupportingInterface(mapping.getSupportingOts());
-            } else {
-                LOG.error("Unable to get ots interface from mapping {} - {}", nodeId,
-                          mapping.getLogicalConnectionPoint());
-                return null;
-            }
-            this.openRoadmInterfaces.postInterface(nodeId, omsInterfaceBldr);
-            this.portMapping.updateMapping(nodeId, mapping);
-            return omsInterfaceBldr.build().getName();
-        } else {
+        if (mapping.getSupportingOms() != null) {
             return mapping.getSupportingOms();
         }
+        if (mapping.getSupportingOts() == null) {
+            LOG.error("Unable to get ots interface from mapping {} - {}", nodeId, mapping.getLogicalConnectionPoint());
+            return null;
+        }
+        // Create generic interface
+        InterfaceBuilder omsInterfaceBldr =
+            createGenericInterfaceBuilder(
+                     mapping, OpenROADMOpticalMultiplex.VALUE,
+                    "OMS-" + mapping.getLogicalConnectionPoint())
+                .setSupportingInterface(mapping.getSupportingOts());
+        this.openRoadmInterfaces.postInterface(nodeId, omsInterfaceBldr);
+        this.portMapping.updateMapping(nodeId, mapping);
+        return omsInterfaceBldr.build().getName();
     }
 
     public String createOpenRoadmOtsInterface(String nodeId, Mapping mapping) throws OpenRoadmInterfaceException {
-        if (mapping.getSupportingOts() == null) {
-            // Create generic interface
-            InterfaceBuilder otsInterfaceBldr = createGenericInterfaceBuilder(mapping, OpticalTransport.class, "OTS-"
-                    + mapping.getLogicalConnectionPoint());
-            // OTS interface augmentation specific data
-            OtsBuilder otsIfBuilder = new OtsBuilder();
-            otsIfBuilder.setFiberType(OtsAttributes.FiberType.Smf);
-
-            // Create Interface1 type object required for adding as
-            // augmentation
-            org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces.rev161014
-                    .Interface1Builder otsIf1Builder = new org.opendaylight.yang.gen.v1.http.org.openroadm
-                    .optical.transport.interfaces.rev161014.Interface1Builder();
-            otsInterfaceBldr.addAugmentation(otsIf1Builder.setOts(otsIfBuilder.build()).build());
-            this.openRoadmInterfaces.postInterface(nodeId, otsInterfaceBldr);
-            this.portMapping.updateMapping(nodeId, mapping);
-            return otsInterfaceBldr.build().getName();
-        } else {
+        if (mapping.getSupportingOts() != null) {
             return mapping.getSupportingOts();
         }
+        // Create generic interface
+        InterfaceBuilder otsInterfaceBldr =
+            createGenericInterfaceBuilder(mapping, OpticalTransport.VALUE, "OTS-" + mapping.getLogicalConnectionPoint())
+                .addAugmentation(
+                    // Create Interface1 type object required for adding as
+                    // augmentation
+                    new org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces.rev161014
+                            .Interface1Builder()
+                        .setOts(
+                            // OTS interface augmentation specific data
+                            new OtsBuilder().setFiberType(OtsAttributes.FiberType.Smf).build())
+                        .build());
+        this.openRoadmInterfaces.postInterface(nodeId, otsInterfaceBldr);
+        this.portMapping.updateMapping(nodeId, mapping);
+        return otsInterfaceBldr.build().getName();
     }
 
     public boolean isUsedByXc(String nodeId, String interfaceName, String xc,
-        DeviceTransactionManager deviceTransactionManager) {
-        InstanceIdentifier<RoadmConnections> xciid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-            .child(RoadmConnections.class, new RoadmConnectionsKey(xc));
+            DeviceTransactionManager deviceTransactionManager) {
         LOG.info("reading xc {} in node {}", xc, nodeId);
-        Optional<RoadmConnections> crossconnection = deviceTransactionManager.getDataFromDevice(nodeId,
-            LogicalDatastoreType.CONFIGURATION, xciid, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-        if (crossconnection.isPresent()) {
-            RoadmConnections rc = crossconnection.get();
-            LOG.info("xd {} found", xc);
-            if (rc.getSource().getSrcIf().equals(interfaceName)
-                || rc.getDestination().getDstIf().equals(interfaceName)) {
-                return true;
-            }
-        } else {
+        Optional<RoadmConnections> crossconnection = deviceTransactionManager.getDataFromDevice(
+            nodeId,
+            LogicalDatastoreType.CONFIGURATION,
+            InstanceIdentifier
+                .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
+                .child(RoadmConnections.class, new RoadmConnectionsKey(xc))
+                .build(),
+            Timeouts.DEVICE_READ_TIMEOUT,
+            Timeouts.DEVICE_READ_TIMEOUT_UNIT);
+        if (crossconnection.isEmpty()) {
             LOG.info("xd {} not found !", xc);
+            return false;
+        }
+        RoadmConnections rc = crossconnection.orElseThrow();
+        LOG.info("xd {} found", xc);
+        if (rc.getSource().getSrcIf().equals(interfaceName)
+                || rc.getDestination().getDstIf().equals(interfaceName)) {
+            return true;
         }
         return false;
     }