Added a command-line to set device mastership role.
diff --git a/cli/src/main/java/org/onlab/onos/cli/net/DeviceRoleCommand.java b/cli/src/main/java/org/onlab/onos/cli/net/DeviceRoleCommand.java
new file mode 100644
index 0000000..32da169
--- /dev/null
+++ b/cli/src/main/java/org/onlab/onos/cli/net/DeviceRoleCommand.java
@@ -0,0 +1,33 @@
+package org.onlab.onos.cli.net;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.onos.cli.AbstractShellCommand;
+import org.onlab.onos.net.DeviceId;
+import org.onlab.onos.net.MastershipRole;
+import org.onlab.onos.net.device.DeviceAdminService;
+
+/**
+ * Sets role of the controller node for the given infrastructure device.
+ */
+@Command(scope = "onos", name = "device-role",
+         description = "Sets role of the controller node for the given infrastructure device")
+public class DeviceRoleCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "uri", description = "Device ID",
+              required = true, multiValued = false)
+    String uri = null;
+
+    @Argument(index = 1, name = "role", description = "Mastership role",
+              required = true, multiValued = false)
+    String role = null;
+
+    @Override
+    protected Object doExecute() throws Exception {
+        MastershipRole mastershipRole = MastershipRole.valueOf(role.toUpperCase());
+        getService(DeviceAdminService.class).setRole(DeviceId.deviceId(uri),
+                                                     mastershipRole);
+        return null;
+    }
+
+}
diff --git a/cli/src/main/java/org/onlab/onos/cli/net/RoleCompleter.java b/cli/src/main/java/org/onlab/onos/cli/net/RoleCompleter.java
new file mode 100644
index 0000000..b540fca
--- /dev/null
+++ b/cli/src/main/java/org/onlab/onos/cli/net/RoleCompleter.java
@@ -0,0 +1,27 @@
+package org.onlab.onos.cli.net;
+
+import org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onlab.onos.net.MastershipRole;
+
+import java.util.List;
+import java.util.SortedSet;
+
+/**
+ * Device mastership role completer.
+ */
+public class RoleCompleter implements Completer {
+    @Override
+    public int complete(String buffer, int cursor, List<String> candidates) {
+        // Delegate string completer
+        StringsCompleter delegate = new StringsCompleter();
+        SortedSet<String> strings = delegate.getStrings();
+        strings.add(MastershipRole.MASTER.toString().toLowerCase());
+        strings.add(MastershipRole.STANDBY.toString().toLowerCase());
+        strings.add(MastershipRole.NONE.toString().toLowerCase());
+
+        // Now let the completer do the work for figuring out what to offer.
+        return delegate.complete(buffer, cursor, candidates);
+    }
+
+}
diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 7aa67be..96c494d 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -16,6 +16,13 @@
                 <ref component-id="deviceIdCompleter"/>
             </completers>
         </command>
+        <command>
+            <action class="org.onlab.onos.cli.net.DeviceRoleCommand"/>
+            <completers>
+                <ref component-id="deviceIdCompleter"/>
+                <ref component-id="roleCompleter"/>
+            </completers>
+        </command>
 
         <command>
             <action class="org.onlab.onos.cli.net.LinksListCommand"/>
@@ -33,6 +40,7 @@
     </command-bundle>
 
     <bean id="deviceIdCompleter" class="org.onlab.onos.cli.net.DeviceIdCompleter"/>
+    <bean id="roleCompleter" class="org.onlab.onos.cli.net.RoleCompleter"/>
 
     <bean id="nameCompleter" class="org.onlab.onos.cli.NameCompleter"/>
 
diff --git a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManager.java b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManager.java
index ac84e7b..2914673 100644
--- a/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManager.java
+++ b/net/core/trivial/src/main/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManager.java
@@ -139,7 +139,7 @@
             Device device = event.subject();
             DeviceProvider provider = getProvider(device.providerId());
             if (provider != null) {
-                provider.roleChanged(device, newRole);
+                triggerRoleSelection(device, provider);
             }
             post(event);
         }
diff --git a/net/core/trivial/src/test/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManagerTest.java b/net/core/trivial/src/test/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManagerTest.java
index 5bc60c0..398e9cb 100644
--- a/net/core/trivial/src/test/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManagerTest.java
+++ b/net/core/trivial/src/test/java/org/onlab/onos/net/trivial/impl/SimpleDeviceManagerTest.java
@@ -143,11 +143,13 @@
     }
 
     @Test
-    public void setRole() {
+    public void setRole() throws InterruptedException {
         connectDevice(DID1, SW1);
         admin.setRole(DID1, MastershipRole.STANDBY);
         validateEvents(DEVICE_ADDED, DEVICE_MASTERSHIP_CHANGED);
         assertEquals("incorrect role", MastershipRole.STANDBY, service.getRole(DID1));
+
+        Thread.sleep(200); // FIXME: replace this with more reliable and faster testing means!!!
         assertEquals("incorrect device", DID1, provider.deviceReceived.id());
         assertEquals("incorrect role", MastershipRole.STANDBY, provider.roleReceived);
     }