Adding fujitsu t200 driver, reads OCH and ODUclt port descriptions from netconf. Does not work with the simulator!

Change-Id: I91e624eb747870b7a74d4d4c30e1748b637f7411
diff --git a/drivers/fujitsu/features.xml b/drivers/fujitsu/features.xml
new file mode 100644
index 0000000..ed1f431
--- /dev/null
+++ b/drivers/fujitsu/features.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+
+<!--
+  ~ 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.
+  -->
+<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
+    <feature name="${project.artifactId}" version="${project.version}"
+             description="${project.description}">
+        <feature>onos-api</feature>
+        <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
+
+        <bundle>mvn:${project.groupId}/onos-drivers-utilities/${project.version}</bundle>
+
+        <bundle>mvn:${project.groupId}/onos-netconf-api/${project.version}</bundle>
+    </feature>
+</features>
diff --git a/drivers/fujitsu/pom.xml b/drivers/fujitsu/pom.xml
index 5ec9f1a..272a9d5 100644
--- a/drivers/fujitsu/pom.xml
+++ b/drivers/fujitsu/pom.xml
@@ -26,6 +26,18 @@
     <modelVersion>4.0.0</modelVersion>
 
     <description>Fujitsu device drivers</description>
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-netconf-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-drivers-utilities</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
 
     <artifactId>onos-drivers-fujitsu</artifactId>
     <packaging>bundle</packaging>
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/PortGetterFujitsuImpl.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/PortGetterFujitsuImpl.java
new file mode 100644
index 0000000..3dc4223
--- /dev/null
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/PortGetterFujitsuImpl.java
@@ -0,0 +1,77 @@
+/*
+ * 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.drivers.fujitsu;
+
+import org.onosproject.drivers.utilities.XmlConfigParser;
+import org.onosproject.net.behaviour.PortDiscovery;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+import org.slf4j.Logger;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Retrieves the ports from a Fujitsu T100 device via netconf.
+ */
+public class PortGetterFujitsuImpl extends AbstractHandlerBehaviour
+        implements PortDiscovery {
+
+    private final Logger log = getLogger(getClass());
+
+    @Override
+    public List<PortDescription> getPorts() {
+        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
+        NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
+        String reply;
+        try {
+            reply = session.get(requestBuilder());
+        } catch (IOException e) {
+            throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
+        }
+        List<PortDescription> descriptions = XmlConfigParser.
+                parseFujitsuT100Ports(XmlConfigParser.
+                        loadXml(new ByteArrayInputStream(reply.getBytes())));
+        return descriptions;
+    }
+
+    /**
+     * Builds a request crafted to get the configuration required to create port
+     * descriptions for the device.
+     * @return The request string.
+     */
+    private String requestBuilder() {
+        StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+        //Message ID is injected later.
+        rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">");
+        rpc.append("<get>");
+        rpc.append("<filter type=\"subtree\">");
+        rpc.append("<interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">");
+        rpc.append("</interfaces>");
+        rpc.append("</filter>");
+        rpc.append("</get>");
+        rpc.append("</rpc>");
+        return rpc.toString();
+    }
+}
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/package-info.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/package-info.java
index a7ab664..2d0a157 100644
--- a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/package-info.java
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/package-info.java
@@ -15,6 +15,6 @@
  */
 
 /**
- * Package for Ciena device drivers.
+ * Package for fujitsu device drivers.
  */
 package org.onosproject.drivers.fujitsu;
\ No newline at end of file
diff --git a/drivers/fujitsu/src/main/resources/onos-drivers.xml b/drivers/fujitsu/src/main/resources/fujitsu-drivers.xml
similarity index 72%
rename from drivers/fujitsu/src/main/resources/onos-drivers.xml
rename to drivers/fujitsu/src/main/resources/fujitsu-drivers.xml
index ed74191..f42f91f 100644
--- a/drivers/fujitsu/src/main/resources/onos-drivers.xml
+++ b/drivers/fujitsu/src/main/resources/fujitsu-drivers.xml
@@ -15,7 +15,9 @@
   ~ limitations under the License.
   -->
 <drivers>
-  <driver>
-  </driver>
+    <driver name="fujitsu-netconf" manufacturer="Fujitsu" hwVersion="T100" swVersion="01-01-X">
+        <behaviour api="org.onosproject.net.behaviour.PortDiscovery"
+                   impl="org.onosproject.drivers.fujitsu.PortGetterFujitsuImpl"/>
+    </driver>
 </drivers>
 
