ONOS-3758 restructuring driver module with sub-modules for different drivers

Change-Id: I3c65d19be87066448655610abf9d8b89385a4141
diff --git a/drivers/ciena/features.xml b/drivers/ciena/features.xml
new file mode 100644
index 0000000..4dbf1a8
--- /dev/null
+++ b/drivers/ciena/features.xml
@@ -0,0 +1,27 @@
+<?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-restsb-api/${project.version}</bundle>
+
+        <bundle>mvn:${project.groupId}/onos-drivers-utilities/${project.version}</bundle>
+    </feature>
+</features>
diff --git a/drivers/ciena/pom.xml b/drivers/ciena/pom.xml
new file mode 100644
index 0000000..b774f84
--- /dev/null
+++ b/drivers/ciena/pom.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>onos-drivers-general</artifactId>
+        <groupId>org.onosproject</groupId>
+        <version>1.5.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-drivers-ciena</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>Ciena device drivers</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.drivers.ciena</onos.app.name>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-drivers-utilities</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-restsb-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/drivers/ciena/src/main/java/org/onosproject/drivers/ciena/CienaDriversLoader.java b/drivers/ciena/src/main/java/org/onosproject/drivers/ciena/CienaDriversLoader.java
new file mode 100644
index 0000000..13a6d93
--- /dev/null
+++ b/drivers/ciena/src/main/java/org/onosproject/drivers/ciena/CienaDriversLoader.java
@@ -0,0 +1,41 @@
+/*
+ * 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.ciena;
+
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+
+import java.io.InputStream;
+
+/**
+ * Loader for Ciena device drivers from specific xml.
+ */
+@Component(immediate = true)
+public class CienaDriversLoader extends AbstractDriverLoader {
+
+    private static final String DRIVERS_XML = "/ciena-drivers.xml";
+
+    @Override
+    protected InputStream loadXMLDriversStream() {
+        return getClassLoaderInstance().getResourceAsStream(DRIVERS_XML);
+    }
+
+    @Override
+    protected ClassLoader getClassLoaderInstance() {
+        return getClass().getClassLoader();
+    }
+}
diff --git a/drivers/ciena/src/main/java/org/onosproject/drivers/ciena/PortDiscoveryCienaWaveserverImpl.java b/drivers/ciena/src/main/java/org/onosproject/drivers/ciena/PortDiscoveryCienaWaveserverImpl.java
new file mode 100644
index 0000000..affebe5
--- /dev/null
+++ b/drivers/ciena/src/main/java/org/onosproject/drivers/ciena/PortDiscoveryCienaWaveserverImpl.java
@@ -0,0 +1,111 @@
+/*
+ *
+ *  * 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.ciena;
+
+import com.google.common.collect.Lists;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.onosproject.drivers.utilities.XmlConfigParser;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.CltSignalType;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.behaviour.PortDiscovery;
+import org.onosproject.net.device.OduCltPortDescription;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.protocol.rest.RestSBController;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Discovers the ports from a Ciena WaveServer Rest device.
+ */
+public class PortDiscoveryCienaWaveserverImpl extends AbstractHandlerBehaviour
+        implements PortDiscovery {
+
+    private static final String SPEED = "speed";
+    private static final String GBPS = "Gbps";
+    private static final String PORT_ID = "port-id";
+    private static final String XML = "xml";
+    private static final String ENABLED = "enabled";
+    private static final String EMPTY_STRING = "";
+    private static final String NAME = "name";
+    private static final String ADMIN_STATE = "admin-state";
+
+    private static final ArrayList<String> LINESIDE = Lists.newArrayList(
+            "1.1", "1.2", "12.1", "12.2");
+
+    private static final String GENERAL_PORT_REQUEST =
+            "yang-api/datastore/ws-ports?config=true&format=xml&depth=unbounded";
+    private static final String SPECIFIC_PORT_PATH = "yang-api/datastore/ws-ptps/ptp/";
+    private static final String SPECIFIC_PORT_CONFIG =
+            "/ptp-config?config=true&format=xml&depth=unbounded";
+
+
+    @Override
+    public List<PortDescription> getPorts() {
+        List<PortDescription> ports = Lists.newArrayList();
+        DriverHandler handler = handler();
+        RestSBController controller = checkNotNull(handler.get(RestSBController.class));
+        DeviceId deviceId = handler.data().deviceId();
+
+
+        HierarchicalConfiguration config = XmlConfigParser.
+                loadXml(controller.get(deviceId, GENERAL_PORT_REQUEST, XML));
+        List<HierarchicalConfiguration> portsConfig =
+                XmlConfigParser.parseWaveServerCienaPorts(config);
+
+        portsConfig.stream().forEach(sub -> {
+            String name = sub.getString(NAME);
+            SparseAnnotations annotations = DefaultAnnotations.builder()
+                    .set(AnnotationKeys.NAME, String.valueOf(name)).build();
+            if (LINESIDE.contains(name)) {
+                String wsportInfoRequest = SPECIFIC_PORT_PATH + sub.getLong(PORT_ID) +
+                        SPECIFIC_PORT_CONFIG;
+                ports.add(XmlConfigParser.parseWaveServerCienaOCHPorts(
+                        sub.getLong(PORT_ID),
+                        toGbps(Long.parseLong(sub.getString(SPEED).replace(GBPS, EMPTY_STRING))),
+                        XmlConfigParser.loadXml(controller.get(deviceId, wsportInfoRequest, XML)),
+                        annotations));
+            } else {
+                //FIXME change when all optical types have two way information methods, see jira tickets
+                final int speed100GbpsinMbps = 100000;
+                CltSignalType cltType = toGbps(Long.parseLong(
+                        sub.getString(SPEED).replace(GBPS, EMPTY_STRING))) == speed100GbpsinMbps ?
+                        CltSignalType.CLT_100GBE : null;
+                ports.add(new OduCltPortDescription(PortNumber.portNumber(sub.getLong(PORT_ID)),
+                                                    sub.getString(ADMIN_STATE).equals(ENABLED),
+                                                    cltType, annotations));
+            }
+        });
+        return ports;
+    }
+
+    //FIXME remove when all optical types have two way information methods, see jira tickets
+    private long toGbps(long speed) {
+        return speed * 1000;
+    }
+}
+
diff --git a/drivers/ciena/src/main/java/org/onosproject/drivers/ciena/package-info.java b/drivers/ciena/src/main/java/org/onosproject/drivers/ciena/package-info.java
new file mode 100644
index 0000000..e0d97c2
--- /dev/null
+++ b/drivers/ciena/src/main/java/org/onosproject/drivers/ciena/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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 for Ciena device drivers.
+ */
+package org.onosproject.drivers.ciena;
\ No newline at end of file
diff --git a/drivers/ciena/src/main/resources/ciena-drivers.xml b/drivers/ciena/src/main/resources/ciena-drivers.xml
new file mode 100644
index 0000000..fa3a0f0
--- /dev/null
+++ b/drivers/ciena/src/main/resources/ciena-drivers.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<drivers>
+    <driver name="restCiena" manufacturer="Ciena" hwVersion="1.0.0" swVersion="1.0.0">
+        <behaviour api="org.onosproject.net.behaviour.PortDiscovery"
+                   impl="org.onosproject.drivers.ciena.PortDiscoveryCienaWaveserverImpl"/>
+    </driver>
+</drivers>
+