Define Lambda interface representing wavelength and implementations

Two Lambda implementations are defined.
- IndexedLambda
- OchSignal

Change-Id: I36763390f61abd9f861e76edab63f558ae97e8a2
diff --git a/core/api/src/main/java/org/onosproject/net/IndexedLambda.java b/core/api/src/main/java/org/onosproject/net/IndexedLambda.java
new file mode 100644
index 0000000..e350b47
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/IndexedLambda.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Implementation of Lambda simply designated by an index number of wavelength.
+ */
+public class IndexedLambda implements Lambda {
+
+    private final long lambda;
+
+    /**
+     * Creates an instance representing the wavelength specified by the given index number.
+     *
+     * @param lambda index number of wavelength
+     */
+    IndexedLambda(long lambda) {
+        this.lambda = lambda;
+    }
+
+    @Override
+    public int hashCode() {
+        return (int) (lambda ^ (lambda >>> 32));
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof IndexedLambda)) {
+            return false;
+        }
+
+        final IndexedLambda that = (IndexedLambda) obj;
+        return this.lambda == that.lambda;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("lambda", lambda)
+                .toString();
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/Lambda.java b/core/api/src/main/java/org/onosproject/net/Lambda.java
new file mode 100644
index 0000000..d0995be
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/Lambda.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net;
+
+/**
+ * Abstraction of wavelength. Currently, this is just a marker interface
+ */
+public interface Lambda {
+    /**
+     * Create an Lambda instance with the specified wavelength index number.
+     *
+     * @param lambda index number
+     * @return an instance
+     */
+    static Lambda indexedLambda(long lambda) {
+        return new IndexedLambda(lambda);
+    }
+
+    /**
+     * Creates a Lambda instance with the specified arguments.
+     *
+     * @param gridType          grid type
+     * @param channelSpacing    channel spacing
+     * @param spacingMultiplier channel spacing multiplier
+     * @param slotGranularity   slot width granularity
+     */
+    static Lambda ochSignal(GridType gridType, ChannelSpacing channelSpacing,
+                            int spacingMultiplier, int slotGranularity) {
+        return new OchSignal(gridType, channelSpacing, spacingMultiplier, slotGranularity);
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/OchSignal.java b/core/api/src/main/java/org/onosproject/net/OchSignal.java
new file mode 100644
index 0000000..3f6c14f
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/OchSignal.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net;
+
+import com.google.common.base.MoreObjects;
+import org.onlab.util.Frequency;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Implementation of Lambda representing OCh (Optical Channel) Signal.
+ *
+ * <p>
+ * See ITU G.709 "Interfaces for the Optical Transport Network (OTN)".
+ * </p>
+ */
+// TODO: consider which is better, OchSignal or OpticalChannelSignal
+public class OchSignal implements Lambda {
+
+    private static final Frequency CENTER_FREQUENCY = Frequency.ofTHz(193.1);
+    private static final Frequency FLEX_GRID_SLOT = Frequency.ofGHz(12.5);
+
+    private final GridType gridType;
+    private final ChannelSpacing channelSpacing;
+    // Frequency = 193.1 THz + spacingMultiplier * channelSpacing
+    private final int spacingMultiplier;
+    // Slot width = slotGranularity * 12.5 GHz
+    private final int slotGranularity;
+
+    /**
+     * Creates an instance with the specified arguments.
+     *
+     * @param gridType          grid type
+     * @param channelSpacing    channel spacing
+     * @param spacingMultiplier channel spacing multiplier
+     * @param slotGranularity   slot width granularity
+     */
+    OchSignal(GridType gridType, ChannelSpacing channelSpacing,
+              int spacingMultiplier, int slotGranularity) {
+        this.gridType = checkNotNull(gridType);
+        this.channelSpacing = checkNotNull(channelSpacing);
+        // TODO: check the precondition for spacingMultiplier. Is negative value permitted?
+        this.spacingMultiplier = spacingMultiplier;
+
+        checkArgument(slotGranularity > 0, "slotGranularity must be more than 0, but %s", slotGranularity);
+        this.slotGranularity = slotGranularity;
+    }
+
+    /**
+     * Returns grid type.
+     *
+     * @return grid type
+     */
+    public GridType gridType() {
+        return gridType;
+    }
+
+    /**
+     * Returns channel spacing.
+     *
+     * @return channel spacing
+     */
+    public ChannelSpacing channelSpacing() {
+        return channelSpacing;
+    }
+
+    /**
+     * Returns spacing multiplier.
+     *
+     * @return spacing multiplier
+     */
+    public int spacingMultiplier() {
+        return spacingMultiplier;
+    }
+
+    /**
+     * Returns slow width granularity.
+     *
+     * @return slow width granularity
+     */
+    public int slotGranularity() {
+        return slotGranularity;
+    }
+
+    /**
+     * Returns central frequency in MHz.
+     *
+     * @return frequency in MHz
+     */
+    public Frequency centralFrequency() {
+        return CENTER_FREQUENCY.add(channelSpacing().frequency().multiply(spacingMultiplier));
+    }
+
+    /**
+     * Returns slot width.
+     *
+     * @return slot width
+     */
+    public Frequency slotWidth() {
+        return FLEX_GRID_SLOT.multiply(slotGranularity);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(gridType, channelSpacing, spacingMultiplier, slotGranularity);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof OchSignal)) {
+            return false;
+        }
+        final OchSignal other = (OchSignal) obj;
+        return Objects.equals(this.gridType, other.gridType)
+                && Objects.equals(this.channelSpacing, other.channelSpacing)
+                && Objects.equals(this.spacingMultiplier, other.spacingMultiplier)
+                && Objects.equals(this.slotGranularity, other.slotGranularity);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("gridType", gridType)
+                .add("channelSpacing", channelSpacing)
+                .add("spacingMultiplier", spacingMultiplier)
+                .add("slotGranularity", slotGranularity)
+                .toString();
+    }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/IndexedLambdaTest.java b/core/api/src/test/java/org/onosproject/net/IndexedLambdaTest.java
new file mode 100644
index 0000000..b08d995
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/IndexedLambdaTest.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+public class IndexedLambdaTest {
+    @Test
+    public void testEquality() {
+        new EqualsTester()
+                .addEqualityGroup(Lambda.indexedLambda(10), Lambda.indexedLambda(10))
+                .addEqualityGroup(Lambda.indexedLambda(11), Lambda.indexedLambda(11), Lambda.indexedLambda(11))
+                .testEquals();
+    }
+}
diff --git a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
index d047135..9a8e268 100644
--- a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
+++ b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
@@ -18,7 +18,6 @@
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
-
 import org.onlab.packet.ChassisId;
 import org.onlab.packet.Ip4Address;
 import org.onlab.packet.Ip4Prefix;
@@ -57,14 +56,16 @@
 import org.onosproject.net.GridType;
 import org.onosproject.net.HostId;
 import org.onosproject.net.HostLocation;
+import org.onosproject.net.IndexedLambda;
 import org.onosproject.net.Link;
 import org.onosproject.net.LinkKey;
 import org.onosproject.net.OchPort;
+import org.onosproject.net.OchSignal;
 import org.onosproject.net.OduCltPort;
+import org.onosproject.net.OduSignalType;
 import org.onosproject.net.OmsPort;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
-import org.onosproject.net.OduSignalType;
 import org.onosproject.net.device.DefaultDeviceDescription;
 import org.onosproject.net.device.DefaultPortDescription;
 import org.onosproject.net.flow.CompletedBatchOperation;
@@ -384,6 +385,8 @@
             .register(ChannelSpacing.class)
             .register(OduCltPort.class)
             .register(OduCltPort.SignalType.class)
+            .register(IndexedLambda.class)
+            .register(OchSignal.class)
             .register(
                     MplsIntent.class,
                     MplsPathIntent.class,
diff --git a/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java b/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java
index 388532a..d0e1c6d 100644
--- a/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java
+++ b/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java
@@ -324,6 +324,18 @@
     }
 
     @Test
+    public void testIndexedLambda() {
+        testSerializedEquals(org.onosproject.net.Lambda.indexedLambda(10L));
+    }
+
+    @Test
+    public void testOchSignal() {
+        testSerializedEquals(org.onosproject.net.Lambda.ochSignal(
+                GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1
+        ));
+    }
+
+    @Test
     public void testDefaultLinkResourceRequest() {
         testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
                         .addLambdaRequest()