ONOS-3961 ONOS-4285 Implemented BMv2 drivers and protocol

Provides Thrift-based implementation for FlowRuleProgrammable and
PortDiscovery behaviours.

Change-Id: Ibbf8720d92301bcd23c5c583d156f464015ff1ef
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Action.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Action.java
new file mode 100644
index 0000000..5bbc50b
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Action.java
@@ -0,0 +1,138 @@
+/*
+ * 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.bmv2.api;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.Lists;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Bmv2 Action representation.
+ */
+public final class Bmv2Action {
+
+    private final String name;
+    private final List<ByteBuffer> parameters;
+
+    private Bmv2Action(String name, List<ByteBuffer> parameters) {
+        // hide constructor
+        this.name = name;
+        this.parameters = parameters;
+    }
+
+    /**
+     * Returns a new action builder.
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Get action name.
+     *
+     * @return action name
+     */
+    public final String name() {
+        return name;
+    }
+
+    /**
+     * Get list of action parameters ordered as per P4 action definition.
+     *
+     * @return List of action parameters
+     */
+    public final List<ByteBuffer> parameters() {
+        return Collections.unmodifiableList(parameters);
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(name, parameters);
+    }
+
+    @Override
+    public final boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final Bmv2Action other = (Bmv2Action) obj;
+        return Objects.equals(this.name, other.name)
+                && Objects.equals(this.parameters, other.parameters);
+    }
+
+    @Override
+    public final String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("name", name)
+                .add("parameters", parameters)
+                .toString();
+    }
+
+    /**
+     * A Bmv2 action builder.
+     */
+    public static final class Builder {
+
+        private String name;
+        private List<ByteBuffer> parameters;
+
+        private Builder() {
+            this.parameters = Lists.newArrayList();
+        }
+
+        /**
+         * Set the action name.
+         *
+         * @param actionName a string value
+         * @return this
+         */
+        public Builder withName(String actionName) {
+            this.name = actionName;
+            return this;
+        }
+
+        /**
+         * Add a parameter at the end of the parameters list.
+         *
+         * @param parameter a ByteBuffer value
+         * @return this
+         */
+        public Builder addParameter(ByteBuffer parameter) {
+            parameters.add(parameter);
+            return this;
+        }
+
+        /**
+         * Builds a Bmv2 action object.
+         *
+         * @return a Bmv2 action
+         */
+        public Bmv2Action build() {
+            checkNotNull(name, "Action name not set");
+            return new Bmv2Action(name, parameters);
+        }
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Exception.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Exception.java
new file mode 100644
index 0000000..a428491
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Exception.java
@@ -0,0 +1,27 @@
+/*
+ * 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.bmv2.api;
+
+/**
+ * General Bmv2 exception.
+ */
+public class Bmv2Exception extends Exception {
+
+    public Bmv2Exception(String message, Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionSelector.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionSelector.java
new file mode 100644
index 0000000..1bd9f1c
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionSelector.java
@@ -0,0 +1,51 @@
+/*
+ * 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.bmv2.api;
+
+import org.onlab.util.KryoNamespace;
+import org.onosproject.net.flow.AbstractExtension;
+import org.onosproject.net.flow.criteria.ExtensionSelector;
+import org.onosproject.net.flow.criteria.ExtensionSelectorType;
+
+public class Bmv2ExtensionSelector extends AbstractExtension implements ExtensionSelector {
+
+    private final KryoNamespace appKryo = new KryoNamespace.Builder().build();
+    private Bmv2MatchKey matchKey;
+
+    public Bmv2ExtensionSelector(Bmv2MatchKey matchKey) {
+        this.matchKey = matchKey;
+    }
+
+    public Bmv2MatchKey matchKey() {
+        return matchKey;
+    }
+
+    @Override
+    public ExtensionSelectorType type() {
+        return ExtensionSelectorType.ExtensionSelectorTypes.P4_BMV2_MATCH_KEY.type();
+    }
+
+    @Override
+    public byte[] serialize() {
+        return appKryo.serialize(matchKey);
+    }
+
+    @Override
+    public void deserialize(byte[] data) {
+        matchKey = appKryo.deserialize(data);
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionTreatment.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionTreatment.java
new file mode 100644
index 0000000..cf48e38
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionTreatment.java
@@ -0,0 +1,51 @@
+/*
+ * 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.bmv2.api;
+
+import org.onlab.util.KryoNamespace;
+import org.onosproject.net.flow.AbstractExtension;
+import org.onosproject.net.flow.instructions.ExtensionTreatment;
+import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
+
+public class Bmv2ExtensionTreatment extends AbstractExtension implements ExtensionTreatment {
+
+    private final KryoNamespace appKryo = new KryoNamespace.Builder().build();
+    private Bmv2Action action;
+
+    public Bmv2ExtensionTreatment(Bmv2Action action) {
+        this.action = action;
+    }
+
+    public Bmv2Action getAction() {
+        return action;
+    }
+
+    @Override
+    public ExtensionTreatmentType type() {
+        return ExtensionTreatmentType.ExtensionTreatmentTypes.P4_BMV2_ACTION.type();
+    }
+
+    @Override
+    public byte[] serialize() {
+        return appKryo.serialize(action);
+    }
+
+    @Override
+    public void deserialize(byte[] data) {
+        action = appKryo.deserialize(data);
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2MatchKey.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2MatchKey.java
new file mode 100644
index 0000000..d068328
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2MatchKey.java
@@ -0,0 +1,175 @@
+/*
+ * 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.bmv2.api;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.Lists;
+import org.p4.bmv2.thrift.BmMatchParam;
+import org.p4.bmv2.thrift.BmMatchParamExact;
+import org.p4.bmv2.thrift.BmMatchParamLPM;
+import org.p4.bmv2.thrift.BmMatchParamTernary;
+import org.p4.bmv2.thrift.BmMatchParamType;
+import org.p4.bmv2.thrift.BmMatchParamValid;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Bmv2 match key representation.
+ */
+public final class Bmv2MatchKey {
+
+    private final List<BmMatchParam> matchParams;
+
+    /**
+     * Creates a new match key.
+     *
+     * @param matchParams The ordered list of match parameters
+     */
+    private Bmv2MatchKey(List<BmMatchParam> matchParams) {
+        this.matchParams = matchParams;
+    }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Returns the match parameters defined for this match key (read-only).
+     *
+     * @return match parameters
+     */
+    public final List<BmMatchParam> bmMatchParams() {
+        return Collections.unmodifiableList(matchParams);
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hashCode(matchParams);
+    }
+
+    @Override
+    public final boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final Bmv2MatchKey other = (Bmv2MatchKey) obj;
+
+        return Objects.equals(this.matchParams, other.matchParams);
+    }
+
+    @Override
+    public final String toString() {
+        return MoreObjects.toStringHelper(this)
+                .addValue(matchParams)
+                .toString();
+    }
+
+    /**
+     * Builder of a Bmv2 match key.
+     */
+    public static final class Builder {
+
+        private List<BmMatchParam> matchParams;
+
+        private Builder() {
+            this.matchParams = Lists.newArrayList();
+        }
+
+        /**
+         * Adds an exact match parameter.
+         *
+         * @param key a ByteBuffer value
+         * @return this
+         */
+        public Builder withExact(ByteBuffer key) {
+            this.matchParams.add(
+                    new BmMatchParam(BmMatchParamType.EXACT)
+                            .setExact(new BmMatchParamExact(key)));
+            return this;
+        }
+
+
+        /**
+         * Adds a longest prefix match parameter.
+         *
+         * @param key          a ByteBuffer value
+         * @param prefixLength an integer value
+         * @return this
+         */
+        public Builder withLpm(ByteBuffer key, int prefixLength) {
+            this.matchParams.add(
+                    new BmMatchParam(BmMatchParamType.LPM)
+                            .setLpm(new BmMatchParamLPM(key, prefixLength)));
+            return this;
+        }
+
+        /**
+         * Adds a ternary match parameter.
+         *
+         * @param key  a ByteBuffer value
+         * @param mask an ByteBuffer value
+         * @return this
+         */
+        public Builder withTernary(ByteBuffer key, ByteBuffer mask) {
+            this.matchParams.add(
+                    new BmMatchParam(BmMatchParamType.TERNARY).
+                            setTernary(new BmMatchParamTernary(key, mask)));
+            return this;
+        }
+
+        /**
+         * Adds a ternary match parameter where all bits are don't-care.
+         *
+         * @param byteLength an integer value representing the length in byte of the parameter
+         * @return this
+         */
+        public Builder withWildcard(int byteLength) {
+            byte[] zeros = new byte[byteLength];
+            Arrays.fill(zeros, (byte) 0);
+            return this.withTernary(ByteBuffer.wrap(zeros), ByteBuffer.wrap(zeros));
+        }
+
+        /**
+         * Adds a valid match parameter.
+         *
+         * @param key a boolean value
+         * @return this
+         */
+        public Builder withValid(boolean key) {
+            this.matchParams.add(
+                    new BmMatchParam(BmMatchParamType.VALID)
+                            .setValid(new BmMatchParamValid(key)));
+            return this;
+        }
+
+        /**
+         * Builds a new match key object.
+         *
+         * @return match key
+         */
+        public Bmv2MatchKey build() {
+            return new Bmv2MatchKey(this.matchParams);
+        }
+    }
+}
\ No newline at end of file
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2PortInfo.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2PortInfo.java
new file mode 100644
index 0000000..3f36ade
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2PortInfo.java
@@ -0,0 +1,58 @@
+/*
+ * 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.bmv2.api;
+
+import com.google.common.base.MoreObjects;
+import org.p4.bmv2.thrift.DevMgrPortInfo;
+
+import java.util.Collections;
+import java.util.Map;
+
+/**
+ * Bmv2 representation of a switch port information.
+ */
+public final class Bmv2PortInfo {
+
+    private final DevMgrPortInfo portInfo;
+
+    public Bmv2PortInfo(DevMgrPortInfo portInfo) {
+        this.portInfo = portInfo;
+    }
+
+    public final String ifaceName() {
+        return portInfo.getIface_name();
+    }
+
+    public final int portNumber() {
+        return portInfo.getPort_num();
+    }
+
+    public final boolean isUp() {
+        return portInfo.isIs_up();
+    }
+
+    public final Map<String, String> getExtraProperties() {
+        return Collections.unmodifiableMap(portInfo.getExtra());
+    }
+
+    @Override
+    public final String toString() {
+        return MoreObjects.toStringHelper(this)
+                .addValue(portInfo)
+                .toString();
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2TableEntry.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2TableEntry.java
new file mode 100644
index 0000000..4bf0291
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2TableEntry.java
@@ -0,0 +1,219 @@
+/*
+ * 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.bmv2.api;
+
+import com.google.common.base.Preconditions;
+
+import java.util.Objects;
+
+/**
+ * Bmv2 representation of a table entry.
+ */
+public final class Bmv2TableEntry {
+
+    private static final int NO_PRIORITY_VALUE = -1;
+    private static final int NO_TIMEOUT_VALUE = -1;
+
+    private final String tableName;
+    private final Bmv2MatchKey matchKey;
+    private final Bmv2Action action;
+    private final int priority;
+    private final double timeout;
+
+    private Bmv2TableEntry(String tableName, Bmv2MatchKey matchKey,
+                           Bmv2Action action, int priority, double timeout) {
+        this.tableName = tableName;
+        this.matchKey = matchKey;
+        this.action = action;
+        this.priority = priority;
+        this.timeout = timeout;
+    }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Returns the name of the table where this entry is installed.
+     *
+     * @return table name
+     */
+    public final String tableName() {
+        return this.tableName;
+    }
+
+    /**
+     * Returns the match key.
+     *
+     * @return match key
+     */
+    public final Bmv2MatchKey matchKey() {
+        return matchKey;
+    }
+
+    /**
+     * Returns the action.
+     *
+     * @return action
+     */
+    public final Bmv2Action action() {
+        return action;
+    }
+
+    /**
+     * Returns true is the entry has a valid priority value set.
+     *
+     * @return true if priority is set, false elsewhere
+     */
+    public final boolean hasPriority() {
+        return this.priority != NO_PRIORITY_VALUE;
+    }
+
+    /**
+     * Return the entry priority.
+     *
+     * @return priority
+     */
+    public final int priority() {
+        return priority;
+    }
+
+    /**
+     * Returns true is the entry has a valid timeout value set.
+     *
+     * @return true if timeout is set, false elsewhere
+     */
+    public final boolean hasTimeout() {
+        return this.timeout != NO_PRIORITY_VALUE;
+    }
+
+    /**
+     * Returns the entry timeout in fractional seconds.
+     *
+     * @return timeout
+     */
+    public final double timeout() {
+        return timeout;
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(matchKey, action, priority, timeout);
+    }
+
+    @Override
+    public final boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final Bmv2TableEntry other = (Bmv2TableEntry) obj;
+        return Objects.equals(this.matchKey, other.matchKey)
+                && Objects.equals(this.action, other.action)
+                && Objects.equals(this.priority, other.priority)
+                && Objects.equals(this.timeout, other.timeout);
+    }
+
+    @Override
+    public final String toString() {
+        return com.google.common.base.MoreObjects.toStringHelper(this)
+                .addValue(matchKey)
+                .addValue(action)
+                .add("priority", priority)
+                .add("timeout", timeout)
+                .toString();
+    }
+
+    public static final class Builder {
+
+        private String tableName;
+        private Bmv2MatchKey matchKey;
+        private Bmv2Action action;
+        private int priority = NO_PRIORITY_VALUE;
+        private double timeout = NO_TIMEOUT_VALUE;
+
+        private Builder() {
+            // hide constructor
+        }
+
+        /**
+         * Sets the table name.
+         *
+         * @param tableName a string value
+         * @return this
+         */
+        public Builder withTableName(String tableName) {
+            this.tableName = tableName;
+            return this;
+        }
+
+        /**
+         * Sets the match key.
+         *
+         * @param matchKey a match key value
+         * @return this
+         */
+        public Builder withMatchKey(Bmv2MatchKey matchKey) {
+            this.matchKey = matchKey;
+            return this;
+        }
+
+        /**
+         * Sets the action.
+         *
+         * @param action an action value
+         * @return this
+         */
+        public Builder withAction(Bmv2Action action) {
+            this.action = action;
+            return this;
+        }
+
+        public Builder withPriority(int priority) {
+            this.priority = priority;
+            return this;
+        }
+
+        /**
+         * Sets the timeout.
+         *
+         * @param timeout a timeout value in fractional seconds
+         * @return this
+         */
+        public Builder withTimeout(double timeout) {
+            this.timeout = timeout;
+            return this;
+        }
+
+        /**
+         * Build the table entry.
+         *
+         * @return a new table entry object
+         */
+        public Bmv2TableEntry build() {
+            Preconditions.checkNotNull(tableName);
+            Preconditions.checkNotNull(matchKey);
+            Preconditions.checkNotNull(action);
+
+            return new Bmv2TableEntry(tableName, matchKey, action, priority,
+                                      timeout);
+
+        }
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/package-info.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/package-info.java
new file mode 100644
index 0000000..d8946e3
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/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 API abstractions.
+ */
+package org.onosproject.bmv2.api;
\ No newline at end of file