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/features.xml b/drivers/bmv2/features.xml
new file mode 100644
index 0000000..c78c28b
--- /dev/null
+++ b/drivers/bmv2/features.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+
+<!--
+  ~ 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.
+  -->
+
+<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}">
+        <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
+        <bundle>mvn:${project.groupId}/onos-bmv2-protocol/${project.version}</bundle>
+    </feature>
+</features>
diff --git a/drivers/bmv2/pom.xml b/drivers/bmv2/pom.xml
new file mode 100644
index 0000000..680f25d
--- /dev/null
+++ b/drivers/bmv2/pom.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<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.6.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-drivers-bmv2</artifactId>
+    <version>1.6.0-SNAPSHOT</version>
+
+    <packaging>bundle</packaging>
+
+    <description>Device drivers for p4.org reference softswitch BMv2</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.drivers.bmv2</onos.app.name>
+        <onos.app.origin>ON.Lab</onos.app.origin>
+        <onos.app.category>Drivers</onos.app.category>
+        <onos.app.title>BMv2 Device Drivers</onos.app.title>
+        <onos.app.url>http://onosproject.org</onos.app.url>
+        <onos.app.requires>
+            org.onosproject.bmv2
+        </onos.app.requires>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-bmv2-protocol</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
+</project>
\ No newline at end of file
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..89ed1bd
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DriversLoader.java
@@ -0,0 +1,33 @@
+/*
+ * 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 org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+
+/**
+ * Loader for barefoot drivers from specific xml.
+ */
+@Component(immediate = true)
+public class Bmv2DriversLoader extends AbstractDriverLoader {
+
+    private static final String DRIVERS_XML = "/bmv2-drivers.xml";
+
+    public Bmv2DriversLoader() {
+        super(DRIVERS_XML);
+    }
+}
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleDriver.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleDriver.java
new file mode 100644
index 0000000..c000f52
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleDriver.java
@@ -0,0 +1,236 @@
+/*
+ * 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.base.Preconditions;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+import org.onosproject.bmv2.api.Bmv2ExtensionSelector;
+import org.onosproject.bmv2.api.Bmv2ExtensionTreatment;
+import org.onosproject.bmv2.api.Bmv2TableEntry;
+import org.onosproject.bmv2.api.Bmv2Exception;
+import org.onosproject.bmv2.ctl.Bmv2ThriftClient;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.flow.DefaultFlowEntry;
+import org.onosproject.net.flow.FlowEntry;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleProgrammable;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.criteria.ExtensionCriterion;
+import org.onosproject.net.flow.criteria.ExtensionSelector;
+import org.onosproject.net.flow.criteria.ExtensionSelectorType;
+import org.onosproject.net.flow.instructions.ExtensionTreatment;
+import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
+import org.onosproject.net.flow.instructions.Instruction;
+import org.onosproject.net.flow.instructions.Instructions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour
+        implements FlowRuleProgrammable {
+
+    private final Logger log =
+            LoggerFactory.getLogger(this.getClass());
+
+    // Bmv2 doesn't support proper table dump, use a local store
+    // FIXME: synchronize entries with device
+    private final Map<FlowRule, FlowEntry> deviceEntriesMap = Maps.newHashMap();
+    private final Map<Integer, Set<FlowRule>> tableRulesMap = Maps.newHashMap();
+    private final Map<FlowRule, Long> tableEntryIdsMap = Maps.newHashMap();
+
+    @Override
+    public Collection<FlowEntry> getFlowEntries() {
+        return Collections.unmodifiableCollection(
+                deviceEntriesMap.values());
+    }
+
+    @Override
+    public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
+        Bmv2ThriftClient deviceClient;
+        try {
+            deviceClient = getDeviceClient();
+        } catch (Bmv2Exception e) {
+            return Collections.emptyList();
+        }
+
+        List<FlowRule> appliedFlowRules = Lists.newArrayList();
+
+        for (FlowRule rule : rules) {
+
+            Bmv2TableEntry entry;
+
+            try {
+                entry = parseFlowRule(rule);
+            } catch (IllegalStateException e) {
+                log.error("Unable to parse flow rule", e);
+                continue;
+            }
+
+            // Instantiate flowrule set for table if it does not exist
+            if (!tableRulesMap.containsKey(rule.tableId())) {
+                tableRulesMap.put(rule.tableId(), Sets.newHashSet());
+            }
+
+            if (tableRulesMap.get(rule.tableId()).contains(rule)) {
+                /* Rule is already installed in the table */
+                long entryId = tableEntryIdsMap.get(rule);
+
+                try {
+                    deviceClient.modifyTableEntry(
+                            entry.tableName(), entryId, entry.action());
+
+                    // Replace stored rule as treatment, etc. might have changed
+                    // Java Set doesn't replace on add, remove first
+                    tableRulesMap.get(rule.tableId()).remove(rule);
+                    tableRulesMap.get(rule.tableId()).add(rule);
+                    tableEntryIdsMap.put(rule, entryId);
+                    deviceEntriesMap.put(rule, new DefaultFlowEntry(
+                            rule, FlowEntry.FlowEntryState.ADDED, 0, 0, 0));
+                } catch (Bmv2Exception e) {
+                    log.error("Unable to update flow rule", e);
+                    continue;
+                }
+
+            } else {
+                /* Rule is new */
+                try {
+                    long entryId = deviceClient.addTableEntry(entry);
+
+                    tableRulesMap.get(rule.tableId()).add(rule);
+                    tableEntryIdsMap.put(rule, entryId);
+                    deviceEntriesMap.put(rule, new DefaultFlowEntry(
+                            rule, FlowEntry.FlowEntryState.ADDED, 0, 0, 0));
+                } catch (Bmv2Exception e) {
+                    log.error("Unable to add flow rule", e);
+                    continue;
+                }
+            }
+
+            appliedFlowRules.add(rule);
+        }
+
+        return Collections.unmodifiableCollection(appliedFlowRules);
+    }
+
+    @Override
+    public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
+        Bmv2ThriftClient deviceClient;
+        try {
+            deviceClient = getDeviceClient();
+        } catch (Bmv2Exception e) {
+            return Collections.emptyList();
+        }
+
+        List<FlowRule> removedFlowRules = Lists.newArrayList();
+
+        for (FlowRule rule : rules) {
+
+            if (tableEntryIdsMap.containsKey(rule)) {
+                long entryId = tableEntryIdsMap.get(rule);
+                String tableName = parseTableName(rule.tableId());
+
+                try {
+                    deviceClient.deleteTableEntry(tableName, entryId);
+                } catch (Bmv2Exception e) {
+                    log.error("Unable to delete flow rule", e);
+                    continue;
+                }
+
+                /* remove from local store */
+                tableEntryIdsMap.remove(rule);
+                tableRulesMap.get(rule.tableId()).remove(rule);
+                deviceEntriesMap.remove(rule);
+
+                removedFlowRules.add(rule);
+            }
+        }
+
+        return Collections.unmodifiableCollection(removedFlowRules);
+    }
+
+    private Bmv2TableEntry parseFlowRule(FlowRule flowRule) {
+
+        // TODO make it pipeline dependant, i.e. implement mapping
+
+        Bmv2TableEntry.Builder entryBuilder = Bmv2TableEntry.builder();
+
+        // Check selector
+        ExtensionCriterion ec =
+                (ExtensionCriterion) flowRule
+                        .selector().getCriterion(Criterion.Type.EXTENSION);
+        Preconditions.checkState(
+                flowRule.selector().criteria().size() == 1
+                        && ec != null,
+                "Selector must have only 1 criterion of type EXTENSION");
+        ExtensionSelector es = ec.extensionSelector();
+        Preconditions.checkState(
+                es.type() == ExtensionSelectorType.ExtensionSelectorTypes.P4_BMV2_MATCH_KEY.type(),
+                "ExtensionSelectorType must be P4_BMV2_MATCH_KEY");
+
+        // Selector OK, get Bmv2MatchKey
+        entryBuilder.withMatchKey(((Bmv2ExtensionSelector) es).matchKey());
+
+        // Check treatment
+        Instruction inst = flowRule.treatment().allInstructions().get(0);
+        Preconditions.checkState(
+                flowRule.treatment().allInstructions().size() == 1
+                        && inst.type() == Instruction.Type.EXTENSION,
+                "Treatment must have only 1 instruction of type EXTENSION");
+        ExtensionTreatment et =
+                ((Instructions.ExtensionInstructionWrapper) inst)
+                        .extensionInstruction();
+
+        Preconditions.checkState(
+                et.type() == ExtensionTreatmentType.ExtensionTreatmentTypes.P4_BMV2_ACTION.type(),
+                "ExtensionTreatmentType must be P4_BMV2_ACTION");
+
+        // Treatment OK, get Bmv2Action
+        entryBuilder.withAction(((Bmv2ExtensionTreatment) et).getAction());
+
+        // Table name
+        entryBuilder.withTableName(parseTableName(flowRule.tableId()));
+
+        if (!flowRule.isPermanent()) {
+            entryBuilder.withTimeout(flowRule.timeout());
+        }
+
+        entryBuilder.withPriority(flowRule.priority());
+
+        return entryBuilder.build();
+    }
+
+    private String parseTableName(int tableId) {
+        // TODO: map tableId with tableName according to P4 JSON
+        return "table" + String.valueOf(tableId);
+    }
+
+    private Bmv2ThriftClient getDeviceClient() throws Bmv2Exception {
+        try {
+            return Bmv2ThriftClient.of(handler().data().deviceId());
+        } catch (Bmv2Exception e) {
+            log.error("Failed to connect to Bmv2 device", e);
+            throw e;
+        }
+    }
+}
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);
+    }
+}
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..95cb23d
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * BMv2 driver implementation.
+ */
+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..279d470
--- /dev/null
+++ b/drivers/bmv2/src/main/resources/bmv2-drivers.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  ~ 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.
+  -->
+<drivers>
+    <driver name="bmv2-thrift" manufacturer="p4.org" hwVersion="bmv2" swVersion="unknown">
+        <behaviour api="org.onosproject.net.behaviour.PortDiscovery"
+                   impl="org.onosproject.drivers.bmv2.Bmv2PortGetterDriver"/>
+        <behaviour api="org.onosproject.net.flow.FlowRuleProgrammable"
+                   impl="org.onosproject.drivers.bmv2.Bmv2FlowRuleDriver"/>
+    </driver>
+</drivers>