Use version 13.1.0 of openroadm-network models
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / fixedflex / GridUtils.java
index a3d13b22b918a223e9488b1612a1730f5c26c689..e6e89839e29754994c69322bc8470af6d107dd33 100644 (file)
@@ -9,15 +9,18 @@
 package org.opendaylight.transportpce.common.fixedflex;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev200529.FrequencyGHz;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev200529.FrequencyTHz;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathInput;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev230526.FrequencyGHz;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev230526.FrequencyTHz;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMaps;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsBuilder;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsKey;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.available.freq.map.AvailFreqMaps;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.available.freq.map.AvailFreqMapsBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.available.freq.map.AvailFreqMapsKey;
+import org.opendaylight.yangtools.yang.common.Decimal64;
 import org.opendaylight.yangtools.yang.common.Uint16;
 import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
@@ -42,8 +45,10 @@ public final class GridUtils {
         Arrays.fill(byteArray, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
         Map<AvailFreqMapsKey, AvailFreqMaps> waveMap = new HashMap<>();
         AvailFreqMaps availFreqMaps = new AvailFreqMapsBuilder().setMapName(GridConstant.C_BAND)
-                .setFreqMapGranularity(new FrequencyGHz(BigDecimal.valueOf(GridConstant.GRANULARITY)))
-                .setStartEdgeFreq(new FrequencyTHz(BigDecimal.valueOf(GridConstant.START_EDGE_FREQUENCY)))
+                .setFreqMapGranularity(
+                    new FrequencyGHz(Decimal64.valueOf(BigDecimal.valueOf(GridConstant.GRANULARITY))))
+                .setStartEdgeFreq(
+                    new FrequencyTHz(Decimal64.valueOf(BigDecimal.valueOf(GridConstant.START_EDGE_FREQUENCY))))
                 .setEffectiveBits(Uint16.valueOf(GridConstant.EFFECTIVE_BITS))
                 .setFreqMap(byteArray)
                 .build();
@@ -83,15 +88,16 @@ public final class GridUtils {
     /**
      * Get the bit index for the frequency.
      *
-     * @param frequency BigDecimal
+     * @param atozMinFrequency BigDecimal
      * @return the bit index of the frequency. Throw IllegalArgumentException if
      *         index not in range of 0 GridConstant.EFFECTIVE_BITS
      */
-    public static int getIndexFromFrequency(BigDecimal frequency) {
-        double nvalue = (frequency.doubleValue() - GridConstant.CENTRAL_FREQUENCY) * (1000 / GridConstant.GRANULARITY);
+    public static int getIndexFromFrequency(Decimal64 atozMinFrequency) {
+        double nvalue = (atozMinFrequency.doubleValue() - GridConstant.CENTRAL_FREQUENCY)
+            * (1000 / GridConstant.GRANULARITY);
         int index =  (int) Math.round(nvalue + 284);
         if (index < 0 || index > GridConstant.EFFECTIVE_BITS) {
-            throw new IllegalArgumentException("Frequency not in range " + frequency);
+            throw new IllegalArgumentException("Frequency not in range " + atozMinFrequency);
         }
         return index;
     }
@@ -100,14 +106,15 @@ public final class GridUtils {
      * Get the spectrum width for rate and modulation format.
      * @param rate Uint32
      * @param modulationFormat ModulationFormat
-     * @return spectrum width in GHz
+     * @return spectrum width in GHz compatible with models 10.1
      */
-    public static FrequencyGHz getWidthFromRateAndModulationFormat(Uint32 rate, ModulationFormat modulationFormat) {
+    public static FrequencyGHz getWidthFromRateAndModulationFormat(Uint32 rate,
+            ModulationFormat modulationFormat) {
         String width = GridConstant.FREQUENCY_WIDTH_TABLE.get(rate, modulationFormat);
         if (width == null) {
             LOG.warn("No width found for service rate {} and modulation format {}, set width to 40", rate,
                     modulationFormat);
-            width = "40";
+            width = String.valueOf(GridConstant.WIDTH_40);
         }
         return FrequencyGHz.getDefaultInstance(width);
     }
@@ -116,11 +123,117 @@ public final class GridUtils {
      * Get central frequency of spectrum.
      * @param minFrequency BigDecimal
      * @param maxFrequency BigDecimal
-     * @return central frequency in THz
+     * @return central frequency in THz compatible with models 10.1
      */
     public static FrequencyTHz getCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
-        return new FrequencyTHz(minFrequency.add(maxFrequency).divide(BigDecimal.valueOf(2)));
+        return new FrequencyTHz(Decimal64.valueOf(computeCentralFrequency(minFrequency, maxFrequency)));
+    }
+
+    /**
+     * Get central frequency of spectrum with precision.
+     * @param minFrequency BigDecimal
+     * @param maxFrequency BigDecimal
+     * @param precision int
+     * @return central frequency in THz with precision
+     */
+    public static org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz
+        getCentralFrequencyWithPrecision(BigDecimal minFrequency,
+            BigDecimal maxFrequency, int precision) {
+        return new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz(
+                Decimal64.valueOf(computeCentralFrequency(minFrequency, maxFrequency)
+                    .setScale(precision, RoundingMode.HALF_EVEN)));
+
+    }
+
+    /**
+     * Compute central frequency from min and max frequency.
+     * @param minFrequency BigDecimal
+     * @param maxFrequency BigDecimal
+     * @return central frequency
+     */
+    private static BigDecimal computeCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
+        return minFrequency.add(maxFrequency).divide(BigDecimal.valueOf(2));
+    }
+
+    /**
+     * Get the lower spectral index for the frequency.
+     * @param frequency BigDecimal
+     * @return the lower spectral index
+     */
+    public static int getLowerSpectralIndexFromFrequency(Decimal64 frequency) {
+        return getIndexFromFrequency(frequency) + 1;
+    }
+
+    /**
+     * Get the higher spectral index for the frequency.
+     * @param frequency BigDecimal
+     * @return the lower spectral index
+     */
+    public static int getHigherSpectralIndexFromFrequency(Decimal64 frequency) {
+        return getIndexFromFrequency(frequency);
+    }
 
+    /**
+     * Create spectrum information from service path input.
+     * @param input ServicePathInput
+     * @return SpectrumInformation
+     */
+    public static SpectrumInformation initSpectrumInformationFromServicePathInput(ServicePathInput input) {
+        if (input.getLowerSpectralSlotNumber() == null || input.getHigherSpectralSlotNumber() == null) {
+            LOG.error("low and higher spectral slot numbers cannot be null");
+            throw new IllegalArgumentException("low and higher spectral slot numbers cannot be null");
+        }
+        SpectrumInformation spectrumInformation = new SpectrumInformation();
+        spectrumInformation.setLowerSpectralSlotNumber(input.getLowerSpectralSlotNumber().intValue());
+        spectrumInformation.setHigherSpectralSlotNumber(input.getHigherSpectralSlotNumber().intValue());
+        if (input.getWaveNumber() != null) {
+            spectrumInformation.setWaveLength(input.getWaveNumber());
+        }
+        if (input.getMinFreq() != null) {
+            spectrumInformation.setMinFrequency(input.getMinFreq().getValue().decimalValue());
+        } else {
+            spectrumInformation.setMinFrequency(
+                    GridUtils.getStartFrequencyFromIndex(input.getLowerSpectralSlotNumber().intValue() - 1));
+        }
+        if (input.getMaxFreq() != null) {
+            spectrumInformation.setMaxFrequency(input.getMaxFreq().getValue().decimalValue());
+        } else {
+            spectrumInformation.setMaxFrequency(
+                    GridUtils.getStopFrequencyFromIndex(input.getHigherSpectralSlotNumber().intValue() - 1));
+        }
+        if (input.getCenterFreq() != null) {
+            spectrumInformation.setCenterFrequency(input.getCenterFreq().getValue().decimalValue());
+        } else {
+            spectrumInformation.setCenterFrequency(GridUtils.getCentralFrequency(
+                    spectrumInformation.getMinFrequency(),
+                    spectrumInformation.getMaxFrequency()).getValue().decimalValue());
+        }
+        if (input.getNmcWidth() != null) {
+            spectrumInformation.setWidth(input.getNmcWidth().getValue().decimalValue()
+                    .setScale(0, RoundingMode.CEILING));
+        }
+        if (input.getModulationFormat() != null) {
+            spectrumInformation.setModulationFormat(input.getModulationFormat());
+        }
+        return spectrumInformation;
+    }
+
+    /**
+     * Get the N value of range -284 +484 from frequency index array.
+     * @param frequencyIndex the frequency index f range 0 768.
+     * @return the N value
+     */
+    public static int getNFromFrequencyIndex(int frequencyIndex) {
+        return frequencyIndex - 284;
+    }
+
+    /**
+     * Convert the power from dBm to Watt.
+     * @param  dbm power in dBm.
+     * @return outputpower in Watt.
+     */
+    public static BigDecimal convertDbmW(double dbm) {
+        return BigDecimal.valueOf(Math.pow(10, (dbm - 30) / 10));
     }
 
 }