Manage ODU4 services over multiple OTU4
[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.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;
27
28 /**
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.
34  *
35  */
36 public final class GridUtils {
37     private static final Logger LOG = LoggerFactory.getLogger(GridUtils.class);
38
39     private GridUtils() {
40     }
41
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)
51                 .build();
52         waveMap.put(availFreqMaps.key(), availFreqMaps);
53         return waveMap;
54     }
55
56     /**
57      * Compute the wavelength index from Spectrum assignment begin index.
58      * Only for fix grid and device 1.2.1.
59      * @param index int
60      * @return the wavelength number.
61      */
62     public static long getWaveLengthIndexFromSpectrumAssigment(int index) {
63         return (GridConstant.EFFECTIVE_BITS - index) / GridConstant.NB_SLOTS_100G;
64     }
65
66     /**
67      * Compute the start frequency in TGz for the given index.
68      * @param index int
69      * @return the start frequency in THz for the provided index.
70      */
71     public static BigDecimal getStartFrequencyFromIndex(int index) {
72         int nvalue = index - 284;
73         return BigDecimal.valueOf(GridConstant.CENTRAL_FREQUENCY + (nvalue * GridConstant.GRANULARITY / 1000));
74     }
75
76     /**
77      * Compute the stop frequency in TGz for the given index.
78      * @param index int
79      * @return the stop frequency in THz for the provided index.
80      */
81     public static BigDecimal getStopFrequencyFromIndex(int index) {
82         return getStartFrequencyFromIndex(index).add(BigDecimal.valueOf(GridConstant.GRANULARITY / 1000));
83     }
84
85     /**
86      * Get the bit index for the frequency.
87      *
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
91      */
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);
97         }
98         return index;
99     }
100
101     /**
102      * Get the spectrum width for rate and modulation format.
103      * @param rate Uint32
104      * @param modulationFormat ModulationFormat
105      * @return spectrum width in GHz
106      */
107     public static FrequencyGHz getWidthFromRateAndModulationFormat(Uint32 rate, ModulationFormat modulationFormat) {
108         String width = GridConstant.FREQUENCY_WIDTH_TABLE.get(rate, modulationFormat);
109         if (width == null) {
110             LOG.warn("No width found for service rate {} and modulation format {}, set width to 40", rate,
111                     modulationFormat);
112             width = String.valueOf(GridConstant.WIDTH_40);
113         }
114         return FrequencyGHz.getDefaultInstance(width);
115     }
116
117     /**
118      * Get central frequency of spectrum.
119      * @param minFrequency BigDecimal
120      * @param maxFrequency BigDecimal
121      * @return central frequency in THz
122      */
123     public static FrequencyTHz getCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
124         return new FrequencyTHz(computeCentralFrequency(minFrequency, maxFrequency));
125
126     }
127
128     /**
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
134      */
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));
140
141     }
142
143     /**
144      * Compute central frequency from min and max frequency.
145      * @param minFrequency BigDecimal
146      * @param maxFrequency BigDecimal
147      * @return central frequency
148      */
149     private static BigDecimal computeCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
150         return minFrequency.add(maxFrequency).divide(BigDecimal.valueOf(2));
151     }
152
153     /**
154      * Get the lower spectral index for the frequency.
155      * @param frequency BigDecimal
156      * @return the lower spectral index
157      */
158     public static int getLowerSpectralIndexFromFrequency(BigDecimal frequency) {
159         return getIndexFromFrequency(frequency) + 1;
160     }
161
162     /**
163      * Get the higher spectral index for the frequency.
164      * @param frequency BigDecimal
165      * @return the lower spectral index
166      */
167     public static int getHigherSpectralIndexFromFrequency(BigDecimal frequency) {
168         return getIndexFromFrequency(frequency);
169     }
170
171     /**
172      * Create spectrum information from service path input.
173      * @param input ServicePathInput
174      * @return SpectrumInformation
175      */
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");
180         }
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());
186         }
187         if (input.getMinFreq() != null) {
188             spectrumInformation.setMinFrequency(input.getMinFreq().getValue());
189         } else {
190             spectrumInformation.setMinFrequency(
191                     GridUtils.getStartFrequencyFromIndex(input.getLowerSpectralSlotNumber().intValue() - 1));
192         }
193         if (input.getMaxFreq() != null) {
194             spectrumInformation.setMaxFrequency(input.getMaxFreq().getValue());
195         } else {
196             spectrumInformation.setMaxFrequency(
197                     GridUtils.getStopFrequencyFromIndex(input.getHigherSpectralSlotNumber().intValue() - 1));
198         }
199         if (input.getCenterFreq() != null) {
200             spectrumInformation.setCenterFrequency(input.getCenterFreq().getValue());
201         } else {
202             spectrumInformation.setCenterFrequency(GridUtils
203                     .getCentralFrequency(spectrumInformation.getMinFrequency(),
204                             spectrumInformation.getMaxFrequency()).getValue());
205         }
206         if (input.getWidth() != null) {
207             spectrumInformation.setWidth(input.getWidth().getValue());
208         }
209         if (input.getModulationFormat() != null) {
210             spectrumInformation.setModulationFormat(input.getModulationFormat());
211         }
212         return spectrumInformation;
213     }
214
215     /**
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
219      */
220     public static int getNFromFrequencyIndex(int frequencyIndex) {
221         return frequencyIndex - 284;
222     }
223
224 }