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

Change-Id: I3c65d19be87066448655610abf9d8b89385a4141
diff --git a/drivers/netconf/features.xml b/drivers/netconf/features.xml
new file mode 100644
index 0000000..ed1f431
--- /dev/null
+++ b/drivers/netconf/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/netconf/pom.xml b/drivers/netconf/pom.xml
new file mode 100644
index 0000000..0235eaa
--- /dev/null
+++ b/drivers/netconf/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-netconf</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>Netconf device drivers</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.drivers.netconf</onos.app.name>
+    </properties>
+
+    <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>
+
+</project>
\ No newline at end of file
diff --git a/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/NetconfConfigGetter.java b/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/NetconfConfigGetter.java
new file mode 100644
index 0000000..4f42a96
--- /dev/null
+++ b/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/NetconfConfigGetter.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2015-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.netconf;
+
+import com.google.common.base.Preconditions;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.ConfigGetter;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.netconf.NetconfController;
+import org.slf4j.Logger;
+
+import java.io.IOException;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Gets the configuration of the specified type from the specified device. If a
+ * failure occurs it returns the error string found in UNABLE_TO_READ_CONFIG.
+ * This is a temporary development tool for use until yang integration is complete.
+ * This is not a properly specified behavior implementation. DO NOT USE AS AN EXAMPLE.
+ */
+//FIXME this should eventually be removed.
+
+public class NetconfConfigGetter extends AbstractHandlerBehaviour
+        implements ConfigGetter {
+
+    private final Logger log = getLogger(NetconfControllerConfig.class);
+
+    //FIXME the error string should be universal for all implementations of
+    // ConfigGetter
+    public static final String UNABLE_TO_READ_CONFIG = "config retrieval error";
+
+    @Override
+    public String getConfiguration(String type) {
+        DriverHandler handler = handler();
+        NetconfController controller = handler.get(NetconfController.class);
+        DeviceId ofDeviceId = handler.data().deviceId();
+        Preconditions.checkNotNull(controller, "Netconf controller is null");
+        try {
+            return controller.getDevicesMap().
+                    get(ofDeviceId).
+                    getSession().
+                    getConfig(type);
+        } catch (IOException e) {
+            log.error("Configuration could not be retrieved {}",
+                      e.getStackTrace().toString());
+        }
+        return UNABLE_TO_READ_CONFIG;
+    }
+
+}
diff --git a/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/NetconfControllerConfig.java b/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/NetconfControllerConfig.java
new file mode 100644
index 0000000..85a0dd8
--- /dev/null
+++ b/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/NetconfControllerConfig.java
@@ -0,0 +1,102 @@
+/*
+ * 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.drivers.netconf;
+
+import com.google.common.base.Preconditions;
+import org.onosproject.drivers.utilities.XmlConfigParser;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.ControllerConfig;
+import org.onosproject.net.behaviour.ControllerInfo;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfDevice;
+import org.slf4j.Logger;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of controller config which allows to get and set controllers
+ * through the Netconf protocol.
+ */
+public class NetconfControllerConfig extends AbstractHandlerBehaviour
+        implements ControllerConfig {
+
+    private final Logger log = getLogger(NetconfControllerConfig.class);
+
+    @Override
+    public List<ControllerInfo> getControllers() {
+        DriverHandler handler = handler();
+        NetconfController controller = handler.get(NetconfController.class);
+        DeviceId ofDeviceId = handler.data().deviceId();
+        Preconditions.checkNotNull(controller, "Netconf controller is null");
+        List<ControllerInfo> controllers = new ArrayList<>();
+        try {
+            controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser.
+                    loadXml(new ByteArrayInputStream(controller.
+                            getDevicesMap().get(ofDeviceId).getSession().
+                            getConfig("running").getBytes(StandardCharsets.UTF_8)))));
+        } catch (IOException e) {
+            log.error("Cannot comunicate to device {} ", ofDeviceId);
+        }
+        return controllers;
+    }
+
+    @Override
+    public void setControllers(List<ControllerInfo> controllers) {
+        DriverHandler handler = handler();
+        NetconfController controller = handler.get(NetconfController.class);
+        DeviceId deviceId = handler.data().deviceId();
+        Preconditions.checkNotNull(controller, "Netconf controller is null");
+        try {
+            NetconfDevice device = controller.getNetconfDevice(deviceId);
+            log.warn("provider map {}", controller.getDevicesMap());
+            String config = null;
+            try {
+                config = XmlConfigParser.createControllersConfig(
+                        XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")),
+                        XmlConfigParser.loadXml(
+                                new ByteArrayInputStream(device.getSession()
+                                                                 .getConfig("running")
+                                                                 .getBytes(
+                                                                         StandardCharsets.UTF_8))),
+                        "running", "merge", "create", controllers
+                );
+            } catch (IOException e) {
+                log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
+            }
+            device.getSession().editConfig(config.substring(config.indexOf("-->") + 3));
+        } catch (NullPointerException e) {
+            log.warn("No NETCONF device with requested parameters " + e);
+            throw new NullPointerException("No NETCONF device with requested parameters " + e);
+        } catch (IOException e) {
+            log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
+        }
+
+    }
+
+    //TODO maybe put method getNetconfClientService like in ovsdb if we need it
+
+}
+
+
diff --git a/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/NetconfDriversLoader.java b/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/NetconfDriversLoader.java
new file mode 100644
index 0000000..30c8758
--- /dev/null
+++ b/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/NetconfDriversLoader.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.netconf;
+
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+
+import java.io.InputStream;
+
+/**
+ * Loader for NETCONF device drivers from specific xml.
+ */
+@Component(immediate = true)
+public class NetconfDriversLoader extends AbstractDriverLoader {
+
+    private static final String DRIVERS_XML = "/netconf-drivers.xml";
+
+    @Override
+    protected InputStream loadXMLDriversStream() {
+        return getClassLoaderInstance().getResourceAsStream(DRIVERS_XML);
+    }
+
+    @Override
+    protected ClassLoader getClassLoaderInstance() {
+        return getClass().getClassLoader();
+    }
+}
diff --git a/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/package-info.java b/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/package-info.java
new file mode 100644
index 0000000..1ff58b5
--- /dev/null
+++ b/drivers/netconf/src/main/java/org/onosproject/drivers/netconf/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 NETCONF device drivers.
+ */
+package org.onosproject.drivers.netconf;
\ No newline at end of file
diff --git a/drivers/netconf/src/main/resources/netconf-drivers.xml b/drivers/netconf/src/main/resources/netconf-drivers.xml
new file mode 100644
index 0000000..dc54612
--- /dev/null
+++ b/drivers/netconf/src/main/resources/netconf-drivers.xml
@@ -0,0 +1,31 @@
+<?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>
+    <!--This driver is for simulated NETCONF devices through of-config tool on top og OVSDB-->
+    <driver name="ovs-netconf" extends="default,ovs"
+            manufacturer="" hwVersion="" swVersion="">
+        <behaviour api="org.onosproject.net.behaviour.ControllerConfig"
+                   impl="org.onosproject.drivers.netconf.NetconfControllerConfig"/>
+        <behaviour api="org.onosproject.net.behaviour.ConfigGetter"
+                   impl="org.onosproject.drivers.netconf.NetconfConfigGetter"/>
+    </driver>
+    <driver name="netconf" extends="default">
+        <behaviour api="org.onosproject.net.behaviour.ConfigGetter"
+                   impl="org.onosproject.drivers.netconf.NetconfConfigGetter"/>
+    </driver>
+</drivers>
+
diff --git a/drivers/netconf/src/main/resources/org/onosproject/drivers/netconf/controllers.xml b/drivers/netconf/src/main/resources/org/onosproject/drivers/netconf/controllers.xml
new file mode 100644
index 0000000..21e4dd1
--- /dev/null
+++ b/drivers/netconf/src/main/resources/org/onosproject/drivers/netconf/controllers.xml
@@ -0,0 +1,36 @@
+<!--
+  ~ 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.
+  -->
+
+<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <edit-config>
+        <target>
+        </target>
+        <default-operation>
+        </default-operation>
+        <config>
+            <capable-switch xmlns="urn:onf:config:yang">
+                <id></id>
+                <logical-switches>
+                    <switch>
+                        <id></id>
+                        <controllers>
+                        </controllers>
+                    </switch>
+                </logical-switches>
+            </capable-switch>
+        </config>
+    </edit-config>
+</rpc>
\ No newline at end of file