diff --git a/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/XmlConfigParser.java b/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/XmlConfigParser.java
index ff0abb6..c846fd1 100644
--- a/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/XmlConfigParser.java
+++ b/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/XmlConfigParser.java
@@ -22,7 +22,10 @@
 import org.apache.commons.configuration.XMLConfiguration;
 import org.apache.commons.configuration.tree.ConfigurationNode;
 import org.onlab.packet.IpAddress;
+import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.ChannelSpacing;
+import org.onosproject.net.CltSignalType;
+import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.GridType;
 import org.onosproject.net.OchSignal;
 import org.onosproject.net.OduSignalType;
@@ -30,6 +33,7 @@
 import org.onosproject.net.SparseAnnotations;
 import org.onosproject.net.behaviour.ControllerInfo;
 import org.onosproject.net.device.OchPortDescription;
+import org.onosproject.net.device.OduCltPortDescription;
 import org.onosproject.net.device.PortDescription;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,6 +42,8 @@
 import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
 
 /**
  * Parser for Netconf XML configurations and replys.
@@ -76,7 +82,56 @@
         return controllers;
     }
 
-    public static String parseSwitchId(HierarchicalConfiguration cfg) {
+    /**
+     * Parses a configuration and returns a set of ports for the fujitsu T100.
+     * @param cfg a hierarchical configuration
+     * @return a list of port descriptions
+     */
+    public static List<PortDescription> parseFujitsuT100Ports(HierarchicalConfiguration cfg) {
+        AtomicInteger counter = new AtomicInteger(1);
+        List<PortDescription> portDescriptions = Lists.newArrayList();
+        List<HierarchicalConfiguration> subtrees =
+                cfg.configurationsAt("data.interfaces.interface");
+        for (HierarchicalConfiguration portConfig : subtrees) {
+            if (!portConfig.getString("name").contains("LCN") &&
+                    !portConfig.getString("name").contains("LMP") &&
+                    portConfig.getString("type").equals("ianaift:ethernetCsmacd")) {
+                portDescriptions.add(parseT100OduPort(portConfig, counter.getAndIncrement()));
+            } else if (portConfig.getString("type").equals("ianaift:otnOtu")) {
+                portDescriptions.add(parseT100OchPort(portConfig, counter.getAndIncrement()));
+            }
+        }
+        return portDescriptions;
+    }
+
+    private static OchPortDescription parseT100OchPort(HierarchicalConfiguration cfg, long count) {
+        PortNumber portNumber = PortNumber.portNumber(count);
+        HierarchicalConfiguration otuConfig = cfg.configurationAt("otu");
+        boolean enabled = otuConfig.getString("administrative-state").equals("up");
+        OduSignalType signalType = otuConfig.getString("rate").equals("OTU4") ? OduSignalType.ODU4 : null;
+        //Unsure how to retreive, outside knowledge it is tunable.
+        boolean isTunable = true;
+        OchSignal lambda = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
+        DefaultAnnotations annotations = DefaultAnnotations.builder().
+                set(AnnotationKeys.PORT_NAME, cfg.getString("name")).
+                build();
+        return new OchPortDescription(portNumber, enabled, signalType, isTunable, lambda, annotations);
+    }
+
+    private static OduCltPortDescription parseT100OduPort(HierarchicalConfiguration cfg, long count) {
+        PortNumber portNumber = PortNumber.portNumber(count);
+        HierarchicalConfiguration ethernetConfig = cfg.configurationAt("ethernet");
+        boolean enabled = ethernetConfig.getString("administrative-state").equals("up");
+        //Rate is in kbps
+        CltSignalType signalType = ethernetConfig.getString("rate").equals("100000000") ?
+                CltSignalType.CLT_100GBE : null;
+        DefaultAnnotations annotations = DefaultAnnotations.builder().
+                set(AnnotationKeys.PORT_NAME, cfg.getString("name")).
+                build();
+        return new OduCltPortDescription(portNumber, enabled, signalType, annotations);
+    }
+
+    protected static String parseSwitchId(HierarchicalConfiguration cfg) {
         HierarchicalConfiguration field =
                 cfg.configurationAt("data.capable-switch." +
                                             "logical-switches." +