Use LambdaQuery in OpenFlowDeviceProvider to get details for optical ports and fix OmsPort totalChannels()

Change-Id: I09bee1ad1cbf4b8d7185c2b022ffed4d8b2ef2e7
diff --git a/core/api/src/main/java/org/onosproject/net/optical/OmsPort.java b/core/api/src/main/java/org/onosproject/net/optical/OmsPort.java
index a08ab5f..898fce9 100644
--- a/core/api/src/main/java/org/onosproject/net/optical/OmsPort.java
+++ b/core/api/src/main/java/org/onosproject/net/optical/OmsPort.java
@@ -37,7 +37,7 @@
      */
     default short totalChannels() {
         Frequency diff = maxFrequency().subtract(minFrequency());
-        return (short) (diff.asHz() / grid().asHz());
+        return (short) (diff.asHz() / grid().asHz() + 1);
     }
 
     /**
diff --git a/core/api/src/test/java/org/onosproject/net/optical/device/OmsPortHelperTest.java b/core/api/src/test/java/org/onosproject/net/optical/device/OmsPortHelperTest.java
index c8c68f4..6a309aa 100644
--- a/core/api/src/test/java/org/onosproject/net/optical/device/OmsPortHelperTest.java
+++ b/core/api/src/test/java/org/onosproject/net/optical/device/OmsPortHelperTest.java
@@ -91,7 +91,7 @@
         assertThat(oms.maxFrequency(), is(maxF));
         assertThat(oms.minFrequency(), is(minF));
         assertThat(oms.grid(), is(grid));
-        assertThat("(33-3)/2 = 15", oms.totalChannels(), is((short) 15));
+        assertThat("((33-3)/2)+1 = 16", oms.totalChannels(), is((short) 16));
     }
 
 }
diff --git a/core/api/src/test/java/org/onosproject/net/optical/impl/DefaultOmsPortTest.java b/core/api/src/test/java/org/onosproject/net/optical/impl/DefaultOmsPortTest.java
index 4f32afc..a09d5ec 100644
--- a/core/api/src/test/java/org/onosproject/net/optical/impl/DefaultOmsPortTest.java
+++ b/core/api/src/test/java/org/onosproject/net/optical/impl/DefaultOmsPortTest.java
@@ -115,7 +115,7 @@
         assertThat(oms.maxFrequency(), is(maxFrequency));
         assertThat(oms.minFrequency(), is(minFrequency));
         assertThat(oms.grid(), is(grid));
-        assertThat("(33-3)/2 = 15", oms.totalChannels(), is((short) 15));
+        assertThat("((33-3)/2)+1 = 16", oms.totalChannels(), is((short) 16));
     }
 
 }
diff --git a/drivers/optical/src/main/java/org/onosproject/driver/optical/query/OplinkRoadmLambdaQuery.java b/drivers/optical/src/main/java/org/onosproject/driver/optical/query/OplinkRoadmLambdaQuery.java
new file mode 100644
index 0000000..1aa0896
--- /dev/null
+++ b/drivers/optical/src/main/java/org/onosproject/driver/optical/query/OplinkRoadmLambdaQuery.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2016 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.driver.optical.query;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import org.onosproject.net.ChannelSpacing;
+import org.onosproject.net.OchSignal;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.LambdaQuery;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+
+/**
+ * Lambda query implementation for Oplink ROADM.
+ *
+ * An Oplink ROADM port exposes OMSn resources: 88 lambdas with 50GHz width (fixed grid).
+ *
+ * Channel id: Nominal central frequency = 193.1 THz + spacingMultiplier * channelSpacing).
+ * Channel (-28 to 59): starting from 191.7 THz to 196.05 THz, Increment by 50GHz.
+ */
+
+public class OplinkRoadmLambdaQuery extends AbstractHandlerBehaviour implements LambdaQuery {
+
+    private static final int LAMBDA_COUNT = 88;
+    private static final int CENTER_OFFSET = 29;
+
+    @Override
+    public Set<OchSignal> queryLambdas(PortNumber port) {
+        return IntStream.rangeClosed(1, LAMBDA_COUNT)
+                .mapToObj(x -> OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, x - CENTER_OFFSET))
+                .collect(Collectors.toSet());
+    }
+}
\ No newline at end of file
diff --git a/drivers/optical/src/main/resources/optical-drivers.xml b/drivers/optical/src/main/resources/optical-drivers.xml
index 0047895..c87a8cd 100644
--- a/drivers/optical/src/main/resources/optical-drivers.xml
+++ b/drivers/optical/src/main/resources/optical-drivers.xml
@@ -17,7 +17,7 @@
 <drivers>
     <driver name="linc-oe" extends="default"
             manufacturer="FlowForwarding.org" hwVersion="Unknown"
