ONOS-7251 - Initial implementation of fabric.p4 L2 broadcast feature.

Thrift client cherry-picked from the commit dd5792ac9ee38a702c3128a34224852b5c284687

Change-Id: I989f2b2074485a892195889a7c976b518510da88
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DeviceHandshaker.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DeviceHandshaker.java
new file mode 100644
index 0000000..efb7b77
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DeviceHandshaker.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.drivers.bmv2.api.Bmv2PreController;
+import org.onosproject.drivers.p4runtime.P4RuntimeHandshaker;
+import org.onosproject.net.DeviceId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.CompletableFuture;
+
+public class Bmv2DeviceHandshaker extends P4RuntimeHandshaker {
+
+    public static final String THRIFT_SERVER_ADDRESS_KEY = "bmv2-thrift_ip";
+    public static final String THRIFT_SERVER_PORT_KEY = "bmv2-thrift_port";
+    protected final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Override
+    public CompletableFuture<Boolean> connect() {
+        //connect to both GRPC and Thrift servers in parallel
+        CompletableFuture<Boolean> futureP4Runtime = super.connect();
+        CompletableFuture<Boolean> futureBmv2Pre = connectToBmv2Pre();
+        //combine futures and asses the overall result by using P4Runtime connection status
+        return futureP4Runtime.thenCombine(futureBmv2Pre,
+                                           (p4RuntimeConnected, bmv2PreConnected) -> p4RuntimeConnected);
+    }
+
+    @Override
+    public CompletableFuture<Boolean> disconnect() {
+        //disconnect from both GRPC and Thrift servers in parallel
+        CompletableFuture<Boolean> futureP4Runtime = super.disconnect();
+        CompletableFuture<Boolean> futureBmv2Pre = disconnectFromBmv2Pre();
+        //combine futures and asses the overall result by using P4Runtime disconnection status
+        return futureP4Runtime.thenCombine(futureBmv2Pre,
+                                           (p4RuntimeDisconnected, bmv2PreDisconnected) -> p4RuntimeDisconnected);
+    }
+
+    private CompletableFuture<Boolean> connectToBmv2Pre() {
+        return CompletableFuture.supplyAsync(this::createPreClient);
+    }
+
+    private CompletableFuture<Boolean> disconnectFromBmv2Pre() {
+        return CompletableFuture.supplyAsync(() -> {
+            removePreClient();
+            return true;
+        });
+    }
+
+    /**
+     * Creates a BMv2 PRE client for this device.
+     *
+     * @return true if successful; false otherwise
+     */
+    private boolean createPreClient() {
+        DeviceId deviceId = handler().data().deviceId();
+
+        String serverAddress = data().value(THRIFT_SERVER_ADDRESS_KEY);
+        String serverPortString = data().value(THRIFT_SERVER_PORT_KEY);
+
+        if (serverAddress == null || serverPortString == null) {
+            log.warn("Unable to create PRE client for {}, missing driver data key (required is {}, and {})",
+                     deviceId, THRIFT_SERVER_ADDRESS_KEY, THRIFT_SERVER_PORT_KEY);
+            return false;
+        }
+        Bmv2PreController bmv2PreController = handler().get(Bmv2PreController.class);
+        try {
+            return bmv2PreController.createPreClient(deviceId,
+                                                     serverAddress,
+                                                     Integer.parseUnsignedInt(serverPortString));
+        } catch (RuntimeException e) {
+            log.warn("Unable to create BMv2 PRE client for {} due to {}", deviceId, e.toString());
+            return false;
+        }
+    }
+
+    private void removePreClient() {
+        DeviceId deviceId = handler().data().deviceId();
+        handler().get(Bmv2PreController.class).removePreClient(deviceId);
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2GroupProgrammable.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2GroupProgrammable.java
new file mode 100644
index 0000000..1ab518f
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2GroupProgrammable.java
@@ -0,0 +1,257 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableList;
+import org.onosproject.core.GroupId;
+import org.onosproject.drivers.bmv2.api.Bmv2DeviceAgent;
+import org.onosproject.drivers.bmv2.api.Bmv2PreController;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreGroup;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreGroupHandle;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2RuntimeException;
+import org.onosproject.drivers.bmv2.impl.Bmv2PreGroupTranslatorImpl;
+import org.onosproject.drivers.bmv2.mirror.Bmv2PreGroupMirror;
+import org.onosproject.drivers.p4runtime.P4RuntimeGroupProgrammable;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.group.Group;
+import org.onosproject.net.group.GroupDescription;
+import org.onosproject.net.group.GroupOperation;
+import org.onosproject.net.group.GroupOperations;
+import org.slf4j.Logger;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of the group programmable behaviour for BMv2.
+ */
+public class Bmv2GroupProgrammable extends P4RuntimeGroupProgrammable {
+
+    private static final Logger log = getLogger(Bmv2GroupProgrammable.class);
+
+    private static final int PRE_GROUP_LOCK_EXPIRE_TIME_IN_MIN = 10;
+
+    // Needed to synchronize operations over the same group.
+    private static final LoadingCache<Bmv2PreGroupHandle, Lock> PRE_GROUP_LOCKS = CacheBuilder.newBuilder()
+            .expireAfterAccess(PRE_GROUP_LOCK_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES)
+            .build(new CacheLoader<Bmv2PreGroupHandle, Lock>() {
+                @Override
+                public Lock load(Bmv2PreGroupHandle bmv2PreGroupHandle) {
+                    return new ReentrantLock();
+                }
+            });
+
+    private Bmv2PreGroupMirror preGroupMirror;
+    private Bmv2PreController bmv2PreController;
+
+    @Override
+    protected boolean setupBehaviour() {
+        if (!super.setupBehaviour()) {
+            return false;
+        }
+
+        preGroupMirror = handler().get(Bmv2PreGroupMirror.class);
+        bmv2PreController = handler().get(Bmv2PreController.class);
+
+        return getBmv2DeviceAgent() != null
+                && preGroupMirror != null
+                && bmv2PreController != null;
+    }
+
+    @Override
+    public void performGroupOperation(DeviceId deviceId,
+                                      GroupOperations groupOps) {
+        if (!setupBehaviour()) {
+            return;
+        }
+        groupOps.operations().forEach(op -> processGroupOp(deviceId, op));
+    }
+
+
+    /**
+     * Fetches all groups of P4Runtime and BMv2 PRE in a device. Combines and returns them respectively.
+     *
+     * @return all the groups which are managed via both P4Runtime and BMv2 PRE
+     */
+    @Override
+    public Collection<Group> getGroups() {
+        //get groups managed via P4Runtime
+        Collection<Group> groups = getP4Groups();
+        //get groups managed via BMv2 Thrift
+        groups.addAll(getPreGroups());
+        return ImmutableList.copyOf(groups);
+    }
+
+    private Collection<Group> getP4Groups() {
+        return super.getGroups();
+    }
+
+    /**
+     * Returns BMv2 agent associated with a BMv2 device.
+     *
+     * @return BMv2 agent
+     */
+    private Bmv2DeviceAgent getBmv2DeviceAgent() {
+        return bmv2PreController.getPreClient(deviceId);
+    }
+
+    /**
+     * Retrieves groups of BMv2 PRE.
+     *
+     * @return collection of PRE groups
+     */
+    private Collection<Group> getPreGroups() {
+        if (!setupBehaviour()) {
+            return Collections.emptyList();
+        }
+        Bmv2DeviceAgent bmv2DeviceAgent = getBmv2DeviceAgent();
+
+        try {
+            return bmv2DeviceAgent.getPreGroups().stream()
+                    .map(preGroup -> groupStore.getGroup(deviceId, GroupId.valueOf(preGroup.groupId())))
+                    .collect(Collectors.toList());
+        } catch (Bmv2RuntimeException e) {
+            log.error("Exception while getting Bmv2 PRE groups of {}", deviceId, e);
+            return Collections.emptyList();
+        }
+    }
+
+    /**
+     * Checks whether specified group is a PRE group or not.
+     *
+     * @param group group
+     * @return Returns true iff this group is a PRE group; false otherwise.
+     */
+    private boolean isPreGroup(Group group) {
+        return group.type().equals(GroupDescription.Type.ALL);
+    }
+
+    /**
+     * Makes a decision between two methodologies over group type.
+     * A group of ALL type is evaluated by GroupProgrammable of BMv2;
+     * it is passed on to GroupProgrammable of P4Runtime otherwise.
+     *
+     * @param deviceId ID of the device on which the group is being accommodated.
+     * @param groupOp  group operation
+     */
+    private void processGroupOp(DeviceId deviceId, GroupOperation groupOp) {
+        final Group group = groupStore.getGroup(deviceId, groupOp.groupId());
+
+        if (isPreGroup(group)) {
+            processPreGroupOp(deviceId, groupOp);
+        } else {
+            //means the group is managed via P4Runtime.
+            super.performGroupOperation(deviceId,
+                                        new GroupOperations(Arrays.asList(new GroupOperation[]{groupOp})));
+        }
+    }
+
+    private void processPreGroupOp(DeviceId deviceId, GroupOperation groupOp) {
+        if (!setupBehaviour()) {
+            return;
+        }
+
+        final Group group = groupStore.getGroup(deviceId, groupOp.groupId());
+
+        Bmv2PreGroup preGroup = Bmv2PreGroupTranslatorImpl.translate(group);
+
+        final Bmv2PreGroupHandle handle = Bmv2PreGroupHandle.of(deviceId, preGroup);
+
+        final Bmv2PreGroup groupOnDevice = preGroupMirror.get(handle);
+
+        PRE_GROUP_LOCKS.getUnchecked(handle).lock();
+        try {
+            switch (groupOp.opType()) {
+                case ADD:
+                    onAdd(preGroup, handle);
+                    break;
+                case MODIFY:
+                    onModify(preGroup, groupOnDevice, handle);
+                    break;
+                case DELETE:
+                    onDelete(groupOnDevice, handle);
+                    break;
+                default:
+                    log.warn("PRE Group operation {} not supported", groupOp.opType());
+            }
+        } finally {
+            PRE_GROUP_LOCKS.getUnchecked(handle).unlock();
+        }
+    }
+
+    private void onAdd(Bmv2PreGroup preGroup, Bmv2PreGroupHandle handle) {
+        try {
+            writeGroup(preGroup, handle);
+        } catch (Bmv2RuntimeException e) {
+            log.error("Unable to create the PRE group with groupId={}. deviceId={}", preGroup.groupId(), deviceId, e);
+        }
+    }
+
+    private void onDelete(Bmv2PreGroup preGroupOnDevice, Bmv2PreGroupHandle handle) {
+        if (preGroupOnDevice == null) {
+            log.warn("Unable to delete the group. Nonexistent in the group mirror! deviceId={}", deviceId);
+            return;
+        }
+        try {
+            deleteGroup(preGroupOnDevice, handle);
+        } catch (Bmv2RuntimeException e) {
+            log.error("Unable to delete the group. deviceId={}", deviceId, e);
+        }
+    }
+
+    private void onModify(Bmv2PreGroup preGroup, Bmv2PreGroup preGroupOnDevice, Bmv2PreGroupHandle handle) {
+        if (preGroupOnDevice == null) {
+            log.warn("Unable to modify the group. Nonexistent in the group mirror! deviceId={}", deviceId);
+            return;
+        }
+        if (preGroup.equals(preGroupOnDevice)) {
+            return;
+        }
+        try {
+            deleteGroup(preGroupOnDevice, handle);
+            writeGroup(preGroup, handle);
+        } catch (Bmv2RuntimeException e) {
+            log.error("Unable to modify the group. deviceId={}, groupId={}", deviceId, preGroup.groupId(), e);
+        }
+    }
+
+    private void writeGroup(Bmv2PreGroup preGroup, Bmv2PreGroupHandle handle) throws Bmv2RuntimeException {
+        Bmv2DeviceAgent bmv2DeviceAgent = getBmv2DeviceAgent();
+        Bmv2PreGroup bmv2PreGroupCreated = bmv2DeviceAgent.writePreGroup(preGroup);
+        //put the created group into the mirror store
+        preGroupMirror.put(handle, bmv2PreGroupCreated);
+    }
+
+    private void deleteGroup(Bmv2PreGroup preGroupOnDevice, Bmv2PreGroupHandle handle) throws Bmv2RuntimeException {
+        Bmv2DeviceAgent bmv2DeviceAgent = getBmv2DeviceAgent();
+        bmv2DeviceAgent.deletePreGroup(preGroupOnDevice);
+        //remove the group from the mirror
+        preGroupMirror.remove(handle);
+    }
+
+}
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/Bmv2DeviceAgent.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/Bmv2DeviceAgent.java
new file mode 100644
index 0000000..ac83038
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/Bmv2DeviceAgent.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api;
+
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreGroup;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2RuntimeException;
+import org.onosproject.net.DeviceId;
+
+import java.util.List;
+
+/**
+ * An agent to control a BMv2 device.
+ */
+public interface Bmv2DeviceAgent {
+
+    /**
+     * Returns the device ID of this agent.
+     *
+     * @return a device id
+     */
+    DeviceId deviceId();
+
+    /**
+     * Creates a multicast group and related entries atomically on a BMv2 device.
+     * If successful returns modified version of the group object that is filled with BMv2 switch-specific identifiers
+     * of the created group and nodes; throws Bmv2RuntimeException otherwise.
+     *
+     * @param preGroup Bmv2PreGroup
+     * @return modified version of preGroup param.
+     * @throws Bmv2RuntimeException if any error occurs
+     */
+    Bmv2PreGroup writePreGroup(Bmv2PreGroup preGroup) throws Bmv2RuntimeException;
+
+    /**
+     * Deletes a multicast group and all associated nodes from a BMV2 device.
+     *
+     * @param preGroup Bmv2PreGroup
+     * @throws Bmv2RuntimeException if any error occurs
+     */
+    void deletePreGroup(Bmv2PreGroup preGroup) throws Bmv2RuntimeException;
+
+    /**
+     * Returns all BMv2 PRE groups.
+     *
+     * @return list of PRE groups.
+     * @throws Bmv2RuntimeException if any error occurs
+     */
+    List<Bmv2PreGroup> getPreGroups() throws Bmv2RuntimeException;
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/Bmv2PreController.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/Bmv2PreController.java
new file mode 100644
index 0000000..b9d3910
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/Bmv2PreController.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api;
+
+import org.onosproject.net.DeviceId;
+
+/**
+ * PRE Controller for BMv2 devices.
+ */
+public interface Bmv2PreController {
+
+    /**
+     * Creates a client to communicate with the PRE layer of a BMv2 device
+     * and associates it with the given device identifier.
+     * Returns true if the client was created and the channel to the device is open.
+     * If so, a {@link Bmv2DeviceAgent} can be later obtained by invoking {@link #getPreClient(DeviceId)}.
+     * Otherwise, returns false.
+     *
+     * @param deviceId         device identifier
+     * @param thriftServerIp   Thrift server address
+     * @param thriftServerPort Thrift server port
+     * @return true if the client was created and the channel to the device is open; false otherwise
+     * @throws IllegalStateException if a client already exists for the given device identifier
+     */
+    boolean createPreClient(DeviceId deviceId, String thriftServerIp, Integer thriftServerPort);
+
+    /**
+     * Returns the PRE client associated with the given device identifier, if any.
+     *
+     * @param deviceId device identifier
+     * @return client instance if a client has already been created; null otherwise
+     */
+    Bmv2DeviceAgent getPreClient(DeviceId deviceId);
+
+    /**
+     * Removes the PRE client associated with the given device identifier.
+     *
+     * @param deviceId device identifier
+     */
+    void removePreClient(DeviceId deviceId);
+
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/package-info.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/package-info.java
new file mode 100644
index 0000000..d96ec97
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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 API.
+ */
+package org.onosproject.drivers.bmv2.api;
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2Entity.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2Entity.java
new file mode 100644
index 0000000..8412763
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2Entity.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api.runtime;
+
+
+/**
+ * Abstraction of an entity of BMv2 that can be read or write
+ * at runtime.
+ */
+public interface Bmv2Entity {
+
+    /**
+     * Returns the type of this entity.
+     *
+     * @return entity type
+     */
+    Bmv2EntityType entityType();
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2EntityType.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2EntityType.java
new file mode 100644
index 0000000..e219024
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2EntityType.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api.runtime;
+
+/**
+ * Type of runtime entity of BMv2.
+ */
+public enum Bmv2EntityType {
+
+    /**
+     * Indicates a multicast group.
+     */
+    PRE_GROUP
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2Handle.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2Handle.java
new file mode 100644
index 0000000..373b41b
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2Handle.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api.runtime;
+
+import org.onosproject.net.DeviceId;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Global identifier of an entity applied to a BMv2 device.
+ * @param <E> an object extending Bmv2Entity
+ */
+public abstract class Bmv2Handle<E extends Bmv2Entity> {
+
+    private final DeviceId deviceId;
+    private final E bmv2Entity;
+
+    protected Bmv2Handle(DeviceId deviceId, E bmv2Entity) {
+        this.deviceId = checkNotNull(deviceId);
+        this.bmv2Entity = checkNotNull(bmv2Entity);
+    }
+
+    /**
+     * Returns the device ID of this handle.
+     *
+     * @return device ID
+     */
+    public final DeviceId deviceId() {
+        return deviceId;
+    }
+
+    /**
+     * Returns the type of entity identified by this handle.
+     *
+     * @return BMv2 entity type
+     */
+    public final Bmv2EntityType entityType() {
+        return bmv2Entity.entityType();
+    }
+
+    /**
+     * The entity to which this handle is associated.
+     *
+     * @return Bmv2 entity
+     */
+    public final E bmv2Entity() {
+        return bmv2Entity;
+    }
+
+    @Override
+    public abstract int hashCode();
+
+    @Override
+    public abstract boolean equals(Object obj);
+
+    @Override
+    public abstract String toString();
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreGroup.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreGroup.java
new file mode 100644
index 0000000..c0c4b5d
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreGroup.java
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api.runtime;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Representation of a multicast group in BMv2 PRE.
+ */
+public class Bmv2PreGroup implements Bmv2Entity {
+
+    private final Integer groupId;
+    private final Bmv2PreNodes nodes;
+    //internal device-level identifier used by BMv2
+    private Integer nativeGroupHandle;
+
+    public Bmv2PreGroup(Integer groupId, Bmv2PreNodes nodes) {
+        this.groupId = checkNotNull(groupId, "groupId argument can not be null");
+        this.nodes = checkNotNull(nodes, "nodes argument can not be null");
+    }
+
+    /**
+     * Returns a new builder of BMv2 PRE groups.
+     *
+     * @return a BMv2 PRE group builder
+     */
+    public static Bmv2PreGroupBuilder builder() {
+        return new Bmv2PreGroupBuilder();
+    }
+
+    public Integer groupId() {
+        return groupId;
+    }
+
+    public Integer nativeGroupHandle() {
+        return nativeGroupHandle;
+    }
+
+    public Bmv2PreNodes nodes() {
+        return nodes;
+    }
+
+    public void setNativeGroupHandle(Integer nativeGroupHandle) {
+        this.nativeGroupHandle = nativeGroupHandle;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(groupId, nodes, nativeGroupHandle);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final Bmv2PreGroup other = (Bmv2PreGroup) obj;
+        return Objects.equal(this.groupId, other.groupId)
+                && Objects.equal(this.nodes, other.nodes);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("groupId", groupId)
+                .add("nativeGroupHandle", nativeGroupHandle)
+                .add("nodes", nodes)
+                .toString();
+    }
+
+    @Override
+    public Bmv2EntityType entityType() {
+        return Bmv2EntityType.PRE_GROUP;
+    }
+
+    /**
+     * Builder of BMv2 PRE groups.
+     */
+    public static final class Bmv2PreGroupBuilder {
+        private Integer groupId;
+        private Set<Bmv2PreNode> nodes = Sets.newHashSet();
+
+        private Bmv2PreGroupBuilder() {
+            //hidden constructor
+        }
+
+        /**
+         * Sets the identifier of this group.
+         *
+         * @param groupId identifier of this BMv2 PRE group.
+         * @return this
+         */
+        public Bmv2PreGroupBuilder withGroupId(Integer groupId) {
+            this.groupId = groupId;
+            return this;
+        }
+
+        /**
+         * Adds a node to this group.
+         *
+         * @param node a BMv2 PRE node.
+         * @return this
+         */
+        public Bmv2PreGroupBuilder addNode(Bmv2PreNode node) {
+            nodes.add(node);
+            return this;
+        }
+
+        /**
+         * Creates a new BMv2 PRE group.
+         *
+         * @return a new BMv2 PRE group
+         */
+        public Bmv2PreGroup build() {
+            return new Bmv2PreGroup(groupId, new Bmv2PreNodes(ImmutableSet.copyOf(nodes)));
+        }
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreGroupHandle.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreGroupHandle.java
new file mode 100644
index 0000000..7e886bc
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreGroupHandle.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api.runtime;
+
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import org.onosproject.net.DeviceId;
+
+/**
+ * Global identifier of a BMv2 PRE group applied to a device, uniquely defined
+ * by a device ID and group ID.
+ */
+public final class Bmv2PreGroupHandle extends Bmv2Handle<Bmv2PreGroup> {
+
+    private Bmv2PreGroupHandle(DeviceId deviceId, Bmv2PreGroup group) {
+        super(deviceId, group);
+    }
+
+    /**
+     * Creates a new handle for the given device ID and BMv2 PRE group.
+     *
+     * @param deviceId device ID
+     * @param group    BMv2 PRE group
+     * @return BMv2 PRE group handle
+     */
+    public static Bmv2PreGroupHandle of(DeviceId deviceId,
+                                        Bmv2PreGroup group) {
+        return new Bmv2PreGroupHandle(deviceId, group);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(deviceId(),
+                                bmv2Entity().groupId());
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        Bmv2PreGroupHandle that = (Bmv2PreGroupHandle) o;
+        return Objects.equal(deviceId(), that.deviceId()) &&
+                Objects.equal(bmv2Entity().groupId(), that.bmv2Entity().groupId());
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("deviceId", deviceId())
+                .add("groupId", bmv2Entity().groupId())
+                .toString();
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreJsonGroups.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreJsonGroups.java
new file mode 100644
index 0000000..83a6fa2
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreJsonGroups.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api.runtime;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+/**
+ * Represents mc group list retrieved from BMv2 PRE.
+ */
+public final class Bmv2PreJsonGroups {
+    public final L1Handle[] l1handles;
+    public final L2Handle[] l2handles;
+    public final Lag[] lags;
+    public final Mgrp[] mgrps;
+
+    @JsonCreator
+    public Bmv2PreJsonGroups(@JsonProperty("l1_handles") L1Handle[] l1handles,
+                             @JsonProperty("l2_handles") L2Handle[] l2handles,
+                             @JsonProperty("lags") Lag[] lags,
+                             @JsonProperty("mgrps") Mgrp[] mgrps) {
+        this.l1handles = l1handles;
+        this.l2handles = l2handles;
+        this.lags = lags;
+        this.mgrps = mgrps;
+    }
+
+    public static final class L1Handle {
+        public final int handle;
+        public final int l2handle;
+        public final int rid;
+
+        @JsonCreator
+        public L1Handle(@JsonProperty("handle") int handle,
+                        @JsonProperty("l2_handle") int l2handle,
+                        @JsonProperty("rid") int rid) {
+            this.handle = handle;
+            this.l2handle = l2handle;
+            this.rid = rid;
+        }
+    }
+
+    public static final class L2Handle {
+        public final int handle;
+        public final int[] lags;
+        public final int[] ports;
+
+        @JsonCreator
+        public L2Handle(@JsonProperty("handle") int handle,
+                        @JsonProperty("lags") int[] lags,
+                        @JsonProperty("ports") int[] ports) {
+            this.handle = handle;
+            this.lags = lags;
+            this.ports = ports;
+        }
+    }
+
+    public static final class Lag {
+        //lag is not used for now
+        @JsonCreator
+        public Lag() {
+        }
+    }
+
+    public static final class Mgrp {
+        public final int id;
+        public final int[] l1handles;
+
+        @JsonCreator
+        public Mgrp(@JsonProperty("id") int id, @JsonProperty("l1_handles") int[] l1handles) {
+            this.id = id;
+            this.l1handles = l1handles;
+        }
+    }
+}
+
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreNode.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreNode.java
new file mode 100644
index 0000000..3d50e50
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreNode.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api.runtime;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * The class that represents a multicast node in BMv2 PRE.
+ */
+public class Bmv2PreNode {
+    //replication id
+    private final Integer rid;
+    private final String portMap;
+    private Integer l1Handle;
+
+    public Bmv2PreNode(Integer rid, String portMap) {
+        this.rid = checkNotNull(rid, "rid argument can not be null");
+        this.portMap = checkNotNull(portMap, "portMap argument can not be null");
+    }
+
+    public static Bmv2PreNodeBuilder builder() {
+        return new Bmv2PreNodeBuilder();
+    }
+
+    public Integer rid() {
+        return rid;
+    }
+
+    public String portMap() {
+        return portMap;
+    }
+
+    public Integer l1Handle() {
+        return l1Handle;
+    }
+
+    public void setL1Handle(Integer l1Handle) {
+        this.l1Handle = l1Handle;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(rid, portMap, l1Handle);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final Bmv2PreNode other = (Bmv2PreNode) obj;
+        return Objects.equal(this.rid, other.rid)
+                && Objects.equal(this.portMap, other.portMap);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("rid", rid)
+                .add("portMap", portMap)
+                .add("l1Handle", l1Handle)
+                .toString();
+    }
+
+    public static final class Bmv2PreNodeBuilder {
+        //replication id
+        private Integer rid;
+        private String portMap;
+        private Integer l1Handle;
+
+        private Bmv2PreNodeBuilder() {
+        }
+
+        public Bmv2PreNodeBuilder withRid(Integer rid) {
+            this.rid = rid;
+            return this;
+        }
+
+        public Bmv2PreNodeBuilder withPortMap(String portMap) {
+            this.portMap = portMap;
+            return this;
+        }
+
+        public Bmv2PreNodeBuilder withL1Handle(Integer l1Handle) {
+            this.l1Handle = l1Handle;
+            return this;
+        }
+
+        public Bmv2PreNode build() {
+            Bmv2PreNode bmv2PreNode = new Bmv2PreNode(rid, portMap);
+            bmv2PreNode.setL1Handle(l1Handle);
+            return bmv2PreNode;
+        }
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreNodes.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreNodes.java
new file mode 100644
index 0000000..31edef1
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2PreNodes.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api.runtime;
+
+
+import com.google.common.base.Objects;
+import com.google.common.collect.ImmutableSet;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Immutable collection of BMv2 PRE group nodes.
+ */
+public final class Bmv2PreNodes {
+    private final ImmutableSet<Bmv2PreNode> nodes;
+
+    /**
+     * Creates a immutable list of group nodes.
+     *
+     * @param nodes list of group nodes
+     */
+    public Bmv2PreNodes(ImmutableSet<Bmv2PreNode> nodes) {
+        this.nodes = checkNotNull(nodes);
+    }
+
+    public ImmutableSet<Bmv2PreNode> nodes() {
+        return nodes;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(nodes);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj instanceof Bmv2PreNodes) {
+            return (this.nodes.containsAll(((Bmv2PreNodes) obj).nodes) &&
+                    ((Bmv2PreNodes) obj).nodes.containsAll(this.nodes));
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(this)
+                .add("nodes", nodes)
+                .toString();
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2RuntimeException.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2RuntimeException.java
new file mode 100644
index 0000000..fd61bcf
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/Bmv2RuntimeException.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.api.runtime;
+
+/**
+ * General exception of the BMv2 runtime APIs.
+ */
+public final class Bmv2RuntimeException extends Exception {
+
+    private final Code code;
+    private String codeString;
+
+    public Bmv2RuntimeException(String message) {
+        super(message);
+        this.code = Code.OTHER;
+        this.codeString = message;
+    }
+
+    public Bmv2RuntimeException(Throwable cause) {
+        super(cause);
+        this.code = Code.OTHER;
+        this.codeString = cause.toString();
+    }
+
+    public Bmv2RuntimeException(Code code) {
+        super(code.name());
+        this.code = code;
+    }
+
+    public Code getCode() {
+        return this.code;
+    }
+
+    public String explain() {
+        return (codeString == null) ? code.name() : code.name() + " " + codeString;
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + " " + explain();
+    }
+
+    /**
+     * Code of BMv2 error.
+     */
+    public enum Code {
+        /**
+         * Indicates table is full.
+         */
+        TABLE_FULL,
+        INVALID_MGID,
+        /**
+         * Indicates multicast group handle is invalid.
+         */
+        INVALID_MGRP_HANDLE,
+        /**
+         * Indicates l1 handle is not associated with a node.
+         */
+        INVALID_L1_HANDLE,
+        /**
+         * Indicates a general error.
+         */
+        MC_GENERAL_ERROR,
+        /**
+         * Indicates an unknown error.
+         */
+        MC_UNKNOWN_ERROR,
+        OTHER
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/package-info.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/package-info.java
new file mode 100644
index 0000000..ef19b3a
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/api/runtime/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.
+ *
+ */
+/**
+ * Classes abstracting entities of BMv2.
+ */
+package org.onosproject.drivers.bmv2.api.runtime;
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/Bmv2DeviceThriftClient.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/Bmv2DeviceThriftClient.java
new file mode 100644
index 0000000..cdcfb0c
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/Bmv2DeviceThriftClient.java
@@ -0,0 +1,406 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.ctl;
+
+import org.apache.thrift.TException;
+import org.onosproject.bmv2.thriftapi.SimplePreLAG;
+import org.onosproject.drivers.bmv2.api.Bmv2DeviceAgent;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreGroup;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreNode;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2RuntimeException;
+import org.onosproject.drivers.bmv2.impl.Bmv2PreGroupTranslatorImpl;
+import org.onosproject.net.DeviceId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.drivers.bmv2.ctl.Bmv2TExceptionParser.parseTException;
+
+/**
+ * Implementation of a Thrift client to control a BMv2 device.
+ */
+final class Bmv2DeviceThriftClient implements Bmv2DeviceAgent {
+
+    // FIXME: make context_id arbitrary for each call
+    // See: https://github.com/p4lang/behavioral-model/blob/master/modules/bm_sim/include/bm_sim/context.h
+    private static final int CONTEXT_ID = 0;
+    private static final String DEFAULT_LAG_MAP = "";
+    private final Logger log = LoggerFactory.getLogger(this.getClass());
+    private final SimplePreLAG.Iface simplePreLagClient;
+    private final DeviceId deviceId;
+
+    // ban constructor
+    protected Bmv2DeviceThriftClient(DeviceId deviceId, SimplePreLAG.Iface simplePreLagClient) {
+        this.deviceId = deviceId;
+        this.simplePreLagClient = simplePreLagClient;
+    }
+
+    @Override
+    public DeviceId deviceId() {
+        return deviceId;
+    }
+
+    @Override
+    public Bmv2PreGroup writePreGroup(Bmv2PreGroup preGroup) throws Bmv2RuntimeException {
+        log.debug("Creating a multicast group... > deviceId={}, {}", deviceId, preGroup);
+
+        GroupRollbackMachine groupRollbackMachine = new GroupRollbackMachine(preGroup);
+        try {
+            //first create mc group
+            preGroup.setNativeGroupHandle(createMcGroup(preGroup.groupId()));
+            groupRollbackMachine.setState(GroupOperationState.GROUP_CREATED);
+            //create mc nodes
+            createMcNodesOfGroup(preGroup);
+            groupRollbackMachine.setState(GroupOperationState.NODES_CREATED);
+            //associate nodes with group
+            associateMcNodesOfGroup(preGroup);
+            groupRollbackMachine.setState(GroupOperationState.NODES_ASSOCIATED);
+
+            log.debug("Multicast group created successfully. deviceId={}, {}", deviceId, preGroup);
+
+            return preGroup;
+        } finally {
+            groupRollbackMachine.rollbackIfNecessary();
+        }
+    }
+
+    @Override
+    public void deletePreGroup(Bmv2PreGroup preGroup) throws Bmv2RuntimeException {
+        log.debug("Deleting a multicast group... > deviceId={}, {}", deviceId, preGroup);
+        //disassociate mc nodes from group
+        disassociateMcNodesOfGroup(preGroup);
+        //delete mc nodes
+        deleteMcNodesOfGroup(preGroup);
+        //delete group
+        deleteMcGroup(preGroup);
+
+        log.debug("Multicast group deleted. deviceId={}, {}", deviceId, preGroup);
+    }
+
+    @Override
+    public List<Bmv2PreGroup> getPreGroups() throws Bmv2RuntimeException {
+        try {
+            String entries = simplePreLagClient.bm_mc_get_entries(CONTEXT_ID);
+            return Bmv2PreGroupTranslatorImpl.translate(entries);
+
+        } catch (TException | IOException e) {
+            log.debug("Exception while getting multicast groups. deviceId={}", deviceId, e);
+
+            if (e instanceof TException) {
+                throw parseTException((TException) e);
+            } else {
+                throw new Bmv2RuntimeException(e);
+            }
+        }
+    }
+
+    /**
+     * Creates multicast nodes one by one.
+     * Node handles obtained as the results of node creation operations are stored
+     * in given Bmv2PreGroup object.
+     *
+     * @param preGroup Bmv2PreGroup object
+     * @throws Bmv2RuntimeException
+     */
+    private void createMcNodesOfGroup(Bmv2PreGroup preGroup) throws Bmv2RuntimeException {
+        for (Bmv2PreNode node : preGroup.nodes().nodes()) {
+            node.setL1Handle(createMcNode(node));
+        }
+    }
+
+    /**
+     * Associates multicast nodes with a group one by one.
+     *
+     * @param preGroup Bmv2PreGroup object
+     * @throws Bmv2RuntimeException
+     */
+    private void associateMcNodesOfGroup(Bmv2PreGroup preGroup) throws Bmv2RuntimeException {
+        int nativeGroupHandle = preGroup.nativeGroupHandle();
+        for (Bmv2PreNode node : preGroup.nodes().nodes()) {
+            associateMcNode(nativeGroupHandle, node);
+        }
+    }
+
+    /**
+     * Deletes multicast nodes one by one.
+     *
+     * @param preGroup Bmv2PreGroup object
+     * @throws Bmv2RuntimeException
+     */
+    private void deleteMcNodesOfGroup(Bmv2PreGroup preGroup) throws Bmv2RuntimeException {
+        for (Bmv2PreNode node : preGroup.nodes().nodes()) {
+            destroyMcNode(node);
+        }
+    }
+
+    /**
+     * Disassociates multicast nodes from a group one by one.
+     *
+     * @param preGroup Bmv2PreGroup object
+     * @throws Bmv2RuntimeException
+     */
+    private void disassociateMcNodesOfGroup(Bmv2PreGroup preGroup) throws Bmv2RuntimeException {
+        int nativeGroupHandle = preGroup.nativeGroupHandle();
+        for (Bmv2PreNode node : preGroup.nodes().nodes()) {
+            disassociateMcNode(nativeGroupHandle, node);
+        }
+    }
+
+
+    /**
+     * Creates a multicast group with specified group Id.
+     *
+     * @param groupId identifier of a group
+     * @return group handle (BMv2 specific identifier associated with the group)
+     * @throws Bmv2RuntimeException
+     */
+    private int createMcGroup(int groupId) throws Bmv2RuntimeException {
+        log.debug("Creating the multicast group... > deviceId={}, groupId={}", deviceId, groupId);
+        try {
+            return simplePreLagClient.bm_mc_mgrp_create(CONTEXT_ID, groupId);
+        } catch (TException e) {
+            log.debug("Exception during creating multicast group. deviceId={}, groupId={}", deviceId, groupId);
+            throw parseTException(e);
+        }
+    }
+
+    /**
+     * Deletes a multicast group from a BMv2 device.
+     *
+     * @param preGroup
+     * @throws Bmv2RuntimeException
+     */
+    private void deleteMcGroup(Bmv2PreGroup preGroup) throws Bmv2RuntimeException {
+        log.debug("Destroying the multicast group... > deviceId={}, groupId={}, groupHandle={}",
+                  deviceId, preGroup.groupId(), preGroup.nativeGroupHandle());
+        try {
+            simplePreLagClient.bm_mc_mgrp_destroy(CONTEXT_ID, preGroup.nativeGroupHandle());
+        } catch (TException e) {
+            log.debug("Exception during destroying multicast group. deviceId={}, groupId={}, groupHandle={}",
+                      deviceId, preGroup.groupId(), preGroup.nativeGroupHandle());
+            throw parseTException(e);
+        }
+    }
+
+    /**
+     * Creates a multicast node on the BMv2 device.
+     *
+     * @param node Bmv2PreNode
+     * @return L1 handle
+     * @throws Bmv2RuntimeException
+     */
+    private int createMcNode(Bmv2PreNode node) throws Bmv2RuntimeException {
+        log.debug("Creating the multicast node... > deviceId={}, {}", deviceId, node);
+        try {
+            return simplePreLagClient.bm_mc_node_create(CONTEXT_ID, node.rid(), node.portMap(), DEFAULT_LAG_MAP);
+        } catch (TException e) {
+            log.debug("Exception during creating multicast node: {}", node);
+            throw parseTException(e);
+        }
+    }
+
+    /**
+     * Associates a multicast node with a group.
+     *
+     * @param groupHandle handle of the group that the node will be associated with
+     * @param node        Bmv2PreNode
+     * @throws Bmv2RuntimeException
+     */
+    private void associateMcNode(int groupHandle, Bmv2PreNode node) throws Bmv2RuntimeException {
+        log.debug("Associating the multicast node with the group... > deviceId={}, groupHandle:{}, node:{}",
+                  deviceId, groupHandle, node);
+        try {
+            simplePreLagClient.bm_mc_node_associate(CONTEXT_ID, groupHandle, node.l1Handle());
+        } catch (TException e) {
+            log.debug("Exception during associating multicast node with group. deviceId={} groupHandle:{}, node:{}",
+                      deviceId, groupHandle, node);
+            throw parseTException(e);
+        }
+    }
+
+    /**
+     * Disassociates a multicast node from a group.
+     *
+     * @param groupHandle handle of the group that the node will be disassociated from
+     * @param node        Bmv2PreNode
+     * @throws Bmv2RuntimeException
+     */
+    private void disassociateMcNode(int groupHandle, Bmv2PreNode node) throws Bmv2RuntimeException {
+        log.debug("Disassociating the multicast node from the group... > deviceId={}, groupHandle:{}, node:{}",
+                  deviceId, groupHandle, node);
+        try {
+            simplePreLagClient.bm_mc_node_dissociate(CONTEXT_ID, groupHandle, node.l1Handle());
+        } catch (TException e) {
+            log.debug("Failed to disassociate multicast node from group. deviceId={} groupHandle:{}, node:{}",
+                      deviceId, groupHandle, node);
+            throw parseTException(e);
+        }
+    }
+
+    /**
+     * Destroys the multicast node in a BMv2 device.
+     *
+     * @param node PRE node which is about to be destroyed
+     * @throws Bmv2RuntimeException
+     */
+    private void destroyMcNode(Bmv2PreNode node) throws Bmv2RuntimeException {
+        log.debug("Destroying the multicast node... > deviceId={}, node:{}", deviceId, node);
+        try {
+            simplePreLagClient.bm_mc_node_destroy(CONTEXT_ID, node.l1Handle());
+        } catch (TException e) {
+            log.debug("Exception during destroying multicast node. deviceId={}, node:{}", deviceId, node);
+            throw parseTException(e);
+        }
+    }
+
+
+    /**
+     * Defines identifiers of main group operation steps.
+     */
+    private enum GroupOperationState {
+        IDLE, // nothing has been done
+        GROUP_CREATED, //the last successful step is group creation
+        NODES_CREATED, //the last successful step is node creation
+        NODES_ASSOCIATED //the last successful step is node association.
+    }
+
+    /**
+     * Implementation of a simple state machine to keep track of complex (non-atomic) operations on groups and
+     * to execute essential rollback steps accordingly.
+     * For example, creating a multicast group is composed of multiple steps:
+     * 1- Group creation
+     * 2- Node creation
+     * 3- Node association
+     * Each step associates with a GroupOperationState to keep track of group creation operation.
+     * A rollback flow is executed with respect to the current state.
+     */
+    private class GroupRollbackMachine {
+        Bmv2PreGroup preGroup;
+        //indicates the last successful step
+        GroupOperationState state = GroupOperationState.IDLE;
+
+        private GroupRollbackMachine() {
+            //hidden constructor
+        }
+
+        public GroupRollbackMachine(Bmv2PreGroup preGroup) {
+            this.preGroup = preGroup;
+        }
+
+        GroupOperationState state() {
+            return state;
+        }
+
+        void setState(GroupOperationState state) {
+            this.state = checkNotNull(state);
+        }
+
+        /**
+         * Checks the state and executes necessary rollback flow if necessary.
+         */
+        void rollbackIfNecessary() {
+            switch (state) {
+                case GROUP_CREATED:
+                    //means node creation failed. Delete already created nodes and the group
+                    onGroupCreated();
+                    break;
+                case NODES_CREATED:
+                    //means node association failed.
+                    //Disassociate already associated nodes then delete nodes and the group.
+                    onNodesCreated();
+                    break;
+                default:
+                    //do nothing in IDLE and NODES_ASSOCIATED states. They do not signify a failure.
+                    break;
+            }
+        }
+
+        /**
+         * Executes necessary steps in case the last successful step is group creation.
+         * This means one of the node creation operations has been failed and all previous steps should rollback.
+         */
+        private void onGroupCreated() {
+            log.warn("One of the steps of mc group creation operation has been failed." +
+                             "Rolling back in state {}...> deviceId={}, groupId={}",
+                     state, deviceId, preGroup.groupId());
+            deleteNodes(preGroup);
+            deleteGroup(preGroup);
+        }
+
+        /**
+         * Executes necessary steps in case the last successful step is node creation.
+         * This means one of the node association operations has been failed and all previous steps should rollback.
+         */
+        private void onNodesCreated() {
+            log.warn("One of the steps of mc group creation operation has been failed." +
+                             "Rolling back in state {}...> deviceId={}, groupId={}",
+                     state, deviceId, preGroup.groupId());
+            disassociateNodes(preGroup);
+            deleteNodes(preGroup);
+            deleteGroup(preGroup);
+        }
+
+        /**
+         * Deletes a group in the scope of rollback operation.
+         */
+        private void deleteGroup(Bmv2PreGroup preGroup) {
+            try {
+                deleteMcGroup(preGroup);
+            } catch (Bmv2RuntimeException e) {
+                log.error("Unable to destroy multicast group in the scope of rollback operation." +
+                                  "deviceId={}, groupId={}", deviceId, preGroup.groupId());
+            }
+        }
+
+
+        /**
+         * Disassociates all nodes from their group in the scope of rollback operation.
+         */
+        private void disassociateNodes(Bmv2PreGroup preGroup) {
+            preGroup.nodes().nodes().forEach(node -> {
+                try {
+                    disassociateMcNode(preGroup.nativeGroupHandle(), node);
+                } catch (Bmv2RuntimeException e) {
+                    log.error("Unable to disassociate the node in the scope of rollback operation." +
+                                      "deviceId={}, groupHandle={}, l1Handle={}",
+                              deviceId, preGroup.nativeGroupHandle(), node.l1Handle(), e);
+                }
+            });
+        }
+
+        /**
+         * Deletes all nodes of a group in the scope of rollback operation.
+         */
+        private void deleteNodes(Bmv2PreGroup preGroup) {
+            //filter created nodes and destroy them
+            preGroup.nodes().nodes().stream()
+                    .filter(node -> node.l1Handle() != null)
+                    .forEach(node -> {
+                        try {
+                            destroyMcNode(node);
+                        } catch (Bmv2RuntimeException e) {
+                            log.error("Unable to destroy the node in the scope of rollback operation." +
+                                              "deviceId={}, l1Handle={}", deviceId, node.l1Handle(), e);
+                        }
+                    });
+        }
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/Bmv2PreControllerImpl.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/Bmv2PreControllerImpl.java
new file mode 100644
index 0000000..4c80e79
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/Bmv2PreControllerImpl.java
@@ -0,0 +1,237 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.ctl;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.Maps;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TMultiplexedProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.transport.TSocket;
+import org.apache.thrift.transport.TTransport;
+import org.onlab.util.Tools;
+import org.onosproject.bmv2.thriftapi.SimplePreLAG;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.drivers.bmv2.api.Bmv2DeviceAgent;
+import org.onosproject.drivers.bmv2.api.Bmv2PreController;
+import org.onosproject.net.DeviceId;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+
+import java.util.Dictionary;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.lang.String.format;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * BMv2 PRE controller implementation.
+ */
+@Component(immediate = true)
+@Service
+public class Bmv2PreControllerImpl implements Bmv2PreController {
+
+    private static final int DEVICE_LOCK_CACHE_EXPIRE_TIME_IN_MIN = 10;
+    private static final int DEVICE_LOCK_WAITING_TIME_IN_SEC = 60;
+    private static final int DEFAULT_NUM_CONNECTION_RETRIES = 2;
+    private static final int DEFAULT_TIME_BETWEEN_RETRIES = 10;
+    private static final String THRIFT_SERVICE_NAME = "simple_pre_lag";
+    private final Logger log = getLogger(getClass());
+    private final Map<DeviceId, Pair<TTransport, Bmv2DeviceThriftClient>> clients = Maps.newHashMap();
+    //TODO consider a timeout mechanism for locks to limit the retention time
+    private final LoadingCache<DeviceId, ReadWriteLock> deviceLocks = CacheBuilder.newBuilder()
+            .expireAfterAccess(DEVICE_LOCK_CACHE_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES)
+            .build(new CacheLoader<DeviceId, ReadWriteLock>() {
+                @Override
+                public ReadWriteLock load(DeviceId deviceId) {
+                    return new ReentrantReadWriteLock();
+                }
+            });
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ComponentConfigService cfgService;
+    @Property(name = "numConnectionRetries", intValue = DEFAULT_NUM_CONNECTION_RETRIES,
+            label = "Number of connection retries after a network error")
+    private int numConnectionRetries = DEFAULT_NUM_CONNECTION_RETRIES;
+    @Property(name = "timeBetweenRetries", intValue = DEFAULT_TIME_BETWEEN_RETRIES,
+            label = "Time between retries in milliseconds")
+    private int timeBetweenRetries = DEFAULT_TIME_BETWEEN_RETRIES;
+    @Property(name = "deviceLockWaitingTime", intValue = DEVICE_LOCK_WAITING_TIME_IN_SEC,
+            label = "Waiting time for a read/write lock in seconds")
+    private int deviceLockWaitingTime = DEVICE_LOCK_WAITING_TIME_IN_SEC;
+
+    @Activate
+    public void activate() {
+        cfgService.registerProperties(getClass());
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        cfgService.unregisterProperties(getClass(), false);
+        log.info("Stopped");
+    }
+
+    @Modified
+    protected void modified(ComponentContext context) {
+        Dictionary<?, ?> properties = context.getProperties();
+
+        Integer newNumConnRetries = Tools.getIntegerProperty(properties, "numConnectionRetries");
+        if (newNumConnRetries != null && newNumConnRetries >= 0) {
+            numConnectionRetries = newNumConnRetries;
+        } else {
+            log.warn("numConnectionRetries must be equal to or greater than 0");
+        }
+
+        Integer newTimeBtwRetries = Tools.getIntegerProperty(properties, "timeBetweenRetries");
+        if (newTimeBtwRetries != null && newTimeBtwRetries >= 0) {
+            timeBetweenRetries = newTimeBtwRetries;
+        } else {
+            log.warn("timeBetweenRetries must be equal to or greater than 0");
+        }
+
+        Integer newDeviceLockWaitingTime = Tools.getIntegerProperty(properties, "deviceLockWaitingTime");
+        if (newDeviceLockWaitingTime != null && newDeviceLockWaitingTime >= 0) {
+            deviceLockWaitingTime = newDeviceLockWaitingTime;
+        } else {
+            log.warn("deviceLockWaitingTime must be equal to or greater than 0");
+        }
+    }
+
+    @Override
+    public boolean createPreClient(DeviceId deviceId, String thriftServerIp, Integer thriftServerPort) {
+        checkNotNull(deviceId);
+        checkNotNull(thriftServerIp);
+        checkNotNull(thriftServerPort);
+
+        if (!acquireWriteLock(deviceId)) {
+            //reason is already logged during the lock acquisition
+            return false;
+        }
+
+        log.info("Creating PRE client for {} through Thrift server {}:{}", deviceId, thriftServerIp, thriftServerPort);
+
+        try {
+            if (clients.containsKey(deviceId)) {
+                throw new IllegalStateException(format("A client already exists for %s", deviceId));
+            } else {
+                return doCreateClient(deviceId, thriftServerIp, thriftServerPort);
+            }
+        } finally {
+            releaseWriteLock(deviceId);
+        }
+    }
+
+    @Override
+    public Bmv2DeviceAgent getPreClient(DeviceId deviceId) {
+        if (!acquireReadLock(deviceId)) {
+            return null;
+        }
+        try {
+            return clients.containsKey(deviceId) ? clients.get(deviceId).getRight() : null;
+        } finally {
+            releaseReadLock(deviceId);
+        }
+    }
+
+    @Override
+    public void removePreClient(DeviceId deviceId) {
+        if (!acquireWriteLock(deviceId)) {
+            //reason is already logged during the lock acquisition
+            return;
+        }
+
+        try {
+            if (clients.containsKey(deviceId)) {
+                TTransport transport = clients.get(deviceId).getLeft();
+                if (transport.isOpen()) {
+                    transport.close();
+                }
+                clients.remove(deviceId);
+            }
+        } finally {
+            releaseWriteLock(deviceId);
+        }
+    }
+
+    private boolean acquireWriteLock(DeviceId deviceId) {
+        try {
+            return deviceLocks.getUnchecked(deviceId).writeLock().tryLock(deviceLockWaitingTime, TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            log.error("Unable to acquire write lock for device {} due to {}", deviceId, e.toString());
+        }
+        return false;
+    }
+
+    private boolean acquireReadLock(DeviceId deviceId) {
+        try {
+            return deviceLocks.getUnchecked(deviceId).readLock().tryLock(deviceLockWaitingTime, TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            log.error("Unable to acquire read lock for device {} due to {}", deviceId, e.toString());
+        }
+        return false;
+    }
+
+    private void releaseWriteLock(DeviceId deviceId) {
+        deviceLocks.getUnchecked(deviceId).writeLock().unlock();
+    }
+
+    private void releaseReadLock(DeviceId deviceId) {
+        deviceLocks.getUnchecked(deviceId).readLock().unlock();
+    }
+
+    private boolean doCreateClient(DeviceId deviceId, String thriftServerIp, Integer thriftServerPort) {
+        SafeThriftClient.Options options = new SafeThriftClient.Options(numConnectionRetries, timeBetweenRetries);
+
+        try {
+            // Make the expensive call
+            TTransport transport = new TSocket(thriftServerIp, thriftServerPort);
+
+            TProtocol protocol = new TBinaryProtocol(transport);
+            // Create a client for simple_pre service.
+            SimplePreLAG.Client simplePreClient = new SimplePreLAG.Client(
+                    new TMultiplexedProtocol(protocol, THRIFT_SERVICE_NAME));
+
+            SimplePreLAG.Iface safeSimplePreClient = SafeThriftClient.wrap(simplePreClient,
+                                                                           SimplePreLAG.Iface.class,
+                                                                           options);
+
+            Bmv2DeviceThriftClient client = new Bmv2DeviceThriftClient(deviceId, safeSimplePreClient);
+            clients.put(deviceId, Pair.of(transport, client));
+            return true;
+
+        } catch (RuntimeException e) {
+            log.warn("Failed to create Thrift client for BMv2 device. deviceId={}, cause={}", deviceId, e);
+            return false;
+        }
+    }
+}
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/Bmv2TExceptionParser.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/Bmv2TExceptionParser.java
new file mode 100644
index 0000000..b765eca
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/Bmv2TExceptionParser.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.ctl;
+
+
+import org.apache.thrift.TException;
+import org.onosproject.bmv2.thriftapi.InvalidMcOperation;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2RuntimeException;
+
+import static org.onosproject.drivers.bmv2.api.runtime.Bmv2RuntimeException.Code;
+
+/**
+ * Utility class to translate a Thrift exception into a Bmv2RuntimeException.
+ */
+final class Bmv2TExceptionParser {
+
+    private Bmv2TExceptionParser() {
+        // ban constructor.
+    }
+
+    static Bmv2RuntimeException parseTException(TException cause) {
+        try {
+            return new Bmv2RuntimeException(getCode(cause));
+        } catch (ParserException e) {
+            return new Bmv2RuntimeException(e.codeString);
+        }
+    }
+
+    private static Code getCode(TException e) throws ParserException {
+        if (e instanceof InvalidMcOperation) {
+            switch (((InvalidMcOperation) e).getCode()) {
+                case TABLE_FULL:
+                    return Code.TABLE_FULL;
+                case INVALID_MGID:
+                    return Code.INVALID_MGID;
+                case INVALID_L1_HANDLE:
+                    return Code.INVALID_L1_HANDLE;
+                case INVALID_MGRP_HANDLE:
+                    return Code.INVALID_MGRP_HANDLE;
+                case ERROR:
+                    return Code.MC_GENERAL_ERROR;
+                default:
+                    return Code.MC_UNKNOWN_ERROR;
+            }
+        } else {
+            throw new ParserException(e.toString());
+        }
+    }
+
+    private static class ParserException extends Exception {
+
+        private String codeString;
+
+        public ParserException(String codeString) {
+            this.codeString = codeString;
+        }
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/SafeThriftClient.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/SafeThriftClient.java
new file mode 100644
index 0000000..a510954
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/SafeThriftClient.java
@@ -0,0 +1,251 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.
+ *
+ */
+
+/*
+ * Most of the code of this class was copied from:
+ * http://liveramp.com/engineering/reconnecting-thrift-client/
+ */
+
+package org.onosproject.drivers.bmv2.ctl;
+
+import com.google.common.collect.ImmutableSet;
+import org.apache.thrift.TServiceClient;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Set;
+
+/**
+ * Thrift client wrapper that attempts a few reconnects before giving up a method call execution. It also provides
+ * synchronization between calls over the same transport.
+ */
+final class SafeThriftClient {
+
+    private static final Logger LOG = LoggerFactory.getLogger(SafeThriftClient.class);
+
+    /**
+     * List of causes which suggest a restart might fix things (defined as constants in {@link TTransportException}).
+     */
+    private static final Set<Integer> RESTARTABLE_CAUSES = ImmutableSet.of(TTransportException.NOT_OPEN,
+                                                                           TTransportException.END_OF_FILE,
+                                                                           TTransportException.TIMED_OUT,
+                                                                           TTransportException.UNKNOWN);
+
+    private SafeThriftClient() {
+        // ban constructor.
+    }
+
+    /**
+     * Reflectively wraps an already existing Thrift client.
+     *
+     * @param baseClient      the client to wrap
+     * @param clientInterface the interface that the client implements
+     * @param options         options that control behavior of the reconnecting client
+     * @param <T>             a class extending TServiceClient
+     * @param <C>             a client interface
+     * @return a wrapped client interface
+     */
+    public static <T extends TServiceClient, C> C wrap(T baseClient, Class<C> clientInterface, Options options) {
+        Object proxyObject = Proxy.newProxyInstance(clientInterface.getClassLoader(),
+                                                    new Class<?>[]{clientInterface},
+                                                    new ReconnectingClientProxy<T>(baseClient,
+                                                                                   options.getNumRetries(),
+                                                                                   options.getTimeBetweenRetries()));
+
+        return (C) proxyObject;
+    }
+
+    /**
+     * Reflectively wraps an already existing Thrift client.
+     *
+     * @param baseClient the client to wrap
+     * @param options    options that control behavior of the reconnecting client
+     * @param <T>        a class that extends TServiceClient
+     * @param <C>        a client interface
+     * @return a wrapped client interface
+     */
+    public static <T extends TServiceClient, C> C wrap(T baseClient, Options options) {
+        Class<?>[] interfaces = baseClient.getClass().getInterfaces();
+
+        for (Class<?> iface : interfaces) {
+            if (iface.getSimpleName().equals("Iface")
+                    && iface.getEnclosingClass().equals(baseClient.getClass().getEnclosingClass())) {
+                return (C) wrap(baseClient, iface, options);
+            }
+        }
+
+        throw new RuntimeException("Class needs to implement Iface directly. Use wrap(TServiceClient, Class) instead.");
+    }
+
+    /**
+     * Reflectively wraps an already existing Thrift client.
+     *
+     * @param baseClient      the client to wrap
+     * @param clientInterface the interface that the client implements
+     * @param <T>             a class that extends TServiceClient
+     * @param <C>             a client interface
+     * @return a wrapped client interface
+     */
+    public static <T extends TServiceClient, C> C wrap(T baseClient, Class<C> clientInterface) {
+        return wrap(baseClient, clientInterface, Options.defaults());
+    }
+
+    /**
+     * Reflectively wraps an already existing Thrift client.
+     *
+     * @param baseClient the client to wrap
+     * @param <T>        a class that extends TServiceClient
+     * @param <C>        a client interface
+     * @return a wrapped client interface
+     */
+    public static <T extends TServiceClient, C> C wrap(T baseClient) {
+        return wrap(baseClient, Options.defaults());
+    }
+
+    /**
+     * Reconnection options for {@link SafeThriftClient}.
+     */
+    public static class Options {
+        private int numRetries;
+        private long timeBetweenRetries;
+
+        /**
+         * Creates new options with the given parameters.
+         *
+         * @param numRetries         the maximum number of times to try reconnecting before giving up and throwing an
+         *                           exception
+         * @param timeBetweenRetries the number of milliseconds to wait in between reconnection attempts.
+         */
+        public Options(int numRetries, long timeBetweenRetries) {
+            this.numRetries = numRetries;
+            this.timeBetweenRetries = timeBetweenRetries;
+        }
+
+        private static Options defaults() {
+            return new Options(5, 10000L);
+        }
+
+        private int getNumRetries() {
+            return numRetries;
+        }
+
+        private long getTimeBetweenRetries() {
+            return timeBetweenRetries;
+        }
+    }
+
+    /**
+     * Helper proxy class. Attempts to call method on proxy object wrapped in try/catch. If it fails, it attempts a
+     * reconnect and tries the method again.
+     *
+     * @param <T> a class that extends TServiceClient
+     */
+    private static class ReconnectingClientProxy<T extends TServiceClient> implements InvocationHandler {
+        private final T baseClient;
+        private final TTransport transport;
+        private final int maxRetries;
+        private final long timeBetweenRetries;
+
+        public ReconnectingClientProxy(T baseClient, int maxRetries, long timeBetweenRetries) {
+            this.baseClient = baseClient;
+            this.transport = baseClient.getInputProtocol().getTransport();
+            this.maxRetries = maxRetries;
+            this.timeBetweenRetries = timeBetweenRetries;
+        }
+
+        private void reconnectOrThrowException()
+                throws TTransportException {
+            int errors = 0;
+            try {
+                if (transport.isOpen()) {
+                    transport.close();
+                }
+            } catch (Exception e) {
+                // Thrift seems to have a bug where if the transport is already closed a SocketException is thrown.
+                // However, such an exception is not advertised by .close(), hence the general-purpose catch.
+                LOG.debug("Exception while closing transport", e);
+            }
+
+            while (errors < maxRetries) {
+                try {
+                    LOG.debug("Attempting to reconnect...");
+                    transport.open();
+                    LOG.debug("Reconnection successful");
+                    break;
+                } catch (TTransportException e) {
+                    LOG.debug("Error while reconnecting:", e);
+                    errors++;
+
+                    if (errors < maxRetries) {
+                        try {
+                            LOG.debug("Sleeping for {} milliseconds before retrying", timeBetweenRetries);
+                            Thread.sleep(timeBetweenRetries);
+                        } catch (InterruptedException e2) {
+                            Thread.currentThread().interrupt();
+                            throw new RuntimeException(e);
+                        }
+                    }
+                }
+            }
+
+            if (errors >= maxRetries) {
+                throw new TTransportException("Failed to reconnect");
+            }
+        }
+
+        @Override
+        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+
+            // Thrift transport layer is not thread-safe (it's a wrapper on a socket), hence we need locking.
+            synchronized (transport) {
+
+                LOG.debug("Invoking method... > fromThread={}, method={}, args={}",
+                          Thread.currentThread().getId(), method.getName(), args);
+
+                try {
+
+                    return method.invoke(baseClient, args);
+                } catch (InvocationTargetException e) {
+                    if (e.getTargetException() instanceof TTransportException) {
+                        TTransportException cause = (TTransportException) e.getTargetException();
+
+                        if (RESTARTABLE_CAUSES.contains(cause.getType())) {
+                            // Try to reconnect. If fail, a TTransportException will be thrown.
+                            reconnectOrThrowException();
+                            try {
+                                // If here, transport has been successfully open, hence new exceptions will be thrown.
+                                return method.invoke(baseClient, args);
+                            } catch (InvocationTargetException e1) {
+                                LOG.debug("Exception: {}", e1.getTargetException());
+                                throw e1.getTargetException();
+                            }
+                        }
+                    }
+                    // Target exception is neither a TTransportException nor a restartable cause.
+                    LOG.debug("Exception: {}", e.getTargetException());
+                    throw e.getTargetException();
+                }
+            }
+        }
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/package-info.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/package-info.java
new file mode 100644
index 0000000..f273b19
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/ctl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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 controller.
+ */
+package org.onosproject.drivers.bmv2.ctl;
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/impl/Bmv2PreGroupTranslatorImpl.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/impl/Bmv2PreGroupTranslatorImpl.java
new file mode 100644
index 0000000..6652ee0
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/impl/Bmv2PreGroupTranslatorImpl.java
@@ -0,0 +1,270 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.impl;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.Sets;
+import org.onosproject.core.GroupId;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreGroup;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreJsonGroups;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreNode;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.instructions.Instructions;
+import org.onosproject.net.group.Group;
+import org.onosproject.net.group.GroupDescription;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+/**
+ * Implementation of BMv2 PRE group translation logic.
+ */
+public final class Bmv2PreGroupTranslatorImpl {
+
+    private static final int BMV2_PORT_MAP_SIZE = 256;
+
+    //hidden constructor
+    private Bmv2PreGroupTranslatorImpl() {
+    }
+
+    /**
+     * Returns a BMv2 PRE group equivalent to given group.
+     *
+     * @param group group
+     * @return a BMv2 PRE group
+     */
+    public static Bmv2PreGroup translate(Group group) {
+        if (!group.type().equals(GroupDescription.Type.ALL)) {
+            throw new RuntimeException("Unable to translate the group to BMv2 PRE group." +
+                                               "A BMv2 PRE group is to be of ALL type. GroupId="
+                                               + group.id());
+        }
+
+        Bmv2PreGroup.Bmv2PreGroupBuilder bmv2PreGroupBuilder = Bmv2PreGroup.builder();
+        bmv2PreGroupBuilder.withGroupId(group.id().id());
+        // RID is generated per an MG since L2 broadcast can be considered as a MG that have a single RID.
+        // for simplicity RID will be assigned to zero for any node in an MG.
+        int replicationId = 0;
+
+        Set<PortNumber> outPorts = Sets.newHashSet();
+        group.buckets().buckets().forEach(groupBucket -> {
+            //get output instructions of the bucket
+            Set<Instructions.OutputInstruction> outputInstructions = getOutputInstructions(groupBucket.treatment());
+            //check validity of output instructions
+            checkOutputInstructions(group.id(), outputInstructions);
+            outPorts.add(outputInstructions.iterator().next().port());
+        });
+        validatePorts(outPorts);
+        bmv2PreGroupBuilder.addNode(Bmv2PreNode.builder()
+                                            .withRid(replicationId)
+                                            .withPortMap(buildPortMap(outPorts))
+                                            .build());
+        return bmv2PreGroupBuilder.build();
+    }
+
+    /**
+     * Converts a PRE group list in JSON notation to list of Bmv2PreGroups.
+     *
+     * @param groupListJson group list string ing JSON notation
+     * @return list of Bmv2PreGroups
+     * @throws IOException in case JSON string can not be parsed
+     */
+    public static List<Bmv2PreGroup> translate(String groupListJson) throws IOException {
+        List<Bmv2PreGroup> preGroups = new ArrayList<>();
+        if (groupListJson == null) {
+            return preGroups;
+        }
+        ObjectMapper mapper = new ObjectMapper();
+        Bmv2PreJsonGroups bmv2PreJsonGroups = mapper.readValue(groupListJson, Bmv2PreJsonGroups.class);
+
+        Bmv2PreGroup.Bmv2PreGroupBuilder bmv2PreGroupBuilder;
+        for (Bmv2PreJsonGroups.Mgrp mgrp : bmv2PreJsonGroups.mgrps) {
+
+            bmv2PreGroupBuilder = Bmv2PreGroup.builder();
+            bmv2PreGroupBuilder.withGroupId(mgrp.id);
+
+            for (int l1handleId : mgrp.l1handles) {
+                Bmv2PreJsonGroups.L1Handle l1handle = getL1Handle(l1handleId, bmv2PreJsonGroups.l1handles);
+                if (l1handle == null) {
+                    continue;
+                }
+                Bmv2PreJsonGroups.L2Handle l2handle = getL2Handle(l1handle.l2handle, bmv2PreJsonGroups.l2handles);
+                if (l2handle == null) {
+                    continue;
+                }
+                bmv2PreGroupBuilder.addNode(Bmv2PreNode.builder()
+                                                    .withRid(l1handle.rid)
+                                                    .withPortMap(buildPortMap(l2handle.ports))
+                                                    .withL1Handle(l1handleId)
+                                                    .build());
+            }
+            preGroups.add(bmv2PreGroupBuilder.build());
+        }
+        return preGroups;
+    }
+
+    /**
+     * Retrieves L1Handle object pointed by given L1 handle pointer from L1 handles list.
+     *
+     * @param l1handlePointer pointer to a L1 handle
+     * @param l1handles       list of L1 handles
+     * @return L1 handle object if exists; null otherwise
+     */
+    private static Bmv2PreJsonGroups.L1Handle getL1Handle(int l1handlePointer,
+                                                          Bmv2PreJsonGroups.L1Handle[] l1handles) {
+        for (Bmv2PreJsonGroups.L1Handle l1Handle : l1handles) {
+            if (l1handlePointer == l1Handle.handle) {
+                return l1Handle;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Retrieves L2Handle object pointed by given L2 handle pointer from L2 handles list.
+     *
+     * @param l2handlePointer pointer to a L2 handle
+     * @param l2handles       list of L2 handles
+     * @return L2 handle object if exists; null otherwise
+     */
+    private static Bmv2PreJsonGroups.L2Handle getL2Handle(int l2handlePointer,
+                                                          Bmv2PreJsonGroups.L2Handle[] l2handles) {
+        for (Bmv2PreJsonGroups.L2Handle l2handle : l2handles) {
+            if (l2handlePointer == l2handle.handle) {
+                return l2handle;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Builds a port map string composing of 1 and 0s.
+     * BMv2 API reads a port map from most significant to least significant char.
+     * Index of 1s indicates port numbers.
+     * For example, if port numbers are 4,3 and 1, the generated port map string looks like 11010.
+     *
+     * @param outPorts set of output port numbers
+     * @return port map string
+     */
+    private static String buildPortMap(Set<PortNumber> outPorts) {
+        //Sorts port numbers in descending order
+        SortedSet<PortNumber> outPortsSorted = new TreeSet<>((o1, o2) -> (int) (o2.toLong() - o1.toLong()));
+        outPortsSorted.addAll(outPorts);
+        PortNumber biggestPort = outPortsSorted.iterator().next();
+        int portMapSize = (int) biggestPort.toLong() + 1;
+        //init and fill port map with zero characters
+        char[] portMap = new char[portMapSize];
+        Arrays.fill(portMap, '0');
+        //fill in the ports with 1
+        outPortsSorted.forEach(portNumber -> portMap[portMapSize - (int) portNumber.toLong() - 1] = '1');
+        return String.valueOf(portMap);
+    }
+
+    /**
+     * Builds a port map string composing of 1 and 0s.
+     * BMv2 API reads a port map from most significant to least significant char.
+     * The index of 1s indicates port numbers.
+     * For example, if port numbers are 4,3 and 1, the generated port map string looks like 11010.
+     *
+     * @param portArr array of output port numbers
+     * @return port map string
+     */
+    private static String buildPortMap(int[] portArr) {
+        Set<PortNumber> outPorts = new HashSet<>();
+        for (int port : portArr) {
+            outPorts.add(PortNumber.portNumber(port));
+        }
+        return buildPortMap(outPorts);
+    }
+
+    /**
+     * Retrieves output instructions out of the instruction set of the given traffic treatment.
+     *
+     * @param trafficTreatment
+     * @return set of output instructions
+     */
+    private static Set<Instructions.OutputInstruction> getOutputInstructions(TrafficTreatment trafficTreatment) {
+        if (trafficTreatment == null ||
+                trafficTreatment.allInstructions() == null) {
+            return Sets.newHashSet();
+        }
+        Set<Instructions.OutputInstruction> resultList = Sets.newHashSet();
+        trafficTreatment.allInstructions().forEach(instruction -> {
+            if (instruction instanceof Instructions.OutputInstruction) {
+                resultList.add((Instructions.OutputInstruction) instruction);
+            }
+        });
+        return resultList;
+    }
+
+    /**
+     * Checks validity of output instructions of a bucket.
+     * A bucket of a an ALL group must only have one output instruction.
+     * Other conditions can not pass the validation.
+     *
+     * @param groupId            group identifier
+     * @param outputInstructions set of output instructions
+     * @throws RuntimeException if the instructions can not be validated
+     */
+    private static void checkOutputInstructions(GroupId groupId,
+                                                Set<Instructions.OutputInstruction> outputInstructions) {
+        if (outputInstructions.isEmpty()) {
+            throw new RuntimeException(String.format("Group bucket contains no output instruction. GroupId=%s",
+                                                     groupId));
+        }
+        if (outputInstructions.size() != 1) {
+            throw new RuntimeException(String.format("Group bucket contains more than one output instructions. " +
+                                                             "Only one is supported. GroupId=%s", groupId));
+        }
+    }
+
+    /**
+     * Checks whether a port number is a valid physical BMv2 port or not.
+     * If not, throws RuntimeException; does nothing otherwise.
+     *
+     * @param portNumber port number
+     * @throws RuntimeException iff the port number can not be validated
+     */
+    private static void validatePort(PortNumber portNumber) {
+        if (portNumber.toLong() < 0 || portNumber.toLong() >= BMV2_PORT_MAP_SIZE) {
+            throw new RuntimeException(String.format("Port number %d is not a valid BMv2 physical port number." +
+                                                             "Valid port range is [0,255]", portNumber));
+        }
+    }
+
+    /**
+     * Checks whether a port number is a valid physical BMv2 port or not.
+     * If not, throws RuntimeException; does nothing otherwise.
+     *
+     * @param portNumbers port number set
+     * @throws RuntimeException iff a port number can not be validated
+     */
+    private static void validatePorts(Set<PortNumber> portNumbers) {
+        //validate one by one
+        for (PortNumber portNumber : portNumbers) {
+            validatePort(portNumber);
+        }
+    }
+}
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/impl/package-info.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/impl/package-info.java
new file mode 100644
index 0000000..5d03c9f
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.
+ *
+ */
+/**
+ * Translator implementation for Group to BMv2 PRE Group translation logic.
+ */
+package org.onosproject.drivers.bmv2.impl;
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/AbstractDistributedBmv2Mirror.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/AbstractDistributedBmv2Mirror.java
new file mode 100644
index 0000000..d75be38
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/AbstractDistributedBmv2Mirror.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.mirror;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2Entity;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2Handle;
+import org.onosproject.net.DeviceId;
+import org.onosproject.store.service.EventuallyConsistentMap;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.WallClockTimestamp;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Abstract implementation of a distributed BMv2 mirror, backed by an
+ * {@link EventuallyConsistentMap}.
+ *
+ * @param <H> Handle class
+ * @param <E> Entry class
+ */
+@Component(immediate = true)
+public abstract class AbstractDistributedBmv2Mirror
+        <H extends Bmv2Handle, E extends Bmv2Entity>
+        implements Bmv2Mirror<H, E> {
+
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    private StorageService storageService;
+
+    private EventuallyConsistentMap<H, E> mirrorMap;
+
+    @Activate
+    public void activate() {
+        mirrorMap = storageService
+                .<H, E>eventuallyConsistentMapBuilder()
+                .withName(mapName())
+                .withSerializer(storeSerializer())
+                .withTimestampProvider((k, v) -> new WallClockTimestamp())
+                .build();
+        log.info("Started");
+    }
+
+    abstract String mapName();
+
+    abstract KryoNamespace storeSerializer();
+
+    @Deactivate
+    public void deactivate() {
+        mirrorMap.destroy();
+        mirrorMap = null;
+        log.info("Stopped");
+    }
+
+    @Override
+    public Collection<E> getAll(DeviceId deviceId) {
+        checkNotNull(deviceId);
+        return mirrorMap.entrySet().stream()
+                .filter(entry -> entry.getKey().deviceId().equals(deviceId))
+                .map(Map.Entry::getValue)
+                .collect(Collectors.toList());
+    }
+
+    @Override
+    public E get(H handle) {
+        checkNotNull(handle);
+        return mirrorMap.get(handle);
+    }
+
+    @Override
+    public void put(H handle, E entry) {
+        checkNotNull(handle);
+        checkNotNull(entry);
+        mirrorMap.put(handle, entry);
+    }
+
+    @Override
+    public void remove(H handle) {
+        checkNotNull(handle);
+        mirrorMap.remove(handle);
+    }
+}
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/Bmv2Mirror.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/Bmv2Mirror.java
new file mode 100644
index 0000000..9ade51c
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/Bmv2Mirror.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.mirror;
+
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2Entity;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2Handle;
+import org.onosproject.net.DeviceId;
+
+import java.util.Collection;
+
+/**
+ * Mirror of entities installed on a BMv2 device.
+ *
+ * @param <H> Handle class
+ * @param <E> Entity class
+ */
+public interface Bmv2Mirror<H extends Bmv2Handle, E extends Bmv2Entity> {
+
+    /**
+     * Returns all entries for the given device ID.
+     *
+     * @param deviceId device ID
+     * @return collection of BMv2 entries
+     */
+    Collection<E> getAll(DeviceId deviceId);
+
+    /**
+     * Returns entry associated to the given handle, if present, otherwise
+     * null.
+     *
+     * @param handle handle
+     * @return BMv2 entry
+     */
+    E get(H handle);
+
+    /**
+     * Stores the given entry associating it to the given handle.
+     *
+     * @param handle handle
+     * @param entry  entry
+     */
+    void put(H handle, E entry);
+
+    /**
+     * Removes the entry associated to the given handle.
+     *
+     * @param handle handle
+     */
+    void remove(H handle);
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/Bmv2PreGroupMirror.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/Bmv2PreGroupMirror.java
new file mode 100644
index 0000000..3b4f124
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/Bmv2PreGroupMirror.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.mirror;
+
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreGroup;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreGroupHandle;
+
+/**
+ * Mirror of PRE groups installed on a BMv2 device.
+ */
+public interface Bmv2PreGroupMirror extends
+        Bmv2Mirror<Bmv2PreGroupHandle, Bmv2PreGroup> {
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/DistributedBmv2PreGroupMirror.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/DistributedBmv2PreGroupMirror.java
new file mode 100644
index 0000000..1725676
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/DistributedBmv2PreGroupMirror.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.mirror;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2EntityType;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreGroup;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreGroupHandle;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreNode;
+import org.onosproject.drivers.bmv2.api.runtime.Bmv2PreNodes;
+import org.onosproject.store.serializers.KryoNamespaces;
+
+
+/**
+ * Distributed implementation of a BMv2 PRE group mirror.
+ * We need this mirror to preserve BMv2-specific identifiers of group
+ * and nodes for further operations on them after creation.
+ */
+@Component(immediate = true)
+@Service
+public class DistributedBmv2PreGroupMirror
+        extends AbstractDistributedBmv2Mirror<Bmv2PreGroupHandle, Bmv2PreGroup>
+        implements Bmv2PreGroupMirror {
+
+    private static final String DIST_MAP_NAME = "onos-bmv2-pre-group-mirror";
+
+    @Override
+    String mapName() {
+        return DIST_MAP_NAME;
+    }
+
+    @Override
+    KryoNamespace storeSerializer() {
+        return KryoNamespace.newBuilder()
+                .register(KryoNamespaces.API)
+                .register(Bmv2EntityType.class)
+                .register(Bmv2PreNode.class)
+                .register(Bmv2PreNodes.class)
+                .register(Bmv2PreGroup.class)
+                .build();
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/package-info.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/package-info.java
new file mode 100644
index 0000000..7c94e27
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/mirror/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.
+ *
+ */
+/**
+ * Mirror implementation for BMv2 driver.
+ */
+package org.onosproject.drivers.bmv2.mirror;
\ No newline at end of file