[ONOS-6386] Polatis OpenFlow driver

- Handling Polatis extention for ofp_port_stats_prop_type (=OFPortStatsPropVer14)
  0x7C00 (=31744 in decimal) is future work.

Change-Id: I8ad309502737d2bff46e412e80f4310ca61bf254
diff --git a/drivers/optical/src/main/java/org/onosproject/driver/optical/handshaker/PolatisHandshaker.java b/drivers/optical/src/main/java/org/onosproject/driver/optical/handshaker/PolatisHandshaker.java
new file mode 100644
index 0000000..4f69551
--- /dev/null
+++ b/drivers/optical/src/main/java/org/onosproject/driver/optical/handshaker/PolatisHandshaker.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2017-present 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.handshaker;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.util.List;
+import java.util.Set;
+import org.onosproject.net.Device.Type;
+import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
+import org.onosproject.openflow.controller.PortDescPropertyType;
+import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch;
+import org.projectfloodlight.openflow.protocol.OFMessage;
+import org.projectfloodlight.openflow.protocol.OFPortDesc;
+import org.slf4j.Logger;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+/**
+ * Handshaker for Polatis fiber switch.
+ *
+ */
+public class PolatisHandshaker extends AbstractOpenFlowSwitch
+    implements OpenFlowOpticalSwitch {
+
+    private static final Logger log = getLogger(PolatisHandshaker.class);
+
+    @Override
+    public Boolean supportNxRole() {
+        // Device is OF1.4 so response doesn't matter.
+        return Boolean.FALSE;
+    }
+
+    @Override
+    public void startDriverHandshake() {
+        // nothing Device specific required
+    }
+
+    @Override
+    public boolean isDriverHandshakeComplete() {
+        // nothing Device specific to do
+        return true;
+    }
+
+    @Override
+    public void processDriverHandshakeMessage(OFMessage m) {
+        // nothing Device specific to do
+    }
+
+    @Override
+    public Type deviceType() {
+        return Type.FIBER_SWITCH;
+    }
+
+    @Override
+    public List<OFPortDesc> getPortsOf(PortDescPropertyType type) {
+        return this.getPorts().stream()
+            .filter(pd -> isPortType(type, pd))
+            .collect(ImmutableList.toImmutableList());
+    }
+
+    /**
+     * Tests if OFPortDesc {@code pd} is a {@code type}.
+     *
+     * @param type to test if {@code pd} is of port type
+     * @param pd OpenFlow port description to test.
+     * @return true if {@code pd} is of port description of {@code type}
+     */
+    private static boolean isPortType(PortDescPropertyType type, OFPortDesc pd) {
+        try {
+            return pd.getProperties().stream()
+                        .anyMatch(prop -> prop.getType() == type.valueOf());
+        } catch (UnsupportedOperationException e) {
+            log.warn("Unexpected OFPortDesc {} reveived", pd, e);
+            return false;
+        }
+    }
+
+    @Override
+    public Set<PortDescPropertyType> getPortTypes() {
+        return ImmutableSet.of(PortDescPropertyType.OPTICAL);
+    }
+}
diff --git a/drivers/optical/src/main/resources/optical-drivers.xml b/drivers/optical/src/main/resources/optical-drivers.xml
index 344436e..1dd44ff 100644
--- a/drivers/optical/src/main/resources/optical-drivers.xml
+++ b/drivers/optical/src/main/resources/optical-drivers.xml
@@ -93,7 +93,18 @@
 
     </driver>
 
-    <driver name="polatis-config" manufacturer="Polatis" hwVersion="N-VST-48x48-HU1-DMHNV-805" swVersion="6.6.1.7">
+    <driver name="polatis" extends="default"
+            manufacturer="Polatis" hwVersion="N-VST-48x48-HU1-DMHNV-805" swVersion="6.6.1.7">
+        <behaviour api="org.onosproject.net.optical.OpticalDevice"
+                   impl="org.onosproject.net.optical.DefaultOpticalDevice"/>
+        <behaviour api="org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver"
+                   impl="org.onosproject.driver.optical.handshaker.PolatisHandshaker"/>
+        <!-- TODO use OF1.4 reported information when ready -->
+        <behaviour api="org.onosproject.net.behaviour.LambdaQuery"
+                   impl="org.onosproject.driver.optical.query.ConfigLambdaQuery"/>
+    </driver>
+
+    <driver name="polatis-config" manufacturer="Polatis" hwVersion="config" swVersion=".*">
         <behaviour api="org.onosproject.net.optical.OpticalDevice"
                    impl="org.onosproject.net.optical.DefaultOpticalDevice"/>
         <behaviour api="org.onosproject.net.device.DeviceDescriptionDiscovery"
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 53cf2e5..095fdc6 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
@@ -44,6 +44,7 @@
 import org.onosproject.net.OduSignalType;
 import org.onosproject.net.OtuSignalType;
 import org.onosproject.net.Port;
+import org.onosproject.net.Port.Type;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.SparseAnnotations;
 import org.onosproject.net.behaviour.LambdaQuery;
@@ -101,6 +102,7 @@
 import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
 import org.projectfloodlight.openflow.protocol.OFStatsType;
 import org.projectfloodlight.openflow.protocol.OFVersion;
+import org.projectfloodlight.openflow.protocol.ver14.OFOpticalPortFeaturesSerializerVer14;
 import org.projectfloodlight.openflow.types.OFPort;
 import org.projectfloodlight.openflow.types.PortSpeed;
 import org.slf4j.Logger;
