From ca8cb4e33739f35923fcd17b8394ca8d8532233c Mon Sep 17 00:00:00 2001 From: HIGUCHI Yuta Date: Fri, 11 Dec 2015 13:57:05 -0800 Subject: [PATCH] Unit test for OchSignal Change-Id: If77042f60c1b95889b9442cdda453a44c10c1ef4 --- .../net/DefaultOchSignalComparator.java | 12 ++ .../java/org/onosproject/net/OchSignal.java | 35 +++- .../org/onosproject/net/OchSignalTest.java | 180 +++++++++++++++++- 3 files changed, 217 insertions(+), 10 deletions(-) diff --git a/core/api/src/main/java/org/onosproject/net/DefaultOchSignalComparator.java b/core/api/src/main/java/org/onosproject/net/DefaultOchSignalComparator.java index e605dcfdba..ebc89ab031 100644 --- a/core/api/src/main/java/org/onosproject/net/DefaultOchSignalComparator.java +++ b/core/api/src/main/java/org/onosproject/net/DefaultOchSignalComparator.java @@ -16,6 +16,7 @@ package org.onosproject.net; import java.util.Comparator; +import java.util.TreeSet; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -24,6 +25,17 @@ import static com.google.common.base.Preconditions.checkNotNull; * Comparator implementation for OchSignal. Assumes identical grid type and channel spacing. */ public class DefaultOchSignalComparator implements Comparator { + + private static final DefaultOchSignalComparator INSTANCE = new DefaultOchSignalComparator(); + + /** + * Creates a new instance of {@link TreeSet} using this Comparator. + * @return {@link TreeSet} + */ + public static TreeSet newOchSignalTreeSet() { + return new TreeSet<>(INSTANCE); + } + @Override public int compare(OchSignal o1, OchSignal o2) { checkNotNull(o1.gridType()); diff --git a/core/api/src/main/java/org/onosproject/net/OchSignal.java b/core/api/src/main/java/org/onosproject/net/OchSignal.java index dfb36aeb5b..fc60aaa257 100644 --- a/core/api/src/main/java/org/onosproject/net/OchSignal.java +++ b/core/api/src/main/java/org/onosproject/net/OchSignal.java @@ -24,19 +24,22 @@ import java.util.List; import java.util.Objects; import java.util.Set; import java.util.SortedSet; -import java.util.TreeSet; -import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.IntStream; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static org.onosproject.net.ChannelSpacing.CHL_12P5GHZ; +import static org.onosproject.net.ChannelSpacing.CHL_6P25GHZ; +import static org.onosproject.net.GridType.DWDM; +import static org.onosproject.net.GridType.FLEX; /** * Implementation of Lambda representing OCh (Optical Channel) Signal. * *

* See ITU G.709 "Interfaces for the Optical Transport Network (OTN)". + * ITU G.694.1 "Spectral grids for WDM applications: DWDM frequency grid". *

*/ public class OchSignal implements Lambda { @@ -72,6 +75,26 @@ public class OchSignal implements Lambda { this.slotGranularity = slotGranularity; } + /** + * Creates an instance of {@link OchSignal} representing a flex grid frequency slot. + * @param index slot index (relative to "the center frequency" 193.1THz) + * @return FlexGrid {@link OchSignal} + */ + public static OchSignal newFlexGridSlot(int index) { + return new OchSignal(FLEX, CHL_6P25GHZ, index, 1); + } + + /** + * Creates an instance of {@link OchSignal} representing a fixed DWDM frequency slot. + * @param spacing channel spacing + * @param index slot index (relative to "the center frequency" 193.1THz) + * @return DWDM {@link OchSignal} + */ + public static OchSignal newDwdmSlot(ChannelSpacing spacing, int index) { + return new OchSignal(DWDM, spacing, index, + (int) (spacing.frequency().asHz() / CHL_12P5GHZ.frequency().asHz())); + } + /** * Create OCh signal from channel number. * @@ -170,10 +193,9 @@ public class OchSignal implements Lambda { ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() / ChannelSpacing.CHL_6P25GHZ.frequency().asHz()); - Supplier> supplier = () -> new TreeSet<>(new DefaultOchSignalComparator()); return IntStream.range(0, ochSignal.slotGranularity()) .mapToObj(i -> new OchSignal(GridType.FLEX, ChannelSpacing.CHL_6P25GHZ, startMultiplier + 2 * i, 1)) - .collect(Collectors.toCollection(supplier)); + .collect(Collectors.toCollection(DefaultOchSignalComparator::newOchSignalTreeSet)); } /** @@ -199,7 +221,10 @@ public class OchSignal implements Lambda { checkArgument(Spectrum.CENTER_FREQUENCY.subtract(center).asHz() % spacing.frequency().asHz() == 0); // Multiplier sits in middle of given lambdas, then convert from 6.25 to requested spacing - int spacingMultiplier = (lambdas.get(ratio / 2).spacingMultiplier() + 1) / (ratio * 2); + int spacingMultiplier = lambdas.stream() + .mapToInt(OchSignal::spacingMultiplier) + .sum() / lambdas.size() + / (int) (spacing.frequency().asHz() / CHL_6P25GHZ.frequency().asHz()); return new OchSignal(GridType.DWDM, spacing, spacingMultiplier, lambdas.size()); } diff --git a/core/api/src/test/java/org/onosproject/net/OchSignalTest.java b/core/api/src/test/java/org/onosproject/net/OchSignalTest.java index 1ab1729322..3872d19ae9 100644 --- a/core/api/src/test/java/org/onosproject/net/OchSignalTest.java +++ b/core/api/src/test/java/org/onosproject/net/OchSignalTest.java @@ -15,7 +15,18 @@ */ package org.onosproject.net; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; import com.google.common.testing.EqualsTester; + +import static org.junit.Assert.*; +import static org.onosproject.net.ChannelSpacing.CHL_25GHZ; +import static org.onosproject.net.ChannelSpacing.CHL_50GHZ; +import static org.onosproject.net.DefaultOchSignalComparator.newOchSignalTreeSet; +import static org.onosproject.net.OchSignal.newDwdmSlot; +import static org.onosproject.net.OchSignal.newFlexGridSlot; + +import java.util.SortedSet; import org.junit.Test; /** @@ -23,16 +34,175 @@ import org.junit.Test; */ public class OchSignalTest { - private final Lambda och1 = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1); - private final Lambda sameOch1 = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1); - private final Lambda och2 = Lambda.ochSignal(GridType.CWDM, ChannelSpacing.CHL_6P25GHZ, 4, 1); - private final Lambda sameOch2 = Lambda.ochSignal(GridType.CWDM, ChannelSpacing.CHL_6P25GHZ, 4, 1); - @Test public void testEquality() { + OchSignal och1 = newDwdmSlot(ChannelSpacing.CHL_100GHZ, 1); + OchSignal sameOch1 = newDwdmSlot(ChannelSpacing.CHL_100GHZ, 1); + OchSignal och2 = new OchSignal(GridType.CWDM, ChannelSpacing.CHL_100GHZ, 4, 8); + OchSignal sameOch2 = new OchSignal(GridType.CWDM, ChannelSpacing.CHL_100GHZ, 4, 8); + OchSignal och3 = newDwdmSlot(ChannelSpacing.CHL_100GHZ, 3); + OchSignal sameOch3 = newDwdmSlot(ChannelSpacing.CHL_100GHZ, 3); + OchSignal och4 = newFlexGridSlot(3); + OchSignal sameOch4 = newFlexGridSlot(3); + new EqualsTester() .addEqualityGroup(och1, sameOch1) .addEqualityGroup(och2, sameOch2) + .addEqualityGroup(och3, sameOch3) + .addEqualityGroup(och4, sameOch4) .testEquals(); } + + @Test + public void testToFlexgrid50() { + OchSignal input = newDwdmSlot(CHL_50GHZ, 0); + SortedSet expected = newOchSignalTreeSet(); + expected.addAll(ImmutableList.of( + newFlexGridSlot(-3), newFlexGridSlot(-1), + newFlexGridSlot(+1), newFlexGridSlot(+3))); + + SortedSet flexGrid = OchSignal.toFlexGrid(input); + + assertEquals(expected, flexGrid); + } + + @Test + public void testToFlexgrid50Plus1() { + OchSignal input = newDwdmSlot(CHL_50GHZ, 1); + SortedSet expected = newOchSignalTreeSet(); + // Note: 8 = 50Ghz / 6.25Ghz + expected.addAll(ImmutableList.of( + newFlexGridSlot(8 - 3), newFlexGridSlot(8 - 1), + newFlexGridSlot(8 + 1), newFlexGridSlot(8 + 3))); + + SortedSet flexGrid = OchSignal.toFlexGrid(input); + + assertEquals(expected, flexGrid); + } + + @Test + public void testToFlexgrid50minus1() { + OchSignal input = newDwdmSlot(CHL_50GHZ, -1); + SortedSet expected = newOchSignalTreeSet(); + // Note: 8 = 50Ghz / 6.25Ghz + expected.addAll(ImmutableList.of( + newFlexGridSlot(-8 - 3), newFlexGridSlot(-8 - 1), + newFlexGridSlot(-8 + 1), newFlexGridSlot(-8 + 3))); + + SortedSet flexGrid = OchSignal.toFlexGrid(input); + + assertEquals(expected, flexGrid); + } + + @Test + public void testToFlexgrid25() { + OchSignal input = newDwdmSlot(CHL_25GHZ, 0); + SortedSet expected = newOchSignalTreeSet(); + expected.addAll(ImmutableList.of( + newFlexGridSlot(-1), + newFlexGridSlot(+1))); + + SortedSet flexGrid = OchSignal.toFlexGrid(input); + + assertEquals(expected, flexGrid); + } + + @Test + public void testToFlexgrid25Plus2() { + OchSignal input = newDwdmSlot(CHL_25GHZ, 2); + SortedSet expected = newOchSignalTreeSet(); + // Note: 8 = 25Ghz / 6.25Ghz * 2 + expected.addAll(ImmutableList.of( + newFlexGridSlot(8 - 1), + newFlexGridSlot(8 + 1))); + + SortedSet flexGrid = OchSignal.toFlexGrid(input); + + assertEquals(expected, flexGrid); + } + + @Test + public void testToFlexgrid25minus2() { + OchSignal input = newDwdmSlot(CHL_25GHZ, -2); + SortedSet expected = newOchSignalTreeSet(); + // Note: 8 = 50Ghz / 6.25Ghz * 2 + expected.addAll(ImmutableList.of( + newFlexGridSlot(-8 - 1), + newFlexGridSlot(-8 + 1))); + + SortedSet flexGrid = OchSignal.toFlexGrid(input); + + assertEquals(expected, flexGrid); + } + + @Test + public void testToFixedgrid50() { + SortedSet input = newOchSignalTreeSet(); + input.addAll(ImmutableList.of( + newFlexGridSlot(-3), newFlexGridSlot(-1), + newFlexGridSlot(+1), newFlexGridSlot(+3))); + + OchSignal expected = newDwdmSlot(CHL_50GHZ, 0); + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_50GHZ)); + } + + @Test + public void testToFixedgrid50plus1() { + SortedSet input = newOchSignalTreeSet(); + // Note: 8 = 50Ghz / 6.25Ghz + input.addAll(ImmutableList.of( + newFlexGridSlot(8 - 3), newFlexGridSlot(8 - 1), + newFlexGridSlot(8 + 1), newFlexGridSlot(8 + 3))); + + OchSignal expected = newDwdmSlot(CHL_50GHZ, 1); + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_50GHZ)); + } + + @Test + public void testToFixedgrid50minus1() { + SortedSet input = newOchSignalTreeSet(); + // Note: 8 = 50Ghz / 6.25Ghz + input.addAll(ImmutableList.of( + newFlexGridSlot(-8 - 3), newFlexGridSlot(-8 - 1), + newFlexGridSlot(-8 + 1), newFlexGridSlot(-8 + 3))); + + OchSignal expected = newDwdmSlot(CHL_50GHZ, -1); + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_50GHZ)); + } + + @Test + public void testToFixedgrid25() { + SortedSet input = newOchSignalTreeSet(); + input.addAll(ImmutableList.of( + newFlexGridSlot(-1), + newFlexGridSlot(+1))); + + OchSignal expected = newDwdmSlot(CHL_25GHZ, 0); + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_25GHZ)); + } + + @Test + public void testToFixedgrid25plus2() { + SortedSet input = newOchSignalTreeSet(); + // Note: 8 = 25Ghz / 6.25Ghz * 2 + input.addAll(ImmutableList.of( + newFlexGridSlot(8 - 1), + newFlexGridSlot(8 + 1))); + + OchSignal expected = newDwdmSlot(CHL_25GHZ, 2); + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_25GHZ)); + } + + @Test + public void testToFixedgrid25minus2() { + SortedSet input = newOchSignalTreeSet(); + // Note: 8 = 25Ghz / 6.25Ghz * 2 + input.addAll(ImmutableList.of( + newFlexGridSlot(-8 - 1), + newFlexGridSlot(-8 + 1))); + + OchSignal expected = newDwdmSlot(CHL_25GHZ, -2); + assertEquals(expected, OchSignal.toFixedGrid(Lists.newArrayList(input), CHL_25GHZ)); + } + }