Unit test for OchSignal

Change-Id: If77042f60c1b95889b9442cdda453a44c10c1ef4
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 e605dcf..ebc89ab 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 @@
  * Comparator implementation for OchSignal. Assumes identical grid type and channel spacing.
  */
 public class DefaultOchSignalComparator implements Comparator<OchSignal> {
+
+    private static final DefaultOchSignalComparator INSTANCE = new DefaultOchSignalComparator();
+
+    /**
+     * Creates a new instance of {@link TreeSet} using this Comparator.
+     * @return {@link TreeSet}
+     */
+    public static TreeSet<OchSignal> 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 dfb36ae..fc60aaa 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.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.
  *
  * <p>
  * See ITU G.709 "Interfaces for the Optical Transport Network (OTN)".
+ *     ITU G.694.1 "Spectral grids for WDM applications: DWDM frequency grid".
  * </p>
  */
 public class OchSignal implements Lambda {
@@ -73,6 +76,26 @@
     }
 
     /**
+     * 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.
      *
      * @param channel channel number
@@ -170,10 +193,9 @@
                 ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() /
                         ChannelSpacing.CHL_6P25GHZ.frequency().asHz());
 
-        Supplier<SortedSet<OchSignal>> 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 @@
         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 1ab1729..3872d19 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 @@
  */
 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<OchSignal> expected = newOchSignalTreeSet();
+        expected.addAll(ImmutableList.of(
+                    newFlexGridSlot(-3), newFlexGridSlot(-1),
+                    newFlexGridSlot(+1), newFlexGridSlot(+3)));
+
+        SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
+
+        assertEquals(expected, flexGrid);
+    }
+
+    @Test
+    public void testToFlexgrid50Plus1() {
+        OchSignal input = newDwdmSlot(CHL_50GHZ, 1);
+        SortedSet<OchSignal> expected = newOchSignalTreeSet();
+        // Note: 8 = 50Ghz / 6.25Ghz
+        expected.addAll(ImmutableList.of(
+                    newFlexGridSlot(8 - 3), newFlexGridSlot(8 - 1),
+                    newFlexGridSlot(8 + 1), newFlexGridSlot(8 + 3)));
+
+        SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
+
+        assertEquals(expected, flexGrid);
+    }
+
+    @Test
+    public void testToFlexgrid50minus1() {
+        OchSignal input = newDwdmSlot(CHL_50GHZ, -1);
+        SortedSet<OchSignal> expected = newOchSignalTreeSet();
+        // Note: 8 = 50Ghz / 6.25Ghz
+        expected.addAll(ImmutableList.of(
+                    newFlexGridSlot(-8 - 3), newFlexGridSlot(-8 - 1),
+                    newFlexGridSlot(-8 + 1), newFlexGridSlot(-8 + 3)));
+
+        SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
+
+        assertEquals(expected, flexGrid);
+    }
+
+    @Test
+    public void testToFlexgrid25() {
+        OchSignal input = newDwdmSlot(CHL_25GHZ, 0);
+        SortedSet<OchSignal> expected = newOchSignalTreeSet();
+        expected.addAll(ImmutableList.of(
+                    newFlexGridSlot(-1),
+                    newFlexGridSlot(+1)));
+
+        SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
+
+        assertEquals(expected, flexGrid);
+    }
+
+    @Test
+    public void testToFlexgrid25Plus2() {
+        OchSignal input = newDwdmSlot(CHL_25GHZ, 2);
+        SortedSet<OchSignal> expected = newOchSignalTreeSet();
+        // Note: 8 = 25Ghz / 6.25Ghz * 2
+        expected.addAll(ImmutableList.of(
+                    newFlexGridSlot(8 - 1),
+                    newFlexGridSlot(8 + 1)));
+
+        SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
+
+        assertEquals(expected, flexGrid);
+    }
+
+    @Test
+    public void testToFlexgrid25minus2() {
+        OchSignal input = newDwdmSlot(CHL_25GHZ, -2);
+        SortedSet<OchSignal> expected = newOchSignalTreeSet();
+        // Note: 8 = 50Ghz / 6.25Ghz * 2
+        expected.addAll(ImmutableList.of(
+                    newFlexGridSlot(-8 - 1),
+                    newFlexGridSlot(-8 + 1)));
+
+        SortedSet<OchSignal> flexGrid = OchSignal.toFlexGrid(input);
+
+        assertEquals(expected, flexGrid);
+    }
+
+    @Test
+    public void testToFixedgrid50() {
+        SortedSet<OchSignal> 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<OchSignal> 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<OchSignal> 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<OchSignal> 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<OchSignal> 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<OchSignal> 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));
+    }
+
 }