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