[Emu] Register ODU TributarySlots on OCH ports

Change-Id: Iff6010259485f2402f1b645de8f83af5627bee3c
diff --git a/core/api/src/main/java/org/onosproject/net/TributarySlot.java b/core/api/src/main/java/org/onosproject/net/TributarySlot.java
new file mode 100644
index 0000000..d8a10c8
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/TributarySlot.java
@@ -0,0 +1,73 @@
+/*
+ * 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 ODU Tributary Slot simply designated by an index number of slot.
+ */
+public class TributarySlot {
+
+    private final long index;
+
+    /**
+     * Creates an instance representing the TributarySlot specified by the given index number.
+     *
+     * @param index index number of wavelength
+     */
+    public TributarySlot(long index) {
+        this.index = index;
+    }
+
+    public static TributarySlot of(long index) {
+        return new TributarySlot(index);
+    }
+
+    /**
+     * Returns the index number of TributarySlot.
+     *
+     * @return the index number of TributarySlot
+     */
+    public long index() {
+        return index;
+    }
+
+    @Override
+    public int hashCode() {
+        return Long.hashCode(index);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof TributarySlot)) {
+            return false;
+        }
+
+        final TributarySlot that = (TributarySlot) obj;
+        return this.index == that.index;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("index", index)
+                .toString();
+    }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java
index 066dd33..114c4c7 100644
--- a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java
+++ b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java
@@ -17,12 +17,20 @@
 
 import org.onosproject.net.Device;
 import org.onosproject.net.Port;
+import org.onosproject.net.OchPort;
+import org.onosproject.net.TributarySlot;
+import org.onosproject.net.OduSignalType;
 import org.onosproject.net.device.DeviceEvent;
 import org.onosproject.net.device.DeviceListener;
 import org.onosproject.net.newresource.ResourceAdminService;
 import org.onosproject.net.newresource.ResourcePath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+import java.util.List;
 import java.util.concurrent.ExecutorService;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
@@ -31,6 +39,13 @@
  */
 final class ResourceDeviceListener implements DeviceListener {
 
+    private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
+
+    private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8;
+    private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80;
+    private static final List<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots();
+    private static final List<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots();
+
     private final ResourceAdminService adminService;
     private final ExecutorService executor;
 
@@ -76,11 +91,50 @@
 
     private void registerPortResource(Device device, Port port) {
         ResourcePath parent = ResourcePath.discrete(device.id());
-        executor.submit(() -> adminService.registerResources(parent, port.number()));
+        executor.submit(() -> registerPortResource(device, port, parent));
+    }
+
+    private void registerPortResource(Device device, Port port, ResourcePath parent) {
+        adminService.registerResources(parent, port.number());
+        ResourcePath portPath = ResourcePath.discrete(device.id(), port.number());
+
+        switch (port.type()) {
+            case OCH:
+                // register ODU TributarySlots against the OCH port
+                registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
+                break;
+            default:
+                break;
+        }
+    }
+
+    private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) {
+        switch (oduSignalType) {
+            case ODU2:
+                adminService.registerResources(portPath, ENTIRE_ODU2_TRIBUTARY_SLOTS);
+                break;
+            case ODU4:
+                adminService.registerResources(portPath, ENTIRE_ODU4_TRIBUTARY_SLOTS);
+                break;
+            default:
+                break;
+        }
     }
 
     private void unregisterPortResource(Device device, Port port) {
         ResourcePath parent = ResourcePath.discrete(device.id());
         executor.submit(() -> adminService.unregisterResources(parent, port.number()));
     }
+
+    private static List<TributarySlot> getEntireOdu2TributarySlots() {
+        return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS)
+                .mapToObj(x -> TributarySlot.of(x))
+                .collect(Collectors.toList());
+    }
+    private static List<TributarySlot> getEntireOdu4TributarySlots() {
+        return IntStream.rangeClosed(1, TOTAL_ODU4_TRIBUTARY_SLOTS)
+                .mapToObj(x -> TributarySlot.of(x))
+                .collect(Collectors.toList());
+    }
+
 }
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 6f96498..e0764eb 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
@@ -75,6 +75,7 @@
 import org.onosproject.net.OmsPort;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
+import org.onosproject.net.TributarySlot;
 import org.onosproject.net.device.DefaultDeviceDescription;
 import org.onosproject.net.device.DefaultPortDescription;
 import org.onosproject.net.device.DefaultPortStatistics;
@@ -476,6 +477,7 @@
             .register(OduCltPortDescription.class)
             .register(OchPortDescription.class)
             .register(OmsPortDescription.class)
+            .register(TributarySlot.class)
             .register(
                     MplsIntent.class,
                     MplsPathIntent.class,