-            swVersion="LINC-OE OpenFlow Software Switch 1.1">
+            swVersion="LINC(-OE)? OpenFlow Software Switch 1.1">
         <behaviour api="org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver"
                    impl="org.onosproject.driver.optical.handshaker.OfOpticalSwitchImplLinc13"/>
         <behaviour api="org.onosproject.net.behaviour.LambdaQuery"
@@ -52,6 +52,8 @@
             swVersion="of-agent">
         <behaviour api="org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver"
                    impl="org.onosproject.driver.optical.handshaker.OplinkRoadmHandshaker"/>
+        <behaviour api="org.onosproject.net.behaviour.LambdaQuery"
+                   impl="org.onosproject.driver.optical.query.OplinkRoadmLambdaQuery"/>
         <behaviour api="org.onosproject.net.optical.OpticalDevice"
                    impl="org.onosproject.net.optical.DefaultOpticalDevice"/>
     </driver>
diff --git a/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProvider.java b/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProvider.java
index abede47..1f6a888 100644
--- a/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProvider.java
+++ b/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProvider.java
@@ -32,10 +32,12 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
@@ -62,6 +64,7 @@
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.behaviour.LambdaQuery;
 import org.onosproject.net.device.DefaultDeviceDescription;
 import org.onosproject.net.device.DefaultPortDescription;
 import org.onosproject.net.device.DefaultPortStatistics;
@@ -71,6 +74,8 @@
 import org.onosproject.net.device.DeviceProviderService;
 import org.onosproject.net.device.PortDescription;
 import org.onosproject.net.device.PortStatistics;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.driver.HandlerBehaviour;
 import org.onosproject.net.provider.AbstractProvider;
 import org.onosproject.net.provider.ProviderId;
 import org.onosproject.openflow.controller.Dpid;
@@ -479,7 +484,7 @@
                     LOG.debug("Ports Of{}", portsOf);
                     portsOf.forEach(
                         op -> {
-                            portDescs.add(buildPortDescription(type, op));
+                            portDescs.add(buildPortDescription(type, op, opsw));
                         }
                      );
                     });
@@ -540,9 +545,10 @@
             return annotations;
         }
 
-        private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port) {
+        private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port,
+                OpenFlowOpticalSwitch opsw) {
             if (port instanceof  OFPortOptical) {
-               return buildPortDescription(ptype, (OFPortOptical) port);
+                return buildPortDescription(ptype, (OFPortOptical) port, opsw);
             }
             return buildPortDescription(ptype, (OFExpPort) port);
         }
@@ -681,7 +687,8 @@
          * @param port the port to build from.
          * @return portDescription for the port.
          */
-        private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port) {
+        private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port,
+                OpenFlowOpticalSwitch opsw) {
             checkArgument(port.getDesc().size() >= 1);
 
             // Minimally functional fixture. This needs to be fixed as we add better support.
@@ -706,8 +713,35 @@
                 case 2:     // OMS port
                     // Assume complete optical spectrum and 50 GHz grid
                     // LINC-OE is only supported optical OF device for now
-                    return omsPortDescription(portNo, enabled,
-                            Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
+                    Set<OchSignal> signals = null;
+                    if (opsw instanceof HandlerBehaviour) {
+                        DriverHandler driverHandler = ((HandlerBehaviour) opsw).handler();
+                        if (driverHandler != null && driverHandler.hasBehaviour(LambdaQuery.class)) {
+                            try {
+                                signals = driverHandler.behaviour(LambdaQuery.class).queryLambdas(portNo);
+                            } catch (NullPointerException e) {
+                                signals = null;
+                            }
+                        }
+                    }
+                    Frequency minFreq;
+                    Frequency maxFreq;
+                    Frequency channelSpacing;
+                    if (signals == null || signals.isEmpty()) {
+                        minFreq = Spectrum.U_BAND_MIN;
+                        maxFreq = Spectrum.O_BAND_MAX;
+                        channelSpacing = Frequency.ofGHz(50);
+                    } else {
+                        Comparator<OchSignal> compare =
+                                (OchSignal a, OchSignal b) -> a.spacingMultiplier() - b.spacingMultiplier();
+                        OchSignal minOch = Collections.min(signals, compare);
+                        OchSignal maxOch = Collections.max(signals, compare);
+                        minFreq = minOch.centralFrequency();
+                        maxFreq = maxOch.centralFrequency();
+                        channelSpacing = minOch.channelSpacing().frequency();
+                    }
+                    return omsPortDescription(portNo, enabled, minFreq,
+                            maxFreq, channelSpacing, annotations);
                 case 5:     // OCH port
                     OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
                     return ochPortDescription(portNo, enabled, OduSignalType.ODU4,