Adapt TransportPCE code to Sulfur
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / fixedflex / GridUtils.java
1 /*
2  * Copyright © 2020 Orange, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.transportpce.common.fixedflex;
10
11 import java.math.BigDecimal;
12 import java.math.RoundingMode;
13 import java.util.Arrays;
14 import java.util.HashMap;
15 import java.util.Map;
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;
28
29 /**
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.
35  *
36  */
37 public final class GridUtils {
38     private static final Logger LOG = LoggerFactory.getLogger(GridUtils.class);
39
40     private GridUtils() {
41     }
42
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))))
50                 .setStartEdgeFreq(
51                     new FrequencyTHz(Decimal64.valueOf(BigDecimal.valueOf(GridConstant.START_EDGE_FREQUENCY))))
52                 .setEffectiveBits(Uint16.valueOf(GridConstant.EFFECTIVE_BITS))
53                 .setFreqMap(byteArray)
54                 .build();
55         waveMap.put(availFreqMaps.key(), availFreqMaps);
56         return waveMap;
57     }
58
59     /**
60      * Compute the wavelength index from Spectrum assignment begin index.
61      * Only for fix grid and device 1.2.1.
62      * @param index int
63      * @return the wavelength number.
64      */
65     public static long getWaveLengthIndexFromSpectrumAssigment(int index) {
66         return (GridConstant.EFFECTIVE_BITS - index) / GridConstant.NB_SLOTS_100G;
67     }
68
69     /**
70      * Compute the start frequency in TGz for the given index.
71      * @param index int
72      * @return the start frequency in THz for the provided index.
73      */
74     public static BigDecimal getStartFrequencyFromIndex(int index) {
75         int nvalue = index - 284;
76         return BigDecimal.valueOf(GridConstant.CENTRAL_FREQUENCY + (nvalue * GridConstant.GRANULARITY / 1000));
77     }
78
79     /**
80      * Compute the stop frequency in TGz for the given index.
81      * @param index int
82      * @return the stop frequency in THz for the provided index.
83      */
84     public static BigDecimal getStopFrequencyFromIndex(int index) {
85         return getStartFrequencyFromIndex(index).add(BigDecimal.valueOf(GridConstant.GRANULARITY / 1000));
86     }
87
88     /**
89      * Get the bit index for the frequency.
90      *
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
94      */
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);
101         }
102         return index;
103     }
104
105     /**
106      * Get the spectrum width for rate and modulation format.
107      * @param rate Uint32
108      * @param modulationFormat ModulationFormat
109      * @return spectrum width in GHz
110      */
111     public static FrequencyGHz getWidthFromRateAndModulationFormat(Uint32 rate, ModulationFormat modulationFormat) {
112         String width = GridConstant.FREQUENCY_WIDTH_TABLE.get(rate, modulationFormat);
113         if (width == null) {
114             LOG.warn("No width found for service rate {} and modulation format {}, set width to 40", rate,
115                     modulationFormat);
116             width = String.valueOf(GridConstant.WIDTH_40);
117         }
118         return FrequencyGHz.getDefaultInstance(width);
119     }
120
121     /**
122      * Get central frequency of spectrum.
123      * @param minFrequency BigDecimal
124      * @param maxFrequency BigDecimal
125      * @return central frequency in THz
126      */
127     public static FrequencyTHz getCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
128         return new FrequencyTHz(Decimal64.valueOf(computeCentralFrequency(minFrequency, maxFrequency)));
129
130     }
131
132     /**
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
138      */
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)));
145
146     }
147
148     /**
149      * Compute central frequency from min and max frequency.
150      * @param minFrequency BigDecimal
151      * @param maxFrequency BigDecimal
152      * @return central frequency
153      */
154     private static BigDecimal computeCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
155         return minFrequency.add(maxFrequency).divide(BigDecimal.valueOf(2));
156     }
157
158     /**
159      * Get the lower spectral index for the frequency.
160      * @param frequency BigDecimal
161      * @return the lower spectral index
162      */
163     public static int getLowerSpectralIndexFromFrequency(Decimal64 frequency) {
164         return getIndexFromFrequency(frequency) + 1;
165     }
166
167     /**
168      * Get the higher spectral index for the frequency.
169      * @param frequency BigDecimal
170      * @return the lower spectral index
171      */
172     public static int getHigherSpectralIndexFromFrequency(Decimal64 frequency) {
173         return getIndexFromFrequency(frequency);
174     }
175
176     /**
177      * Create spectrum information from service path input.
178      * @param input ServicePathInput
179      * @return SpectrumInformation
180      */
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");
185         }
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());
191         }
192         if (input.getMinFreq() != null) {
193             spectrumInformation.setMinFrequency(input.getMinFreq().getValue().decimalValue());
194         } else {
195             spectrumInformation.setMinFrequency(
196                     GridUtils.getStartFrequencyFromIndex(input.getLowerSpectralSlotNumber().intValue() - 1));
197         }
198         if (input.getMaxFreq() != null) {
199             spectrumInformation.setMaxFrequency(input.getMaxFreq().getValue().decimalValue());
200         } else {
201             spectrumInformation.setMaxFrequency(
202                     GridUtils.getStopFrequencyFromIndex(input.getHigherSpectralSlotNumber().intValue() - 1));
203         }
204         if (input.getCenterFreq() != null) {
205             spectrumInformation.setCenterFrequency(input.getCenterFreq().getValue().decimalValue());
206         } else {
207             spectrumInformation.setCenterFrequency(GridUtils.getCentralFrequency(spectrumInformation.getMinFrequency(),
208                             spectrumInformation.getMaxFrequency()).getValue().decimalValue());
209         }
210         if (input.getNmcWidth() != null) {
211             spectrumInformation.setWidth(input.getNmcWidth().getValue().decimalValue()
212                     .setScale(0, RoundingMode.CEILING));
213         }
214         if (input.getModulationFormat() != null) {
215             spectrumInformation.setModulationFormat(input.getModulationFormat());
216         }
217         return spectrumInformation;
218     }
219
220     /**
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
224      */
225     public static int getNFromFrequencyIndex(int frequencyIndex) {
226         return frequencyIndex - 284;
227     }
228
229     /**
230      * Convert the power from dBm to Watt.
231      * @param  dbm power in dBm.
232      * @return outputpower in Watt.
233      */
234     public static BigDecimal convertDbmW(double dbm) {
235         return BigDecimal.valueOf(Math.pow(10, (dbm - 30) / 10));
236     }
237
238 }