Add methods to GridUtils
[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.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;
26
27 /**
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.
33  *
34  */
35 public final class GridUtils {
36     private static final Logger LOG = LoggerFactory.getLogger(GridUtils.class);
37
38     private GridUtils() {
39     }
40
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)
50                 .build();
51         waveMap.put(availFreqMaps.key(), availFreqMaps);
52         return waveMap;
53     }
54
55     /**
56      * Compute the wavelength index from Spectrum assignment begin index.
57      * Only for fix grid and device 1.2.1.
58      * @param index int
59      * @return the wavelength number.
60      */
61     public static long getWaveLengthIndexFromSpectrumAssigment(int index) {
62         return (GridConstant.EFFECTIVE_BITS - index) / GridConstant.NB_SLOTS_100G;
63     }
64
65     /**
66      * Compute the start frequency in TGz for the given index.
67      * @param index int
68      * @return the start frequency in THz for the provided index.
69      */
70     public static BigDecimal getStartFrequencyFromIndex(int index) {
71         int nvalue = index - 284;
72         return BigDecimal.valueOf(GridConstant.CENTRAL_FREQUENCY + (nvalue * GridConstant.GRANULARITY / 1000));
73     }
74
75     /**
76      * Compute the stop frequency in TGz for the given index.
77      * @param index int
78      * @return the stop frequency in THz for the provided index.
79      */
80     public static BigDecimal getStopFrequencyFromIndex(int index) {
81         return getStartFrequencyFromIndex(index).add(BigDecimal.valueOf(GridConstant.GRANULARITY / 1000));
82     }
83
84     /**
85      * Get the bit index for the frequency.
86      *
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
90      */
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);
96         }
97         return index;
98     }
99
100     /**
101      * Get the spectrum width for rate and modulation format.
102      * @param rate Uint32
103      * @param modulationFormat ModulationFormat
104      * @return spectrum width in GHz
105      */
106     public static FrequencyGHz getWidthFromRateAndModulationFormat(Uint32 rate, ModulationFormat modulationFormat) {
107         String width = GridConstant.FREQUENCY_WIDTH_TABLE.get(rate, modulationFormat);
108         if (width == null) {
109             LOG.warn("No width found for service rate {} and modulation format {}, set width to 40", rate,
110                     modulationFormat);
111             width = String.valueOf(GridConstant.WIDTH_40);
112         }
113         return FrequencyGHz.getDefaultInstance(width);
114     }
115
116     /**
117      * Get central frequency of spectrum.
118      * @param minFrequency BigDecimal
119      * @param maxFrequency BigDecimal
120      * @return central frequency in THz
121      */
122     public static FrequencyTHz getCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
123         return new FrequencyTHz(computeCentralFrequency(minFrequency, maxFrequency));
124
125     }
126
127     /**
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
133      */
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));
138
139     }
140
141     /**
142      * Compute central frequency from min and max frequency.
143      * @param minFrequency BigDecimal
144      * @param maxFrequency BigDecimal
145      * @return central frequency
146      */
147     private static BigDecimal computeCentralFrequency(BigDecimal minFrequency, BigDecimal maxFrequency) {
148         return minFrequency.add(maxFrequency).divide(BigDecimal.valueOf(2));
149     }
150
151 }