HIGUCHI Yuta
Committed by Yuta HIGUCHI

Unit test for OchSignal

Change-Id: If77042f60c1b95889b9442cdda453a44c10c1ef4
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onosproject.net; 16 package org.onosproject.net;
17 17
18 import java.util.Comparator; 18 import java.util.Comparator;
19 +import java.util.TreeSet;
19 20
20 import static com.google.common.base.Preconditions.checkArgument; 21 import static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull; 22 import static com.google.common.base.Preconditions.checkNotNull;
...@@ -24,6 +25,17 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -24,6 +25,17 @@ import static com.google.common.base.Preconditions.checkNotNull;
24 * Comparator implementation for OchSignal. Assumes identical grid type and channel spacing. 25 * Comparator implementation for OchSignal. Assumes identical grid type and channel spacing.
25 */ 26 */
26 public class DefaultOchSignalComparator implements Comparator<OchSignal> { 27 public class DefaultOchSignalComparator implements Comparator<OchSignal> {
28 +
29 + private static final DefaultOchSignalComparator INSTANCE = new DefaultOchSignalComparator();
30 +
31 + /**
32 + * Creates a new instance of {@link TreeSet} using this Comparator.
33 + * @return {@link TreeSet}
34 + */
35 + public static TreeSet<OchSignal> newOchSignalTreeSet() {
36 + return new TreeSet<>(INSTANCE);
37 + }
38 +
27 @Override 39 @Override
28 public int compare(OchSignal o1, OchSignal o2) { 40 public int compare(OchSignal o1, OchSignal o2) {
29 checkNotNull(o1.gridType()); 41 checkNotNull(o1.gridType());
......
...@@ -24,19 +24,22 @@ import java.util.List; ...@@ -24,19 +24,22 @@ import java.util.List;
24 import java.util.Objects; 24 import java.util.Objects;
25 import java.util.Set; 25 import java.util.Set;
26 import java.util.SortedSet; 26 import java.util.SortedSet;
27 -import java.util.TreeSet;
28 -import java.util.function.Supplier;
29 import java.util.stream.Collectors; 27 import java.util.stream.Collectors;
30 import java.util.stream.IntStream; 28 import java.util.stream.IntStream;
31 29
32 import static com.google.common.base.Preconditions.checkArgument; 30 import static com.google.common.base.Preconditions.checkArgument;
33 import static com.google.common.base.Preconditions.checkNotNull; 31 import static com.google.common.base.Preconditions.checkNotNull;
32 +import static org.onosproject.net.ChannelSpacing.CHL_12P5GHZ;
33 +import static org.onosproject.net.ChannelSpacing.CHL_6P25GHZ;
34 +import static org.onosproject.net.GridType.DWDM;
35 +import static org.onosproject.net.GridType.FLEX;
34 36
35 /** 37 /**
36 * Implementation of Lambda representing OCh (Optical Channel) Signal. 38 * Implementation of Lambda representing OCh (Optical Channel) Signal.
37 * 39 *
38 * <p> 40 * <p>
39 * See ITU G.709 "Interfaces for the Optical Transport Network (OTN)". 41 * See ITU G.709 "Interfaces for the Optical Transport Network (OTN)".
42 + * ITU G.694.1 "Spectral grids for WDM applications: DWDM frequency grid".
40 * </p> 43 * </p>
41 */ 44 */
42 public class OchSignal implements Lambda { 45 public class OchSignal implements Lambda {
...@@ -73,6 +76,26 @@ public class OchSignal implements Lambda { ...@@ -73,6 +76,26 @@ public class OchSignal implements Lambda {
73 } 76 }
74 77
75 /** 78 /**
79 + * Creates an instance of {@link OchSignal} representing a flex grid frequency slot.
80 + * @param index slot index (relative to "the center frequency" 193.1THz)
81 + * @return FlexGrid {@link OchSignal}
82 + */
83 + public static OchSignal newFlexGridSlot(int index) {
84 + return new OchSignal(FLEX, CHL_6P25GHZ, index, 1);
85 + }
86 +
87 + /**
88 + * Creates an instance of {@link OchSignal} representing a fixed DWDM frequency slot.
89 + * @param spacing channel spacing
90 + * @param index slot index (relative to "the center frequency" 193.1THz)
91 + * @return DWDM {@link OchSignal}
92 + */
93 + public static OchSignal newDwdmSlot(ChannelSpacing spacing, int index) {
94 + return new OchSignal(DWDM, spacing, index,
95 + (int) (spacing.frequency().asHz() / CHL_12P5GHZ.frequency().asHz()));
96 + }
97 +
98 + /**
76 * Create OCh signal from channel number. 99 * Create OCh signal from channel number.
77 * 100 *
78 * @param channel channel number 101 * @param channel channel number
...@@ -170,10 +193,9 @@ public class OchSignal implements Lambda { ...@@ -170,10 +193,9 @@ public class OchSignal implements Lambda {
170 ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() / 193 ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() /
171 ChannelSpacing.CHL_6P25GHZ.frequency().asHz()); 194 ChannelSpacing.CHL_6P25GHZ.frequency().asHz());
172 195
173 - Supplier<SortedSet<OchSignal>> supplier = () -> new TreeSet<>(new DefaultOchSignalComparator());
174 return IntStream.range(0, ochSignal.slotGranularity()) 196 return IntStream.range(0, ochSignal.slotGranularity())
175 .mapToObj(i -> new OchSignal(GridType.FLEX, ChannelSpacing.CHL_6P25GHZ, startMultiplier + 2 * i, 1)) 197 .mapToObj(i -> new OchSignal(GridType.FLEX, ChannelSpacing.CHL_6P25GHZ, startMultiplier + 2 * i, 1))
176 - .collect(Collectors.toCollection(supplier)); 198 + .collect(Collectors.toCollection(DefaultOchSignalComparator::newOchSignalTreeSet));
177 } 199 }
178 200
179 /** 201 /**
...@@ -199,7 +221,10 @@ public class OchSignal implements Lambda { ...@@ -199,7 +221,10 @@ public class OchSignal implements Lambda {
199 checkArgument(Spectrum.CENTER_FREQUENCY.subtract(center).asHz() % spacing.frequency().asHz() == 0); 221 checkArgument(Spectrum.CENTER_FREQUENCY.subtract(center).asHz() % spacing.frequency().asHz() == 0);
200 222
201 // Multiplier sits in middle of given lambdas, then convert from 6.25 to requested spacing 223 // Multiplier sits in middle of given lambdas, then convert from 6.25 to requested spacing
202 - int spacingMultiplier = (lambdas.get(ratio / 2).spacingMultiplier() + 1) / (ratio * 2); 224 + int spacingMultiplier = lambdas.stream()
225 + .mapToInt(OchSignal::spacingMultiplier)
226 + .sum() / lambdas.size()
227 + / (int) (spacing.frequency().asHz() / CHL_6P25GHZ.frequency().asHz());
203 228
204 return new OchSignal(GridType.DWDM, spacing, spacingMultiplier, lambdas.size()); 229 return new OchSignal(GridType.DWDM, spacing, spacingMultiplier, lambdas.size());
205 } 230 }
......
...@@ -15,7 +15,18 @@ ...@@ -15,7 +15,18 @@
15 */ 15 */
16 package org.onosproject.net; 16 package org.onosproject.net;
17 17
18 +import com.google.common.collect.ImmutableList;
19 +import com.google.common.collect.Lists;
18 import com.google.common.testing.EqualsTester; 20 import com.google.common.testing.EqualsTester;
21 +
22 +import static org.junit.Assert.*;
23 +import static org.onosproject.net.ChannelSpacing.CHL_25GHZ;
24 +import static org.onosproject.net.ChannelSpacing.CHL_50GHZ;
25 +import static org.onosproject.net.DefaultOchSignalComparator.newOchSignalTreeSet;
26 +import static org.onosproject.net.OchSignal.newDwdmSlot;
27 +import static org.onosproject.net.OchSignal.newFlexGridSlot;
28 +
29 +import java.util.SortedSet;
19 import org.junit.Test; 30 import org.junit.Test;
20 31
21 /** 32 /**
...@@ -23,16 +34,175 @@ import org.junit.Test; ...@@ -23,16 +34,175 @@ import org.junit.Test;
23 */ 34 */
24 public class OchSignalTest { 35 public class OchSignalTest {
25 36
26 - private final Lambda och1 = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1);
27 - private final Lambda sameOch1 = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1);
28 - private final Lambda och2 = Lambda.ochSignal(GridType.CWDM, ChannelSpacing.CHL_6P25GHZ, 4, 1);
29 - private final Lambda sameOch2 = Lambda.ochSignal(GridType.CWDM, ChannelSpacing.CHL_6P25GHZ, 4, 1);
30 -
31 @Test 37 @Test
32 public void testEquality() { 38 public void testEquality() {
39 + OchSignal och1 = newDwdmSlot(ChannelSpacing.CHL_100GHZ, 1);
40 + OchSignal sameOch1 = newDwdmSlot(ChannelSpacing.CHL_100GHZ, 1);
41 + OchSignal och2 = new OchSignal(GridType.CWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
42 + OchSignal sameOch2 = new OchSignal(GridType.CWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
43 + OchSignal och3 = newDwdmSlot(ChannelSpacing.CHL_100GHZ, 3);
44 + OchSignal sameOch3 = newDwdmSlot(ChannelSpacing.CHL_100GHZ, 3);
45 + OchSignal och4 = newFlexGridSlot(3);
46 + OchSignal sameOch4 = newFlexGridSlot(3);
47 +
33 new EqualsTester() 48 new EqualsTester()
34 .addEqualityGroup(och1, sameOch1) 49 .addEqualityGroup(och1, sameOch1)
35 .addEqualityGroup(och2, sameOch2) 50 .addEqualityGroup(och2, sameOch2)
51 + .addEqualityGroup(och3, sameOch3)
52 + .addEqualityGroup(och4, sameOch4)
36 .testEquals(); 53 .testEquals();
37 } 54 }
55 +
56 + @Test
57 + public void testToFlexgrid50() {
58 + OchSignal input = newDwdmSlot(CHL_50GHZ, 0);
59 + SortedSet<OchSignal> expected = newOchSignalTreeSet();
60 + expected.addAll(ImmutableList.of(
61 + newFlexGridSlot(-3), newFlexGridSlot(-1),
62 + newFlexGridSlot(+1), newFlexGridSlot(+3)));
63 +
64 + SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
65 +
66 + assertEquals(expected, flexGrid);
67 + }
68 +
69 + @Test
70 + public void testToFlexgrid50Plus1() {
71 + OchSignal input = newDwdmSlot(CHL_50GHZ, 1);
72 + SortedSet<OchSignal> expected = newOchSignalTreeSet();
73 + // Note: 8 = 50Ghz / 6.25Ghz
74 + expected.addAll(ImmutableList.of(
75 + newFlexGridSlot(8 - 3), newFlexGridSlot(8 - 1),
76 + newFlexGridSlot(8 + 1), newFlexGridSlot(8 + 3)));
77 +
78 + SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
79 +
80 + assertEquals(expected, flexGrid);
81 + }
82 +
83 + @Test
84 + public void testToFlexgrid50minus1() {
85 + OchSignal input = newDwdmSlot(CHL_50GHZ, -1);
86 + SortedSet<OchSignal> expected = newOchSignalTreeSet();
87 + // Note: 8 = 50Ghz / 6.25Ghz
88 + expected.addAll(ImmutableList.of(
89 + newFlexGridSlot(-8 - 3), newFlexGridSlot(-8 - 1),
90 + newFlexGridSlot(-8 + 1), newFlexGridSlot(-8 + 3)));
91 +
92 + SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
93 +
94 + assertEquals(expected, flexGrid);
95 + }
96 +
97 + @Test
98 + public void testToFlexgrid25() {
99 + OchSignal input = newDwdmSlot(CHL_25GHZ, 0);
100 + SortedSet<OchSignal> expected = newOchSignalTreeSet();
101 + expected.addAll(ImmutableList.of(
102 + newFlexGridSlot(-1),
103 + newFlexGridSlot(+1)));
104 +
105 + SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
106 +
107 + assertEquals(expected, flexGrid);
108 + }
109 +
110 + @Test
111 + public void testToFlexgrid25Plus2() {
112 + OchSignal input = newDwdmSlot(CHL_25GHZ, 2);
113 + SortedSet<OchSignal> expected = newOchSignalTreeSet();
114 + // Note: 8 = 25Ghz / 6.25Ghz * 2
115 + expected.addAll(ImmutableList.of(
116 + newFlexGridSlot(8 - 1),
117 + newFlexGridSlot(8 + 1)));
118 +
119 + SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
120 +
121 + assertEquals(expected, flexGrid);
122 + }
123 +
124 + @Test
125 + public void testToFlexgrid25minus2() {
126 + OchSignal input = newDwdmSlot(CHL_25GHZ, -2);
127 + SortedSet<OchSignal> expected = newOchSignalTreeSet();
128 + // Note: 8 = 50Ghz / 6.25Ghz * 2
129 + expected.addAll(ImmutableList.of(
130 + newFlexGridSlot(-8 - 1),
131 + newFlexGridSlot(-8 + 1)));
132 +
133 + SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
134 +
135 + assertEquals(expected, flexGrid);
136 + }
137 +
138 + @Test
139 + public void testToFixedgrid50() {
140 + SortedSet<OchSignal> input = newOchSignalTreeSet();
141 + input.addAll(ImmutableList.of(
142 + newFlexGridSlot(-3), newFlexGridSlot(-1),
143 + newFlexGridSlot(+1), newFlexGridSlot(+3)));
144 +
145 + OchSignal expected = newDwdmSlot(CHL_50GHZ, 0);
146 + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_50GHZ));
147 + }
148 +
149 + @Test
150 + public void testToFixedgrid50plus1() {
151 + SortedSet<OchSignal> input = newOchSignalTreeSet();
152 + // Note: 8 = 50Ghz / 6.25Ghz
153 + input.addAll(ImmutableList.of(
154 + newFlexGridSlot(8 - 3), newFlexGridSlot(8 - 1),
155 + newFlexGridSlot(8 + 1), newFlexGridSlot(8 + 3)));
156 +
157 + OchSignal expected = newDwdmSlot(CHL_50GHZ, 1);
158 + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_50GHZ));
159 + }
160 +
161 + @Test
162 + public void testToFixedgrid50minus1() {
163 + SortedSet<OchSignal> input = newOchSignalTreeSet();
164 + // Note: 8 = 50Ghz / 6.25Ghz
165 + input.addAll(ImmutableList.of(
166 + newFlexGridSlot(-8 - 3), newFlexGridSlot(-8 - 1),
167 + newFlexGridSlot(-8 + 1), newFlexGridSlot(-8 + 3)));
168 +
169 + OchSignal expected = newDwdmSlot(CHL_50GHZ, -1);
170 + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_50GHZ));
171 + }
172 +
173 + @Test
174 + public void testToFixedgrid25() {
175 + SortedSet<OchSignal> input = newOchSignalTreeSet();
176 + input.addAll(ImmutableList.of(
177 + newFlexGridSlot(-1),
178 + newFlexGridSlot(+1)));
179 +
180 + OchSignal expected = newDwdmSlot(CHL_25GHZ, 0);
181 + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_25GHZ));
182 + }
183 +
184 + @Test
185 + public void testToFixedgrid25plus2() {
186 + SortedSet<OchSignal> input = newOchSignalTreeSet();
187 + // Note: 8 = 25Ghz / 6.25Ghz * 2
188 + input.addAll(ImmutableList.of(
189 + newFlexGridSlot(8 - 1),
190 + newFlexGridSlot(8 + 1)));
191 +
192 + OchSignal expected = newDwdmSlot(CHL_25GHZ, 2);
193 + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_25GHZ));
194 + }
195 +
196 + @Test
197 + public void testToFixedgrid25minus2() {
198 + SortedSet<OchSignal> input = newOchSignalTreeSet();
199 + // Note: 8 = 25Ghz / 6.25Ghz * 2
200 + input.addAll(ImmutableList.of(
201 + newFlexGridSlot(-8 - 1),
202 + newFlexGridSlot(-8 + 1)));
203 +
204 + OchSignal expected = newDwdmSlot(CHL_25GHZ, -2);
205 + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_25GHZ));
206 + }
207 +
38 } 208 }
......