Various Bmv2 protocol refactorings in preparation of the flow rule
translator (ONOS-4044)

- Added new classes for different match parameters (exact, ternary, lpm,
  valid)
- Divided api package in two sub-packages, model (previously under
  drivers) and runtime (old api package)
- Improved Bmv2ThriftClient caching and table entry handling

Change-Id: I23c174cf3e8f9f6ecddb99c2d09dc531e8f1c73f
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2Action.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2Action.java
new file mode 100644
index 0000000..9b2a209
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2Action.java
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.Lists;
+import org.onlab.util.ImmutableByteSequence;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+
+/**
+ * Bmv2 action representation.
+ */
+public final class Bmv2Action {
+
+    private final String name;
+    private final List<ImmutableByteSequence> parameters;
+
+    private Bmv2Action(String name, List<ImmutableByteSequence> parameters) {
+        // hide constructor
+        this.name = name;
+        this.parameters = parameters;
+    }
+
+    /**
+     * Returns a new action builder.
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Return the name of this action.
+     *
+     * @return action name
+     */
+    public final String name() {
+        return name;
+    }
+
+    /**
+     * Returns an immutable view of the ordered list of parameters of this
+     * action.
+     *
+     * @return list of byte sequence
+     */
+    public final List<ImmutableByteSequence> 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 = null;
+        private List<ImmutableByteSequence> 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 = checkNotNull(actionName);
+            return this;
+        }
+
+        /**
+         * Add a parameter at the end of the parameters list.
+         *
+         * @param parameter a ByteBuffer value
+         * @return this
+         */
+        public Builder addParameter(ImmutableByteSequence parameter) {
+            parameters.add(checkNotNull(parameter));
+            return this;
+        }
+
+        /**
+         * Builds a Bmv2 action object.
+         *
+         * @return a Bmv2 action
+         */
+        public Bmv2Action build() {
+            checkState(name != null, "action name not set");
+            return new Bmv2Action(name, parameters);
+        }
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ExactMatchParam.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ExactMatchParam.java
new file mode 100644
index 0000000..d23bfc6
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ExactMatchParam.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import org.onlab.util.ImmutableByteSequence;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Representation of a Bmv2 exact match parameter.
+ */
+public class Bmv2ExactMatchParam implements Bmv2MatchParam {
+
+    private final ImmutableByteSequence value;
+
+    /**
+     * Creates a new match parameter object that matches exactly on the
+     * given byte sequence.
+     *
+     * @param value a byte sequence value
+     */
+    public Bmv2ExactMatchParam(ImmutableByteSequence value) {
+        this.value = checkNotNull(value, "value cannot be null");
+    }
+
+    @Override
+    public Type type() {
+        return Type.EXACT;
+    }
+
+    /**
+     * Return the byte sequence value matched by this parameter.
+     *
+     * @return an immutable byte buffer value
+     */
+    public ImmutableByteSequence value() {
+        return this.value;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(value);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final Bmv2ExactMatchParam other = (Bmv2ExactMatchParam) obj;
+        return Objects.equal(this.value, other.value);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("value", value)
+                .toString();
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ExtensionSelector.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ExtensionSelector.java
new file mode 100644
index 0000000..bf44a0e
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ExtensionSelector.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+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;
+
+/**
+ * Extension selector for Bmv2 used as a wrapper for a {@link Bmv2MatchKey}.
+ */
+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/runtime/Bmv2ExtensionTreatment.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ExtensionTreatment.java
new file mode 100644
index 0000000..12ab1e3
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ExtensionTreatment.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+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;
+
+/**
+ * Extension treatment for Bmv2 used as a wrapper for a {@link Bmv2Action}.
+ */
+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/runtime/Bmv2LpmMatchParam.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2LpmMatchParam.java
new file mode 100644
index 0000000..29d5279
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2LpmMatchParam.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import org.onlab.util.ImmutableByteSequence;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Representation of a Bmv2 longest prefix match (LPM) parameter.
+ */
+public class Bmv2LpmMatchParam implements Bmv2MatchParam {
+
+    private final ImmutableByteSequence value;
+    private final int prefixLength;
+
+    /**
+     * Creates a new LPM parameter using the given byte sequence value and
+     * prefix length.
+     *
+     * @param value        a byte sequence value
+     * @param prefixLength an integer value
+     */
+    public Bmv2LpmMatchParam(ImmutableByteSequence value, int prefixLength) {
+        checkArgument(prefixLength >= 0, "prefix length cannot be negative");
+        this.value = checkNotNull(value);
+        this.prefixLength = prefixLength;
+    }
+
+    @Override
+    public Bmv2MatchParam.Type type() {
+        return Type.LPM;
+    }
+
+    /**
+     * Returns the byte sequence value of this parameter.
+     *
+     * @return a byte sequence value
+     */
+    public ImmutableByteSequence value() {
+        return this.value;
+    }
+
+    /**
+     * Returns the prefix length of this parameter.
+     *
+     * @return an integer value
+     */
+    public int prefixLength() {
+        return this.prefixLength;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(value, prefixLength);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final Bmv2LpmMatchParam other = (Bmv2LpmMatchParam) obj;
+        return Objects.equal(this.value, other.value)
+                && Objects.equal(this.prefixLength, other.prefixLength);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("value", value)
+                .add("prefixLength", prefixLength)
+                .toString();
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2MatchKey.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2MatchKey.java
new file mode 100644
index 0000000..cdcfcd9
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2MatchKey.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.Lists;
+import org.onlab.util.ImmutableByteSequence;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Bmv2 match key representation.
+ */
+public final class Bmv2MatchKey {
+
+    private final List<Bmv2MatchParam> matchParams;
+
+    private Bmv2MatchKey(List<Bmv2MatchParam> matchParams) {
+        // ban constructor
+        this.matchParams = matchParams;
+    }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Returns an immutable view of the ordered list of match parameters of this
+     * match key.
+     *
+     * @return list match parameters
+     */
+    public final List<Bmv2MatchParam> matchParams() {
+        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<Bmv2MatchParam> matchParams;
+
+        private Builder() {
+            this.matchParams = Lists.newArrayList();
+        }
+
+        public Builder add(Bmv2MatchParam param) {
+            this.matchParams.add(checkNotNull(param));
+            return this;
+        }
+
+        /**
+         * Adds a ternary match parameter where all bits are don't-care.
+         *
+         * @param byteLength length in bytes of the parameter
+         * @return this
+         */
+        public Builder withWildcard(int byteLength) {
+            checkArgument(byteLength > 0, "length must be a positive integer");
+            return add(new Bmv2TernaryMatchParam(
+                    ImmutableByteSequence.ofZeros(byteLength),
+                    ImmutableByteSequence.ofZeros(byteLength)));
+        }
+
+        /**
+         * 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/runtime/Bmv2MatchParam.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2MatchParam.java
new file mode 100644
index 0000000..35bac79
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2MatchParam.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+/**
+ * Representation of a Bmv2 match parameter.
+ */
+public interface Bmv2MatchParam {
+
+    /**
+     * Returns the match type of this parameter.
+     *
+     * @return a match type value
+     */
+    Type type();
+
+    /**
+     * Bmv2 match types.
+     */
+    enum Type {
+        /**
+         * Exact match type.
+         */
+        EXACT,
+        /**
+         * Ternary match type.
+         */
+        TERNARY,
+        /**
+         * Longest-prefix match type.
+         */
+        LPM,
+        /**
+         * Valid match type.
+         */
+        VALID;
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2PortInfo.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2PortInfo.java
new file mode 100644
index 0000000..c6be426
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2PortInfo.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+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/runtime/Bmv2RuntimeException.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2RuntimeException.java
new file mode 100644
index 0000000..f7153c5
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2RuntimeException.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+/**
+ * General exception of the Bmv2 runtime APIs.
+ */
+public class Bmv2RuntimeException extends Exception {
+
+    public Bmv2RuntimeException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public Bmv2RuntimeException(String message) {
+        super(message);
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2TableEntry.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2TableEntry.java
new file mode 100644
index 0000000..96adadd
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2TableEntry.java
@@ -0,0 +1,223 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * 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;
+    }
+
+    /**
+     * Returns a new Bmv2 table entry builder.
+     *
+     * @return a new builder.
+     */
+    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 = checkNotNull(tableName, "table name cannot be null");
+            return this;
+        }
+
+        /**
+         * Sets the match key.
+         *
+         * @param matchKey a match key value
+         * @return this
+         */
+        public Builder withMatchKey(Bmv2MatchKey matchKey) {
+            this.matchKey = checkNotNull(matchKey, "match key cannot be null");
+            return this;
+        }
+
+        /**
+         * Sets the action.
+         *
+         * @param action an action value
+         * @return this
+         */
+        public Builder withAction(Bmv2Action action) {
+            this.action = checkNotNull(action, "action cannot be null");
+            return this;
+        }
+
+        public Builder withPriority(int priority) {
+            checkArgument(priority >= 0, "priority cannot be negative");
+            this.priority = priority;
+            return this;
+        }
+
+        /**
+         * Sets the timeout.
+         *
+         * @param timeout a timeout value in fractional seconds
+         * @return this
+         */
+        public Builder withTimeout(double timeout) {
+            checkArgument(timeout > 0, "timeout must be a positive non-zero value");
+            this.timeout = timeout;
+            return this;
+        }
+
+        /**
+         * Build the table entry.
+         *
+         * @return a new table entry object
+         */
+        public Bmv2TableEntry build() {
+            return new Bmv2TableEntry(tableName, matchKey, action, priority,
+                                      timeout);
+
+        }
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2TernaryMatchParam.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2TernaryMatchParam.java
new file mode 100644
index 0000000..76d42c3
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2TernaryMatchParam.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import org.onlab.util.ImmutableByteSequence;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+
+/**
+ * Representation of a Bmv2 ternary match parameter.
+ */
+public class Bmv2TernaryMatchParam implements Bmv2MatchParam {
+
+    private final ImmutableByteSequence value;
+    private final ImmutableByteSequence mask;
+
+    /**
+     * Creates a new ternary match parameter using the given byte sequences of
+     * value and mask.
+     *
+     * @param value a byte sequence value
+     * @param mask  a byte sequence value
+     */
+    public Bmv2TernaryMatchParam(ImmutableByteSequence value,
+                                 ImmutableByteSequence mask) {
+        this.value = checkNotNull(value, "value cannot be null");
+        this.mask = checkNotNull(mask, "value cannot be null");
+        checkState(value.size() == mask.size(),
+                   "value and mask must have equal size");
+    }
+
+    @Override
+    public Type type() {
+        return Type.TERNARY;
+    }
+
+    /**
+     * Returns the byte sequence value of by this parameter.
+     *
+     * @return a byte sequence value
+     */
+    public ImmutableByteSequence value() {
+        return this.value;
+    }
+
+    /**
+     * Returns the byte sequence mask of by this parameter.
+     *
+     * @return a byte sequence value
+     */
+    public ImmutableByteSequence mask() {
+        return this.mask;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(value, mask);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final Bmv2TernaryMatchParam other = (Bmv2TernaryMatchParam) obj;
+        return Objects.equal(this.value, other.value)
+                && Objects.equal(this.mask, other.mask);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("value", value)
+                .add("mask", mask)
+                .toString();
+    }
+}
\ No newline at end of file
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ValidMatchParam.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ValidMatchParam.java
new file mode 100644
index 0000000..31080d5
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/Bmv2ValidMatchParam.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2016-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.bmv2.api.runtime;
+
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+/**
+ * Representation of a Bmv2 valid match parameter.
+ */
+public class Bmv2ValidMatchParam implements Bmv2MatchParam {
+
+    private final boolean flag;
+
+    /**
+     * Creates a new valid match parameter using the given boolean flag.
+     *
+     * @param flag a boolean value
+     */
+    public Bmv2ValidMatchParam(boolean flag) {
+        this.flag = flag;
+    }
+
+    @Override
+    public Type type() {
+        return Type.VALID;
+    }
+
+    /**
+     * Returns the boolean flag of this parameter.
+     *
+     * @return a boolean value
+     */
+    public boolean flag() {
+        return flag;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(flag);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final Bmv2ValidMatchParam other = (Bmv2ValidMatchParam) obj;
+        return this.flag == other.flag;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("flag", flag)
+                .toString();
+    }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/package-info.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/package-info.java
new file mode 100644
index 0000000..8c11944
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/runtime/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-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.
+ */
+
+/**
+ * Bmv2 runtime APIs.
+ */
+package org.onosproject.bmv2.api.runtime;
\ No newline at end of file