tom

Added a command-line to set device mastership role.

1 +package org.onlab.onos.cli.net;
2 +
3 +import org.apache.karaf.shell.commands.Argument;
4 +import org.apache.karaf.shell.commands.Command;
5 +import org.onlab.onos.cli.AbstractShellCommand;
6 +import org.onlab.onos.net.DeviceId;
7 +import org.onlab.onos.net.MastershipRole;
8 +import org.onlab.onos.net.device.DeviceAdminService;
9 +
10 +/**
11 + * Sets role of the controller node for the given infrastructure device.
12 + */
13 +@Command(scope = "onos", name = "device-role",
14 + description = "Sets role of the controller node for the given infrastructure device")
15 +public class DeviceRoleCommand extends AbstractShellCommand {
16 +
17 + @Argument(index = 0, name = "uri", description = "Device ID",
18 + required = true, multiValued = false)
19 + String uri = null;
20 +
21 + @Argument(index = 1, name = "role", description = "Mastership role",
22 + required = true, multiValued = false)
23 + String role = null;
24 +
25 + @Override
26 + protected Object doExecute() throws Exception {
27 + MastershipRole mastershipRole = MastershipRole.valueOf(role.toUpperCase());
28 + getService(DeviceAdminService.class).setRole(DeviceId.deviceId(uri),
29 + mastershipRole);
30 + return null;
31 + }
32 +
33 +}
1 +package org.onlab.onos.cli.net;
2 +
3 +import org.apache.karaf.shell.console.Completer;
4 +import org.apache.karaf.shell.console.completer.StringsCompleter;
5 +import org.onlab.onos.net.MastershipRole;
6 +
7 +import java.util.List;
8 +import java.util.SortedSet;
9 +
10 +/**
11 + * Device mastership role completer.
12 + */
13 +public class RoleCompleter implements Completer {
14 + @Override
15 + public int complete(String buffer, int cursor, List<String> candidates) {
16 + // Delegate string completer
17 + StringsCompleter delegate = new StringsCompleter();
18 + SortedSet<String> strings = delegate.getStrings();
19 + strings.add(MastershipRole.MASTER.toString().toLowerCase());
20 + strings.add(MastershipRole.STANDBY.toString().toLowerCase());
21 + strings.add(MastershipRole.NONE.toString().toLowerCase());
22 +
23 + // Now let the completer do the work for figuring out what to offer.
24 + return delegate.complete(buffer, cursor, candidates);
25 + }
26 +
27 +}
...@@ -16,6 +16,13 @@ ...@@ -16,6 +16,13 @@
16 <ref component-id="deviceIdCompleter"/> 16 <ref component-id="deviceIdCompleter"/>
17 </completers> 17 </completers>
18 </command> 18 </command>
19 + <command>
20 + <action class="org.onlab.onos.cli.net.DeviceRoleCommand"/>
21 + <completers>
22 + <ref component-id="deviceIdCompleter"/>
23 + <ref component-id="roleCompleter"/>
24 + </completers>
25 + </command>
19 26
20 <command> 27 <command>
21 <action class="org.onlab.onos.cli.net.LinksListCommand"/> 28 <action class="org.onlab.onos.cli.net.LinksListCommand"/>
...@@ -33,6 +40,7 @@ ...@@ -33,6 +40,7 @@
33 </command-bundle> 40 </command-bundle>
34 41
35 <bean id="deviceIdCompleter" class="org.onlab.onos.cli.net.DeviceIdCompleter"/> 42 <bean id="deviceIdCompleter" class="org.onlab.onos.cli.net.DeviceIdCompleter"/>
43 + <bean id="roleCompleter" class="org.onlab.onos.cli.net.RoleCompleter"/>
36 44
37 <bean id="nameCompleter" class="org.onlab.onos.cli.NameCompleter"/> 45 <bean id="nameCompleter" class="org.onlab.onos.cli.NameCompleter"/>
38 46
......
...@@ -139,7 +139,7 @@ public class SimpleDeviceManager ...@@ -139,7 +139,7 @@ public class SimpleDeviceManager
139 Device device = event.subject(); 139 Device device = event.subject();
140 DeviceProvider provider = getProvider(device.providerId()); 140 DeviceProvider provider = getProvider(device.providerId());
141 if (provider != null) { 141 if (provider != null) {
142 - provider.roleChanged(device, newRole); 142 + triggerRoleSelection(device, provider);
143 } 143 }
144 post(event); 144 post(event);
145 } 145 }
......
...@@ -143,11 +143,13 @@ public class SimpleDeviceManagerTest { ...@@ -143,11 +143,13 @@ public class SimpleDeviceManagerTest {
143 } 143 }
144 144
145 @Test 145 @Test
146 - public void setRole() { 146 + public void setRole() throws InterruptedException {
147 connectDevice(DID1, SW1); 147 connectDevice(DID1, SW1);
148 admin.setRole(DID1, MastershipRole.STANDBY); 148 admin.setRole(DID1, MastershipRole.STANDBY);
149 validateEvents(DEVICE_ADDED, DEVICE_MASTERSHIP_CHANGED); 149 validateEvents(DEVICE_ADDED, DEVICE_MASTERSHIP_CHANGED);
150 assertEquals("incorrect role", MastershipRole.STANDBY, service.getRole(DID1)); 150 assertEquals("incorrect role", MastershipRole.STANDBY, service.getRole(DID1));
151 +
152 + Thread.sleep(200); // FIXME: replace this with more reliable and faster testing means!!!
151 assertEquals("incorrect device", DID1, provider.deviceReceived.id()); 153 assertEquals("incorrect device", DID1, provider.deviceReceived.id());
152 assertEquals("incorrect role", MastershipRole.STANDBY, provider.roleReceived); 154 assertEquals("incorrect role", MastershipRole.STANDBY, provider.roleReceived);
153 } 155 }
......