ONOS-3961 ONOS-4285 Implemented BMv2 drivers and protocol

Provides Thrift-based implementation for FlowRuleProgrammable and
PortDiscovery behaviours.

Change-Id: Ibbf8720d92301bcd23c5c583d156f464015ff1ef
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PortGetterDriver.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PortGetterDriver.java
new file mode 100644
index 0000000..fa2dde0
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PortGetterDriver.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2014-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.bmv2;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.onosproject.bmv2.api.Bmv2Exception;
+import org.onosproject.bmv2.ctl.Bmv2ThriftClient;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.behaviour.PortDiscovery;
+import org.onosproject.net.device.DefaultPortDescription;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collections;
+import java.util.List;
+
+public class Bmv2PortGetterDriver extends AbstractHandlerBehaviour
+        implements PortDiscovery {
+
+    private final Logger log =
+            LoggerFactory.getLogger(this.getClass());
+
+    @Override
+    public List<PortDescription> getPorts() {
+        Bmv2ThriftClient deviceClient;
+        try {
+            deviceClient = Bmv2ThriftClient.of(handler().data().deviceId());
+        } catch (Bmv2Exception e) {
+            log.error("Failed to connect to Bmv2 device", e);
+            return Collections.emptyList();
+        }
+
+        List<PortDescription> portDescriptions = Lists.newArrayList();
+
+        try {
+
+            deviceClient.getPortsInfo().forEach(
+                    p -> {
+                        DefaultAnnotations.Builder builder =
+                                DefaultAnnotations.builder();
+                        p.getExtraProperties().forEach(builder::set);
+                        SparseAnnotations annotations = builder.build();
+
+                        portDescriptions.add(new DefaultPortDescription(
+                                PortNumber.portNumber(
+                                        (long) p.portNumber(),
+                                        p.ifaceName()),
+                                p.isUp(),
+                                annotations
+                        ));
+                    });
+        } catch (Bmv2Exception e) {
+            log.error("Unable to get port description from Bmv2 device", e);
+        }
+
+        return ImmutableList.copyOf(portDescriptions);
+    }
+}