@@ -588,11 +590,20 @@
                 case FIBER_SWITCH:
                     opsw = (OpenFlowOpticalSwitch) sw;
                     opsw.getPortTypes().forEach(type -> {
-                        opsw.getPortsOf(type).forEach(
-                                op -> {
-                                    portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
-                                }
-                        );
+                        opsw.getPortsOf(type).forEach(op -> {
+                            if (op instanceof OFPortDesc) {
+                                // ports using standard optical extension
+                                // TODO OFMessage -> PortDescription should
+                                // probably be a Behaviour
+                                portDescs.add(oms(buildPortDescription((OFPortDesc) op)));
+                            } else if (op instanceof OFCalientPortDescStatsEntry) {
+                                // calient extension
+                                portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op));
+                            } else {
+                                LOG.warn("Unexpected FIBER_SWITCH port {} on {}",
+                                         op, sw.getStringId());
+                            }
+                        });
                     });
                     break;
                 default:
@@ -602,6 +613,41 @@
             return portDescs;
         }
 
+        /**
+         * Ensures returned PortDescription is an OMS port.
+         *
+         * @param descr input PortDescription
+         * @return OMS PortDescription
+         */
+        private PortDescription oms(PortDescription descr) {
+            // Hack until OFMessage -> PortDescription transformation
+            // becomes a Behaviour
+            if (descr.type() == Type.OMS) {
+                return descr;
+            }
+
+            Builder builder = DefaultAnnotations.builder();
+            builder.putAll(descr.annotations());
+
+            // set reasonable default when mandatory key is missing
+            if (Strings.isNullOrEmpty(descr.annotations().value(AK_MIN_FREQ_HZ))) {
+                builder.set(AK_MIN_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MIN.asHz()));
+            }
+
+            if (Strings.isNullOrEmpty(descr.annotations().value(AK_MAX_FREQ_HZ))) {
+                builder.set(AK_MAX_FREQ_HZ, String.valueOf(Spectrum.O_BAND_MAX.asHz()));
+            }
+
+            if (Strings.isNullOrEmpty(descr.annotations().value(AK_GRID_HZ))) {
+                builder.set(AK_GRID_HZ, String.valueOf(Frequency.ofGHz(50).asHz()));
+            }
+
+            return DefaultPortDescription.builder(descr)
+                    .type(Type.OMS)
+                    .annotations(builder.build())
+                    .build();
+        }
+
         private PortDescription buildOduCltPortDescription(OFPortDesc port) {
             PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
             boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
@@ -764,6 +810,16 @@
             return Long.toString(freqMhz * 1_000_000);
         }
 
+        private String lambdaToAnnotationHz(long lambda) {
+            // ref. OF1.5: wavelength (lambda) as nm * 100
+
+            long c = 299792458; // speed of light in m/s
+            // f = c / λ
+            // (m/s) * (nm/m) / (nm * 100) * 100
+            // annotations is in Hz
+            return Long.toString(c * 1_000_000_000 / lambda * 100);
+        }
+
         private PortDescription buildPortDescription14(OFPortDesc port) {
             PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
             boolean enabled =
@@ -793,10 +849,11 @@
             if (optical.isPresent()) {
                 // optical port
 
-                // FIXME is there a generic way to access OFOpticalPortFeaturesSerializerVer14
+                // FIXME is there a OF version neutral way to access
+                // OFOpticalPortFeaturesSerializerVer14
 
                 // wire value for OFOpticalPortFeatures.USE_FREQ
-                final long useFreq = 0x8;
+                long useFreq = OFOpticalPortFeaturesSerializerVer14.USE_FREQ_VAL;
                 if ((optical.get().getSupported() & useFreq) != 0) {
                     // unit is in Frequency Mhz
                     long rxMinFreq = optical.get().getRxMinFreqLmda();
@@ -823,8 +880,28 @@
 
                 } else {
                     // unit is in Lambda nm * 100
-                    // TODO implement
-                    LOG.trace("Optical parameter specified in Lambda not supported yet", portNo);
+                    long rxMin = optical.get().getRxMinFreqLmda();
+                    long rxMax = optical.get().getRxMaxFreqLmda();
+                    long txMin = optical.get().getTxMinFreqLmda();
+                    long txMax = optical.get().getTxMaxFreqLmda();
+
+                    annotations.set(AK_RX_MIN_FREQ_HZ, lambdaToAnnotationHz(rxMin));
+                    annotations.set(AK_RX_MAX_FREQ_HZ, lambdaToAnnotationHz(rxMax));
+
+                    annotations.set(AK_TX_MIN_FREQ_HZ, lambdaToAnnotationHz(txMin));
+                    annotations.set(AK_TX_MAX_FREQ_HZ, lambdaToAnnotationHz(txMax));
+
+                    // FIXME pretty confident this is not going to happen
+                    // unless Device models Tx/Rx ports as separate port
+                    if (rxMin == txMin) {
+                        annotations.set(AK_MIN_FREQ_HZ,
+                                        lambdaToAnnotationHz(rxMin));
+                    }
+                    if (rxMax == txMax) {
+                        annotations.set(AK_MAX_FREQ_HZ,
+                                        lambdaToAnnotationHz(rxMax));
+                    }
+
                 }
                 // TODO parse other part of OFPortDescPropOptical
                 // Tx/Rx tunable, ...
@@ -968,7 +1045,7 @@
             // S160 data sheet
             // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch
             return omsPortDescription(portNo, enabled,
-                    Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(100), annotations);
+                    Spectrum.O_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations);
         }
 
         private PortDescription buildPortDescription(OFPortStatus status) {