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.openroadm.common.optical.channel.types.rev200529.FrequencyGHz;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev200529.FrequencyTHz;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMaps;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsKey;
22 import org.opendaylight.yangtools.yang.common.Uint16;
23 import org.opendaylight.yangtools.yang.common.Uint32;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Util class for grid.
29 * Thoses methods are used for pce spectrum assignment and topology update.
30 * They use maximal precision of BigDecimal
31 * For device configuration which needs precision (4 digits), dedicated methods are
32 * located in FixedFlex and FrexGrid classes.
35 public final class GridUtils {
36 private static final Logger LOG = LoggerFactory.getLogger(GridUtils.class);
41 public static Map<AvailFreqMapsKey, AvailFreqMaps> initFreqMaps4FixedGrid2Available() {
42 byte[] byteArray = new byte[GridConstant.NB_OCTECTS];
43 Arrays.fill(byteArray, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
44 Map<AvailFreqMapsKey, AvailFreqMaps> waveMap = new HashMap<>();
45 AvailFreqMaps availFreqMaps = new AvailFreqMapsBuilder().setMapName(GridConstant.C_BAND)
46 .setFreqMapGranularity(new FrequencyGHz(BigDecimal.valueOf(GridConstant.GRANULARITY)))
47 .setStartEdgeFreq(new FrequencyTHz(BigDecimal.valueOf(GridConstant.START_EDGE_FREQUENCY)))
48 .setEffectiveBits(Uint16.valueOf(GridConstant.EFFECTIVE_BITS))
49 .setFreqMap(byteArray)
51 waveMap.put(availFreqMaps.key(), availFreqMaps);
56 * Compute the wavelength index from Spectrum assignment begin index.
57 * Only for fix grid and device 1.2.1.
59 * @return the wavelength number.
61 public static long getWaveLengthIndexFromSpectrumAssigment(int index) {
62 return (GridConstant.EFFECTIVE_BITS - index) / GridConstant.NB_SLOTS_100G;
66 * Compute the start frequency in TGz for the given index.
68 * @return the start frequency in THz for the provided index.
70 public static BigDecimal getStartFrequencyFromIndex(int index) {
71 int nvalue = index - 284;
72 return BigDecimal.valueOf(GridConstant.CENTRAL_FREQUENCY + (nvalue * GridConstant.GRANULARITY / 1000));
76 * Compute the stop frequency in TGz for the given index.
78 * @return the stop frequency in THz for the provided index.
80 public static BigDecimal getStopFrequencyFromIndex(int index) {
81 return getStartFrequencyFromIndex(index).add(BigDecimal.valueOf(GridConstant.GRANULARITY / 1000));
85 * Get the bit index for the frequency.
87 * @param frequency BigDecimal
88 * @return the bit index of the frequency. Throw IllegalArgumentException if
89 * index not in range of 0 GridConstant.EFFECTIVE_BITS
91 public static int getIndexFromFrequency(BigDecimal frequency) {
92 double nvalue = (frequency.doubleValue() - GridConstant.CENTRAL_FREQUENCY) * (1000 / GridConstant.GRANULARITY);
93 int index = (int) Math.round(nvalue + 284);
94 if (index < 0 || index > GridConstant.EFFECTIVE_BITS) {
95 throw new IllegalArgumentException("Frequency not in range " + frequency);
101 * Get the spectrum width for rate and modulation format.
103 * @param modulationFormat ModulationFormat
104 * @return spectrum width in GHz
106 public static FrequencyGHz getWidthFromRateAndModulationFormat(Uint32 rate, ModulationFormat modulationFormat) {
107 String width = GridConstant.FREQUENCY_WIDTH_TABLE.get(rate, modulationFormat);
109 LOG.warn("No width found for service rate {} and modulation format {}, set width to 40", rate,
111 width = String.valueOf(GridConstant.WIDTH_40);
113 return FrequencyGHz.getDefaultInstance(width);
117 * Get central frequency of spectrum.
118 * @param minFrequency BigDecimal
119 * @param maxFrequency BigDecimal
120 * @return central frequency in THz
122 public static FrequencyTHz getCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
123 return new FrequencyTHz(computeCentralFrequency(minFrequency, maxFrequency));
128 * Get central frequency of spectrum with precision.
129 * @param minFrequency BigDecimal
130 * @param maxFrequency BigDecimal
131 * @param precision int
132 * @return central frequency in THz with precision
134 public static FrequencyTHz getCentralFrequencyWithPrecision(BigDecimal minFrequency,
135 BigDecimal maxFrequency, int precision) {
136 return new FrequencyTHz(computeCentralFrequency(minFrequency, maxFrequency)
137 .setScale(precision, RoundingMode.HALF_EVEN));
142 * Compute central frequency from min and max frequency.
143 * @param minFrequency BigDecimal
144 * @param maxFrequency BigDecimal
145 * @return central frequency
147 private static BigDecimal computeCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
148 return minFrequency.add(maxFrequency).divide(BigDecimal.valueOf(2));