[ONOS-6463] general device Provider

Change-Id: Ibc045bffe14c24068adc7f0adc96366d0f1807a0
diff --git a/drivers/bmv2/BUCK b/drivers/bmv2/BUCK
new file mode 100644
index 0000000..95b2d44
--- /dev/null
+++ b/drivers/bmv2/BUCK
@@ -0,0 +1,29 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+]
+
+TEST_DEPS = [
+    '//lib:TEST_ADAPTERS',
+    '//core/api:onos-api-tests',
+]
+
+BUNDLES = [
+    ':onos-drivers-bmv2',
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+    resources_root = 'src/main/resources',
+    resources = glob(['src/main/resources/**']),
+)
+
+onos_app (
+    app_name = 'org.onosproject.drivers.bmv2',
+    title = 'bmv2 Device Drivers',
+    category = 'Drivers',
+    url = 'http://onosproject.org',
+    description = 'ONOS BMv2 Device Drivers application.',
+    included_bundles = BUNDLES,
+    required_apps = [ 'org.onosproject.generaldeviceprovider' ],
+)
diff --git a/drivers/bmv2/features.xml b/drivers/bmv2/features.xml
new file mode 100644
index 0000000..1741897
--- /dev/null
+++ b/drivers/bmv2/features.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+  ~ 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.
+  -->
+<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>
+
+    </feature>
+</features>
diff --git a/drivers/bmv2/pom.xml b/drivers/bmv2/pom.xml
new file mode 100644
index 0000000..f99f555
--- /dev/null
+++ b/drivers/bmv2/pom.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<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.11.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-drivers-bmv2</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>BMv2 device drivers</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.drivers.bmv2</onos.app.name>
+        <onos.app.origin>ON.Lab</onos.app.origin>
+        <onos.app.title>BMv2 Device Drivers</onos.app.title>
+        <onos.app.category>Drivers</onos.app.category>
+        <onos.app.url>http://onosproject.org</onos.app.url>
+        <onos.app.requires>
+            org.onosproject.generaldeviceprovider
+        </onos.app.requires>
+    </properties>
+
+</project>
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DriversLoader.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DriversLoader.java
new file mode 100644
index 0000000..45fb749
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DriversLoader.java
@@ -0,0 +1,31 @@
+/*
+ * 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.drivers.bmv2;
+
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+
+/**
+ * Loader for P4Runtime device drivers.
+ */
+@Component(immediate = true)
+public class Bmv2DriversLoader extends AbstractDriverLoader {
+
+    public Bmv2DriversLoader() {
+        super("/bmv2-drivers.xml");
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2Handshaker.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2Handshaker.java
new file mode 100644
index 0000000..06f9c9e
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2Handshaker.java
@@ -0,0 +1,81 @@
+/*
+ * 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.drivers.bmv2;
+
+import org.onosproject.net.MastershipRole;
+import org.onosproject.net.device.DeviceHandshaker;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverData;
+import org.onosproject.net.key.DeviceKeyId;
+import org.onosproject.net.key.DeviceKeyService;
+import org.slf4j.Logger;
+
+import java.util.concurrent.CompletableFuture;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of the DeviceHandshaker for the bmv2 softswitch.
+ */
+//TODO consider abstract class with empty connect method and
+//the implementation into a protected one for reusability.
+//FIXME fill method bodies, used for testing.
+public class Bmv2Handshaker extends AbstractHandlerBehaviour
+        implements DeviceHandshaker {
+
+    private final Logger log = getLogger(getClass());
+
+    @Override
+    public CompletableFuture<Boolean> connect() {
+        CompletableFuture<Boolean> result = new CompletableFuture<>();
+        DeviceKeyService deviceKeyService = handler().get(DeviceKeyService.class);
+        DriverData data = data();
+        //Retrieving information from the driver data.
+        log.info("protocol bmv2, ip {}, port {}, key {}", data.value("p4runtime_ip"),
+                data.value("p4runtime_port"),
+                deviceKeyService.getDeviceKey(DeviceKeyId.deviceKeyId(data.value("p4runtime_key")))
+                        .asUsernamePassword().username());
+
+        log.info("protocol gnmi, ip {}, port {}, key {}", data.value("gnmi_ip"), data.value("gnmi_port"),
+                deviceKeyService.getDeviceKey(DeviceKeyId.deviceKeyId(data.value("gnmi_key")))
+                        .asUsernamePassword().username());
+        result.complete(true);
+        return result;
+    }
+
+    @Override
+    public CompletableFuture<Boolean> disconnect() {
+        CompletableFuture<Boolean> result = new CompletableFuture<>();
+        result.complete(true);
+        return result;
+    }
+
+    @Override
+    public CompletableFuture<Boolean> isReachable() {
+        CompletableFuture<Boolean> result = new CompletableFuture<>();
+        result.complete(true);
+        return result;
+    }
+
+    @Override
+    public CompletableFuture<MastershipRole> roleChanged(MastershipRole newRole) {
+        CompletableFuture<MastershipRole> result = new CompletableFuture<>();
+        result.complete(MastershipRole.MASTER);
+        return result;
+    }
+
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/package-info.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/package-info.java
new file mode 100644
index 0000000..e76790e
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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 for bmv2 device drivers.
+ */
+package org.onosproject.drivers.bmv2;
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/resources/bmv2-drivers.xml b/drivers/bmv2/src/main/resources/bmv2-drivers.xml
new file mode 100644
index 0000000..7f0d3f2
--- /dev/null
+++ b/drivers/bmv2/src/main/resources/bmv2-drivers.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<drivers>
+    <driver name="bmv2" manufacturer="barefoot" hwVersion="1.0.0" swVersion="1.0.0">
+        <behaviour api="org.onosproject.net.device.DeviceHandshaker"
+                   impl="org.onosproject.drivers.bmv2.Bmv2Handshaker"/>
+    </driver>
+</drivers>
+
diff --git a/drivers/pom.xml b/drivers/pom.xml
index 99016d9..72fa76f 100644
--- a/drivers/pom.xml
+++ b/drivers/pom.xml
@@ -49,6 +49,7 @@
         <module>flowspec</module>
         <module>microsemi</module>
         <module>oplink</module>
+        <module>bmv2</module>
     </modules>
 
     <!--<properties>