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.util.Arrays;
13 import java.util.HashMap;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev200529.FrequencyGHz;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev200529.FrequencyTHz;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMaps;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsKey;
21 import org.opendaylight.yangtools.yang.common.Uint16;
22 import org.opendaylight.yangtools.yang.common.Uint32;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Util class for grid.
28 * Thoses methods are used for pce spectrum assignment and topology update.
29 * They use maximal precision of BigDecimal
30 * For device configuration which needs precision (4 digits), dedicated methods are
31 * located in FixedFlex and FrexGrid classes.
34 public final class GridUtils {
35 private static final Logger LOG = LoggerFactory.getLogger(GridUtils.class);
40 public static Map<AvailFreqMapsKey, AvailFreqMaps> initFreqMaps4FixedGrid2Available() {
41 byte[] byteArray = new byte[GridConstant.NB_OCTECTS];
42 Arrays.fill(byteArray, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
43 Map<AvailFreqMapsKey, AvailFreqMaps> waveMap = new HashMap<>();
44 AvailFreqMaps availFreqMaps = new AvailFreqMapsBuilder().setMapName(GridConstant.C_BAND)
45 .setFreqMapGranularity(new FrequencyGHz(BigDecimal.valueOf(GridConstant.GRANULARITY)))
46 .setStartEdgeFreq(new FrequencyTHz(BigDecimal.valueOf(GridConstant.START_EDGE_FREQUENCY)))
47 .setEffectiveBits(Uint16.valueOf(GridConstant.EFFECTIVE_BITS))
48 .setFreqMap(byteArray)
50 waveMap.put(availFreqMaps.key(), availFreqMaps);
55 * Compute the wavelength index from Spectrum assignment begin index.
56 * Only for fix grid and device 1.2.1.
58 * @return the wavelength number.
60 public static long getWaveLengthIndexFromSpectrumAssigment(int index) {
61 return (GridConstant.EFFECTIVE_BITS - index) / GridConstant.NB_SLOTS_100G;
65 * Compute the start frequency in TGz for the given index.
67 * @return the start frequency in THz for the provided index.
69 public static BigDecimal getStartFrequencyFromIndex(int index) {
70 int nvalue = index - 284;
71 return BigDecimal.valueOf(GridConstant.CENTRAL_FREQUENCY + (nvalue * GridConstant.GRANULARITY / 1000));
75 * Compute the stop frequency in TGz for the given index.
77 * @return the stop frequency in THz for the provided index.
79 public static BigDecimal getStopFrequencyFromIndex(int index) {
80 return getStartFrequencyFromIndex(index).add(BigDecimal.valueOf(GridConstant.GRANULARITY / 1000));
84 * Get the bit index for the frequency.
86 * @param frequency BigDecimal
87 * @return the bit index of the frequency. Throw IllegalArgumentException if
88 * index not in range of 0 GridConstant.EFFECTIVE_BITS
90 public static int getIndexFromFrequency(BigDecimal frequency) {
91 double nvalue = (frequency.doubleValue() - GridConstant.CENTRAL_FREQUENCY) * (1000 / GridConstant.GRANULARITY);
92 int index = (int) Math.round(nvalue + 284);
93 if (index < 0 || index > GridConstant.EFFECTIVE_BITS) {
94 throw new IllegalArgumentException("Frequency not in range " + frequency);
100 * Get the spectrum width for rate and modulation format.
102 * @param modulationFormat ModulationFormat
103 * @return spectrum width in GHz
105 public static FrequencyGHz getWidthFromRateAndModulationFormat(Uint32 rate, ModulationFormat modulationFormat) {
106 String width = GridConstant.FREQUENCY_WIDTH_TABLE.get(rate, modulationFormat);
108 LOG.warn("No width found for service rate {} and modulation format {}, set width to 40", rate,
112 return FrequencyGHz.getDefaultInstance(width);
116 * Get central frequency of spectrum.
117 * @param minFrequency BigDecimal
118 * @param maxFrequency BigDecimal
119 * @return central frequency in THz
121 public static FrequencyTHz getCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
122 return new FrequencyTHz(minFrequency.add(maxFrequency).divide(BigDecimal.valueOf(2)));