6c60a7c3ec3382cec34078a0287e3ff2ba9613d2
[transportpce.git] / common / src / test / java / org / opendaylight / transportpce / common / fixedflex / GridUtilsTest.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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 import java.math.BigDecimal;
15 import java.util.Arrays;
16 import java.util.Map;
17 import org.junit.Test;
18 import org.opendaylight.transportpce.common.ServiceRateConstant;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev210618.ServicePathInput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev210618.ServicePathInputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev200529.FrequencyGHz;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev200529.FrequencyTHz;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMaps;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsKey;
26 import org.opendaylight.yangtools.yang.common.Uint32;
27
28 public class GridUtilsTest {
29
30     @Test
31     public void getWaveLengthIndexFromSpectrumAssigmentTest() {
32         assertEquals("Wavelength index should be 15", 15, GridUtils.getWaveLengthIndexFromSpectrumAssigment(647));
33     }
34
35     @Test
36     public void getFrequencyFromIndexTest() {
37         BigDecimal[] expectedFrequencies = new BigDecimal[768];
38         BigDecimal frequency = BigDecimal.valueOf(191.325);
39         for (int i = 0; i < expectedFrequencies.length; i++) {
40             expectedFrequencies[i] = frequency;
41             frequency = frequency.add(BigDecimal.valueOf(0.00625));
42         }
43         assertEquals("Frequency should be 191.325", 0, expectedFrequencies[0]
44                 .compareTo(GridUtils.getStartFrequencyFromIndex(0)));
45         assertEquals("Frequency should be 193.1", 0, expectedFrequencies[284]
46                 .compareTo(GridUtils.getStartFrequencyFromIndex(284)));
47         assertEquals("Frequency should be 196.1188", 0, expectedFrequencies[767]
48                 .compareTo(GridUtils.getStartFrequencyFromIndex(767)));
49     }
50
51     @Test
52     public void initFreqMaps4FixedGrid2AvailableTest() {
53         AvailFreqMapsKey key = new AvailFreqMapsKey(GridConstant.C_BAND);
54         byte[] byteArray = new byte[GridConstant.NB_OCTECTS];
55         Arrays.fill(byteArray, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
56         Map<AvailFreqMapsKey, AvailFreqMaps> availFreqMaps = GridUtils.initFreqMaps4FixedGrid2Available();
57         assertEquals("Should contains 1 element", 1, availFreqMaps.size());
58         assertTrue("should contains cband key", availFreqMaps.containsKey(key));
59         assertTrue("Should have available freq map", Arrays.equals(byteArray, availFreqMaps.get(key).getFreqMap()));
60     }
61
62     @Test
63     public void getIndexFromFrequencyTest() {
64         assertEquals("Index should be 693", 693, GridUtils.getIndexFromFrequency(BigDecimal.valueOf(195.65625)));
65         assertEquals("Index should be 0", 0, GridUtils.getIndexFromFrequency(BigDecimal.valueOf(191.325)));
66         assertEquals("Index should be 767", 767, GridUtils.getIndexFromFrequency(BigDecimal.valueOf(196.11875)));
67         assertEquals("Index should be 8", 8, GridUtils.getIndexFromFrequency(BigDecimal.valueOf(191.375)));
68         assertEquals("Index should be 15", 15, GridUtils.getIndexFromFrequency(BigDecimal.valueOf(191.41875)));
69         assertEquals("Index should be 768", 768, GridUtils.getIndexFromFrequency(BigDecimal.valueOf(196.125)));
70     }
71
72     @Test(expected = IllegalArgumentException.class)
73     public void getIndexFromFrequencyExceptionTest() {
74         GridUtils.getIndexFromFrequency(BigDecimal.valueOf(196.13125));
75     }
76
77     @Test(expected = IllegalArgumentException.class)
78     public void getIndexFromFrequencyException2Test() {
79         GridUtils.getIndexFromFrequency(BigDecimal.valueOf(191.31875));
80     }
81
82     @Test
83     public void getWidthFromRateAndModulationFormatTest() {
84         assertEquals("Width should be 75", new FrequencyGHz(GridConstant.WIDTH_75),
85                 GridUtils.getWidthFromRateAndModulationFormat(ServiceRateConstant.RATE_400,
86                         ModulationFormat.DpQam16));
87     }
88
89     @Test
90     public void getWidthFromRateAndModulationFormatNotFoundTest() {
91         assertEquals("As not found width should be 40", new FrequencyGHz(GridConstant.WIDTH_40),
92                 GridUtils.getWidthFromRateAndModulationFormat(ServiceRateConstant.RATE_100,
93                         ModulationFormat.DpQam16));
94     }
95
96     @Test
97     public void getCentralFrequencyTest() {
98         assertEquals("Central frequency should be 191.350",
99                 new FrequencyTHz(BigDecimal.valueOf(191.35).setScale(3)),
100                 GridUtils.getCentralFrequency(BigDecimal.valueOf(191.325), BigDecimal.valueOf(191.375)));
101     }
102
103     @Test
104     public void getCentralFrequencyWithPrecisionTest() {
105         assertEquals("Central frequency should be 191.3500",
106                 new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz(
107                         BigDecimal.valueOf(191.35).setScale(4)),
108                 GridUtils.getCentralFrequencyWithPrecision(BigDecimal.valueOf(191.325),
109                         BigDecimal.valueOf(191.375), 4));
110     }
111
112     @Test
113     public void getCentralFrequencyWithPrecisionAndRoundTest() {
114         assertEquals("Central frequency should be 191.3499",
115                 new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz(
116                         BigDecimal.valueOf(191.3499)),
117                 GridUtils.getCentralFrequencyWithPrecision(BigDecimal.valueOf(191.3244445),
118                         BigDecimal.valueOf(191.3754457788), 4));
119     }
120
121     @Test
122     public void initSpectrumInformationFromServicePathInputTest() {
123         ServicePathInput input = new ServicePathInputBuilder()
124                 .setWaveNumber(Uint32.valueOf(1))
125                 .setCenterFreq(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
126                         .FrequencyTHz(BigDecimal.valueOf(196.1)))
127                 .setHigherSpectralSlotNumber(Uint32.valueOf(768))
128                 .setLowerSpectralSlotNumber(Uint32.valueOf(761))
129                 .setMaxFreq(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
130                         .FrequencyTHz(BigDecimal.valueOf(196.125)))
131                 .setMinFreq(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
132                         .FrequencyTHz(BigDecimal.valueOf(196.075)))
133                 .setWidth(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
134                         .FrequencyGHz(GridConstant.WIDTH_40))
135                 .build();
136         SpectrumInformation spectrumInformation = GridUtils.initSpectrumInformationFromServicePathInput(input);
137         assertEquals("Width should be 40", BigDecimal.valueOf(40), spectrumInformation.getWidth());
138         assertEquals("Wavelength should be 1", Uint32.valueOf(1), spectrumInformation.getWaveLength());
139         assertEquals("Center freq should be 196.1", BigDecimal.valueOf(196.1).setScale(4),
140                 spectrumInformation.getCenterFrequency());
141         assertEquals("Lower slot number should be 761", 761, spectrumInformation.getLowerSpectralSlotNumber());
142         assertEquals("Higher slot number should be 768", 768, spectrumInformation.getHigherSpectralSlotNumber());
143         assertEquals("Min freq should be 196.075", BigDecimal.valueOf(196.075).setScale(4),
144                 spectrumInformation.getMinFrequency());
145         assertEquals("Max freq should be 196.125", BigDecimal.valueOf(196.125).setScale(4),
146                 spectrumInformation.getMaxFrequency());
147     }
148
149     @Test
150     public void initSpectrumInformationFromServicePathInputNoCenterFreqTest() {
151         ServicePathInput input = new ServicePathInputBuilder()
152                 .setWaveNumber(Uint32.valueOf(1))
153                 .setHigherSpectralSlotNumber(Uint32.valueOf(768))
154                 .setLowerSpectralSlotNumber(Uint32.valueOf(761))
155                 .setMaxFreq(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
156                         .FrequencyTHz(BigDecimal.valueOf(196.125)))
157                 .setMinFreq(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
158                         .FrequencyTHz(BigDecimal.valueOf(196.075)))
159                 .setWidth(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
160                         .FrequencyGHz(GridConstant.WIDTH_40))
161                 .build();
162         SpectrumInformation spectrumInformation = GridUtils.initSpectrumInformationFromServicePathInput(input);
163         assertEquals("Width should be 40", BigDecimal.valueOf(40), spectrumInformation.getWidth());
164         assertEquals("Wavelength should be 1", Uint32.valueOf(1), spectrumInformation.getWaveLength());
165         assertEquals("Center freq should be 196.1", BigDecimal.valueOf(196.1).setScale(4),
166                 spectrumInformation.getCenterFrequency());
167         assertEquals("Lower slot number should be 761", 761, spectrumInformation.getLowerSpectralSlotNumber());
168         assertEquals("Higher slot number should be 768", 768, spectrumInformation.getHigherSpectralSlotNumber());
169         assertEquals("Min freq should be 196.075", BigDecimal.valueOf(196.075).setScale(4),
170                 spectrumInformation.getMinFrequency());
171         assertEquals("Max freq should be 196.125", BigDecimal.valueOf(196.125).setScale(4),
172                 spectrumInformation.getMaxFrequency());
173     }
174
175     @Test
176     public void initSpectrumInformationFromServicePathInputNoFreqTest() {
177         ServicePathInput input = new ServicePathInputBuilder()
178                 .setWaveNumber(Uint32.valueOf(1))
179                 .setHigherSpectralSlotNumber(Uint32.valueOf(768))
180                 .setLowerSpectralSlotNumber(Uint32.valueOf(761))
181                 .setWidth(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
182                         .FrequencyGHz(GridConstant.WIDTH_40))
183                 .build();
184         SpectrumInformation spectrumInformation = GridUtils.initSpectrumInformationFromServicePathInput(input);
185         assertEquals("Width should be 40", BigDecimal.valueOf(40), spectrumInformation.getWidth());
186         assertEquals("Wavelength should be 1", Uint32.valueOf(1), spectrumInformation.getWaveLength());
187         assertEquals("Center freq should be 196.1", BigDecimal.valueOf(196.1).setScale(4),
188                 spectrumInformation.getCenterFrequency());
189         assertEquals("Lower slot number should be 761", 761, spectrumInformation.getLowerSpectralSlotNumber());
190         assertEquals("Higher slot number should be 768", 768, spectrumInformation.getHigherSpectralSlotNumber());
191         assertEquals("Min freq should be 196.075", BigDecimal.valueOf(196.075).setScale(4),
192                 spectrumInformation.getMinFrequency());
193         assertEquals("Max freq should be 196.125", BigDecimal.valueOf(196.125).setScale(4),
194                 spectrumInformation.getMaxFrequency());
195     }
196
197     @Test(expected = IllegalArgumentException.class)
198     public void initSpectrumInformationFromServicePathInputNoSlotTest() {
199         GridUtils.initSpectrumInformationFromServicePathInput(new ServicePathInputBuilder().build());
200     }
201 }