2 * Copyright © 2020 Orange, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.transportpce.common.fixedflex;
11 import java.math.BigDecimal;
12 import java.math.RoundingMode;
13 import java.util.Arrays;
14 import java.util.HashMap;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathInput;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev200529.FrequencyGHz;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev200529.FrequencyTHz;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMaps;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsKey;
23 import org.opendaylight.yangtools.yang.common.Uint16;
24 import org.opendaylight.yangtools.yang.common.Uint32;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Util class for grid.
30 * Thoses methods are used for pce spectrum assignment and topology update.
31 * They use maximal precision of BigDecimal
32 * For device configuration which needs precision (4 digits), dedicated methods are
33 * located in FixedFlex and FrexGrid classes.
36 public final class GridUtils {
37 private static final Logger LOG = LoggerFactory.getLogger(GridUtils.class);
42 public static Map<AvailFreqMapsKey, AvailFreqMaps> initFreqMaps4FixedGrid2Available() {
43 byte[] byteArray = new byte[GridConstant.NB_OCTECTS];
44 Arrays.fill(byteArray, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
45 Map<AvailFreqMapsKey, AvailFreqMaps> waveMap = new HashMap<>();
46 AvailFreqMaps availFreqMaps = new AvailFreqMapsBuilder().setMapName(GridConstant.C_BAND)
47 .setFreqMapGranularity(new FrequencyGHz(BigDecimal.valueOf(GridConstant.GRANULARITY)))
48 .setStartEdgeFreq(new FrequencyTHz(BigDecimal.valueOf(GridConstant.START_EDGE_FREQUENCY)))
49 .setEffectiveBits(Uint16.valueOf(GridConstant.EFFECTIVE_BITS))
50 .setFreqMap(byteArray)
52 waveMap.put(availFreqMaps.key(), availFreqMaps);
57 * Compute the wavelength index from Spectrum assignment begin index.
58 * Only for fix grid and device 1.2.1.
60 * @return the wavelength number.
62 public static long getWaveLengthIndexFromSpectrumAssigment(int index) {
63 return (GridConstant.EFFECTIVE_BITS - index) / GridConstant.NB_SLOTS_100G;
67 * Compute the start frequency in TGz for the given index.
69 * @return the start frequency in THz for the provided index.
71 public static BigDecimal getStartFrequencyFromIndex(int index) {
72 int nvalue = index - 284;
73 return BigDecimal.valueOf(GridConstant.CENTRAL_FREQUENCY + (nvalue * GridConstant.GRANULARITY / 1000));
77 * Compute the stop frequency in TGz for the given index.
79 * @return the stop frequency in THz for the provided index.
81 public static BigDecimal getStopFrequencyFromIndex(int index) {
82 return getStartFrequencyFromIndex(index).add(BigDecimal.valueOf(GridConstant.GRANULARITY / 1000));
86 * Get the bit index for the frequency.
88 * @param frequency BigDecimal
89 * @return the bit index of the frequency. Throw IllegalArgumentException if
90 * index not in range of 0 GridConstant.EFFECTIVE_BITS
92 public static int getIndexFromFrequency(BigDecimal frequency) {
93 double nvalue = (frequency.doubleValue() - GridConstant.CENTRAL_FREQUENCY) * (1000 / GridConstant.GRANULARITY);
94 int index = (int) Math.round(nvalue + 284);
95 if (index < 0 || index > GridConstant.EFFECTIVE_BITS) {
96 throw new IllegalArgumentException("Frequency not in range " + frequency);
102 * Get the spectrum width for rate and modulation format.
104 * @param modulationFormat ModulationFormat
105 * @return spectrum width in GHz
107 public static FrequencyGHz getWidthFromRateAndModulationFormat(Uint32 rate, ModulationFormat modulationFormat) {
108 String width = GridConstant.FREQUENCY_WIDTH_TABLE.get(rate, modulationFormat);
110 LOG.warn("No width found for service rate {} and modulation format {}, set width to 40", rate,
112 width = String.valueOf(GridConstant.WIDTH_40);
114 return FrequencyGHz.getDefaultInstance(width);
118 * Get central frequency of spectrum.
119 * @param minFrequency BigDecimal
120 * @param maxFrequency BigDecimal
121 * @return central frequency in THz
123 public static FrequencyTHz getCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
124 return new FrequencyTHz(computeCentralFrequency(minFrequency, maxFrequency));
129 * Get central frequency of spectrum with precision.
130 * @param minFrequency BigDecimal
131 * @param maxFrequency BigDecimal
132 * @param precision int
133 * @return central frequency in THz with precision
135 public static org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz
136 getCentralFrequencyWithPrecision(BigDecimal minFrequency,
137 BigDecimal maxFrequency, int precision) {
138 return new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz(
139 computeCentralFrequency(minFrequency, maxFrequency).setScale(precision, RoundingMode.HALF_EVEN));
144 * Compute central frequency from min and max frequency.
145 * @param minFrequency BigDecimal
146 * @param maxFrequency BigDecimal
147 * @return central frequency
149 private static BigDecimal computeCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
150 return minFrequency.add(maxFrequency).divide(BigDecimal.valueOf(2));
154 * Get the lower spectral index for the frequency.
155 * @param frequency BigDecimal
156 * @return the lower spectral index
158 public static int getLowerSpectralIndexFromFrequency(BigDecimal frequency) {
159 return getIndexFromFrequency(frequency) + 1;
163 * Get the higher spectral index for the frequency.
164 * @param frequency BigDecimal
165 * @return the lower spectral index
167 public static int getHigherSpectralIndexFromFrequency(BigDecimal frequency) {
168 return getIndexFromFrequency(frequency);
172 * Create spectrum information from service path input.
173 * @param input ServicePathInput
174 * @return SpectrumInformation
176 public static SpectrumInformation initSpectrumInformationFromServicePathInput(ServicePathInput input) {
177 if (input.getLowerSpectralSlotNumber() == null || input.getHigherSpectralSlotNumber() == null) {
178 LOG.error("low and higher spectral slot numbers cannot be null");
179 throw new IllegalArgumentException("low and higher spectral slot numbers cannot be null");
181 SpectrumInformation spectrumInformation = new SpectrumInformation();
182 spectrumInformation.setLowerSpectralSlotNumber(input.getLowerSpectralSlotNumber().intValue());
183 spectrumInformation.setHigherSpectralSlotNumber(input.getHigherSpectralSlotNumber().intValue());
184 if (input.getWaveNumber() != null) {
185 spectrumInformation.setWaveLength(input.getWaveNumber());
187 if (input.getMinFreq() != null) {
188 spectrumInformation.setMinFrequency(input.getMinFreq().getValue());
190 spectrumInformation.setMinFrequency(
191 GridUtils.getStartFrequencyFromIndex(input.getLowerSpectralSlotNumber().intValue() - 1));
193 if (input.getMaxFreq() != null) {
194 spectrumInformation.setMaxFrequency(input.getMaxFreq().getValue());
196 spectrumInformation.setMaxFrequency(
197 GridUtils.getStopFrequencyFromIndex(input.getHigherSpectralSlotNumber().intValue() - 1));
199 if (input.getCenterFreq() != null) {
200 spectrumInformation.setCenterFrequency(input.getCenterFreq().getValue());
202 spectrumInformation.setCenterFrequency(GridUtils
203 .getCentralFrequency(spectrumInformation.getMinFrequency(),
204 spectrumInformation.getMaxFrequency()).getValue());
206 if (input.getNmcWidth() != null) {
207 spectrumInformation.setWidth(input.getNmcWidth().getValue());
209 if (input.getModulationFormat() != null) {
210 spectrumInformation.setModulationFormat(input.getModulationFormat());
212 return spectrumInformation;
216 * Get the N value of range -284 +484 from frequency index array.
217 * @param frequencyIndex the frequency index f range 0 768.
218 * @return the N value
220 public static int getNFromFrequencyIndex(int frequencyIndex) {
221 return frequencyIndex - 284;
225 * Convert the power from dBm to Watt.
226 * @param dbm power in dBm.
227 * @return outputpower in Watt.
229 public static BigDecimal convertDbmW(double dbm) {
230 return BigDecimal.valueOf(Math.pow(10, (dbm - 30) / 10));