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.rev211210.FrequencyGHz;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev211210.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.rev211210.available.freq.map.AvailFreqMaps;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.available.freq.map.AvailFreqMapsBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.available.freq.map.AvailFreqMapsKey;
23 import org.opendaylight.yangtools.yang.common.Decimal64;
24 import org.opendaylight.yangtools.yang.common.Uint16;
25 import org.opendaylight.yangtools.yang.common.Uint32;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Util class for grid.
31 * Thoses methods are used for pce spectrum assignment and topology update.
32 * They use maximal precision of BigDecimal
33 * For device configuration which needs precision (4 digits), dedicated methods are
34 * located in FixedFlex and FrexGrid classes.
37 public final class GridUtils {
38 private static final Logger LOG = LoggerFactory.getLogger(GridUtils.class);
43 public static Map<AvailFreqMapsKey, AvailFreqMaps> initFreqMaps4FixedGrid2Available() {
44 byte[] byteArray = new byte[GridConstant.NB_OCTECTS];
45 Arrays.fill(byteArray, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
46 Map<AvailFreqMapsKey, AvailFreqMaps> waveMap = new HashMap<>();
47 AvailFreqMaps availFreqMaps = new AvailFreqMapsBuilder().setMapName(GridConstant.C_BAND)
48 .setFreqMapGranularity(
49 new FrequencyGHz(Decimal64.valueOf(BigDecimal.valueOf(GridConstant.GRANULARITY))))
51 new FrequencyTHz(Decimal64.valueOf(BigDecimal.valueOf(GridConstant.START_EDGE_FREQUENCY))))
52 .setEffectiveBits(Uint16.valueOf(GridConstant.EFFECTIVE_BITS))
53 .setFreqMap(byteArray)
55 waveMap.put(availFreqMaps.key(), availFreqMaps);
60 * Compute the wavelength index from Spectrum assignment begin index.
61 * Only for fix grid and device 1.2.1.
63 * @return the wavelength number.
65 public static long getWaveLengthIndexFromSpectrumAssigment(int index) {
66 return (GridConstant.EFFECTIVE_BITS - index) / GridConstant.NB_SLOTS_100G;
70 * Compute the start frequency in TGz for the given index.
72 * @return the start frequency in THz for the provided index.
74 public static BigDecimal getStartFrequencyFromIndex(int index) {
75 int nvalue = index - 284;
76 return BigDecimal.valueOf(GridConstant.CENTRAL_FREQUENCY + (nvalue * GridConstant.GRANULARITY / 1000));
80 * Compute the stop frequency in TGz for the given index.
82 * @return the stop frequency in THz for the provided index.
84 public static BigDecimal getStopFrequencyFromIndex(int index) {
85 return getStartFrequencyFromIndex(index).add(BigDecimal.valueOf(GridConstant.GRANULARITY / 1000));
89 * Get the bit index for the frequency.
91 * @param atozMinFrequency BigDecimal
92 * @return the bit index of the frequency. Throw IllegalArgumentException if
93 * index not in range of 0 GridConstant.EFFECTIVE_BITS
95 public static int getIndexFromFrequency(Decimal64 atozMinFrequency) {
96 double nvalue = (atozMinFrequency.doubleValue() - GridConstant.CENTRAL_FREQUENCY)
97 * (1000 / GridConstant.GRANULARITY);
98 int index = (int) Math.round(nvalue + 284);
99 if (index < 0 || index > GridConstant.EFFECTIVE_BITS) {
100 throw new IllegalArgumentException("Frequency not in range " + atozMinFrequency);
106 * Get the spectrum width for rate and modulation format.
108 * @param modulationFormat ModulationFormat
109 * @return spectrum width in GHz
111 public static FrequencyGHz getWidthFromRateAndModulationFormat(Uint32 rate, ModulationFormat modulationFormat) {
112 String width = GridConstant.FREQUENCY_WIDTH_TABLE.get(rate, modulationFormat);
114 LOG.warn("No width found for service rate {} and modulation format {}, set width to 40", rate,
116 width = String.valueOf(GridConstant.WIDTH_40);
118 return FrequencyGHz.getDefaultInstance(width);
122 * Get central frequency of spectrum.
123 * @param minFrequency BigDecimal
124 * @param maxFrequency BigDecimal
125 * @return central frequency in THz
127 public static FrequencyTHz getCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
128 return new FrequencyTHz(Decimal64.valueOf(computeCentralFrequency(minFrequency, maxFrequency)));
133 * Get central frequency of spectrum with precision.
134 * @param minFrequency BigDecimal
135 * @param maxFrequency BigDecimal
136 * @param precision int
137 * @return central frequency in THz with precision
139 public static org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz
140 getCentralFrequencyWithPrecision(BigDecimal minFrequency,
141 BigDecimal maxFrequency, int precision) {
142 return new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz(
143 Decimal64.valueOf(computeCentralFrequency(minFrequency, maxFrequency)
144 .setScale(precision, RoundingMode.HALF_EVEN)));
149 * Compute central frequency from min and max frequency.
150 * @param minFrequency BigDecimal
151 * @param maxFrequency BigDecimal
152 * @return central frequency
154 private static BigDecimal computeCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
155 return minFrequency.add(maxFrequency).divide(BigDecimal.valueOf(2));
159 * Get the lower spectral index for the frequency.
160 * @param frequency BigDecimal
161 * @return the lower spectral index
163 public static int getLowerSpectralIndexFromFrequency(Decimal64 frequency) {
164 return getIndexFromFrequency(frequency) + 1;
168 * Get the higher spectral index for the frequency.
169 * @param frequency BigDecimal
170 * @return the lower spectral index
172 public static int getHigherSpectralIndexFromFrequency(Decimal64 frequency) {
173 return getIndexFromFrequency(frequency);
177 * Create spectrum information from service path input.
178 * @param input ServicePathInput
179 * @return SpectrumInformation
181 public static SpectrumInformation initSpectrumInformationFromServicePathInput(ServicePathInput input) {
182 if (input.getLowerSpectralSlotNumber() == null || input.getHigherSpectralSlotNumber() == null) {
183 LOG.error("low and higher spectral slot numbers cannot be null");
184 throw new IllegalArgumentException("low and higher spectral slot numbers cannot be null");
186 SpectrumInformation spectrumInformation = new SpectrumInformation();
187 spectrumInformation.setLowerSpectralSlotNumber(input.getLowerSpectralSlotNumber().intValue());
188 spectrumInformation.setHigherSpectralSlotNumber(input.getHigherSpectralSlotNumber().intValue());
189 if (input.getWaveNumber() != null) {
190 spectrumInformation.setWaveLength(input.getWaveNumber());
192 if (input.getMinFreq() != null) {
193 spectrumInformation.setMinFrequency(input.getMinFreq().getValue().decimalValue());
195 spectrumInformation.setMinFrequency(
196 GridUtils.getStartFrequencyFromIndex(input.getLowerSpectralSlotNumber().intValue() - 1));
198 if (input.getMaxFreq() != null) {
199 spectrumInformation.setMaxFrequency(input.getMaxFreq().getValue().decimalValue());
201 spectrumInformation.setMaxFrequency(
202 GridUtils.getStopFrequencyFromIndex(input.getHigherSpectralSlotNumber().intValue() - 1));
204 if (input.getCenterFreq() != null) {
205 spectrumInformation.setCenterFrequency(input.getCenterFreq().getValue().decimalValue());
207 spectrumInformation.setCenterFrequency(GridUtils.getCentralFrequency(spectrumInformation.getMinFrequency(),
208 spectrumInformation.getMaxFrequency()).getValue().decimalValue());
210 if (input.getNmcWidth() != null) {
211 spectrumInformation.setWidth(input.getNmcWidth().getValue().decimalValue()
212 .setScale(0, RoundingMode.CEILING));
214 if (input.getModulationFormat() != null) {
215 spectrumInformation.setModulationFormat(input.getModulationFormat());
217 return spectrumInformation;
221 * Get the N value of range -284 +484 from frequency index array.
222 * @param frequencyIndex the frequency index f range 0 768.
223 * @return the N value
225 public static int getNFromFrequencyIndex(int frequencyIndex) {
226 return frequencyIndex - 284;
230 * Convert the power from dBm to Watt.
231 * @param dbm power in dBm.
232 * @return outputpower in Watt.
234 public static BigDecimal convertDbmW(double dbm) {
235 return BigDecimal.valueOf(Math.pow(10, (dbm - 30) / 10));