Update connection and interface name
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / fixedflex / GridUtils.java
index 9c150f24520669991aa22d34731aa85729b0f5cd..34695056e5d00f6d9195e092d87e6799ee1400a3 100644 (file)
@@ -9,21 +9,31 @@
 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.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.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Util class for grid.
+ * Thoses methods are used for pce spectrum assignment and topology update.
+ * They use maximal precision of BigDecimal
+ * For device configuration which needs precision (4 digits), dedicated methods are
+ * located in FixedFlex and FrexGrid classes.
  *
  */
 public final class GridUtils {
+    private static final Logger LOG = LoggerFactory.getLogger(GridUtils.class);
 
     private GridUtils() {
     }
@@ -42,4 +52,119 @@ public final class GridUtils {
         return waveMap;
     }
 
+    /**
+     * Compute the wavelength index from Spectrum assignment begin index.
+     * Only for fix grid and device 1.2.1.
+     * @param index int
+     * @return the wavelength number.
+     */
+    public static long getWaveLengthIndexFromSpectrumAssigment(int index) {
+        return (GridConstant.EFFECTIVE_BITS - index) / GridConstant.NB_SLOTS_100G;
+    }
+
+    /**
+     * Compute the start frequency in TGz for the given index.
+     * @param index int
+     * @return the start frequency in THz for the provided index.
+     */
+    public static BigDecimal getStartFrequencyFromIndex(int index) {
+        int nvalue = index - 284;
+        return BigDecimal.valueOf(GridConstant.CENTRAL_FREQUENCY + (nvalue * GridConstant.GRANULARITY / 1000));
+    }
+
+    /**
+     * Compute the stop frequency in TGz for the given index.
+     * @param index int
+     * @return the stop frequency in THz for the provided index.
+     */
+    public static BigDecimal getStopFrequencyFromIndex(int index) {
+        return getStartFrequencyFromIndex(index).add(BigDecimal.valueOf(GridConstant.GRANULARITY / 1000));
+    }
+
+    /**
+     * Get the bit index for the frequency.
+     *
+     * @param frequency 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);
+        int index =  (int) Math.round(nvalue + 284);
+        if (index < 0 || index > GridConstant.EFFECTIVE_BITS) {
+            throw new IllegalArgumentException("Frequency not in range " + frequency);
+        }
+        return index;
+    }
+
+    /**
+     * Get the spectrum width for rate and modulation format.
+     * @param rate Uint32
+     * @param modulationFormat ModulationFormat
+     * @return spectrum width in GHz
+     */
+    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 = String.valueOf(GridConstant.WIDTH_40);
+        }
+        return FrequencyGHz.getDefaultInstance(width);
+    }
+
+    /**
+     * Get central frequency of spectrum.
+     * @param minFrequency BigDecimal
+     * @param maxFrequency BigDecimal
+     * @return central frequency in THz
+     */
+    public static FrequencyTHz getCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
+        return new FrequencyTHz(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(
+                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(BigDecimal 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(BigDecimal frequency) {
+        return getIndexFromFrequency(frequency);
+    }
+
 }