Added some CLI commands and fixed pom.xml and features.xml to use commons-lang 2.6 (bundle)
Showing
11 changed files
with
175 additions
and
14 deletions
... | @@ -10,15 +10,25 @@ import org.osgi.framework.FrameworkUtil; | ... | @@ -10,15 +10,25 @@ import org.osgi.framework.FrameworkUtil; |
10 | public abstract class AbstractShellCommand extends OsgiCommandSupport { | 10 | public abstract class AbstractShellCommand extends OsgiCommandSupport { |
11 | 11 | ||
12 | /** | 12 | /** |
13 | - * Returns the reference to the implementaiton of the specified service. | 13 | + * Returns the reference to the implementation of the specified service. |
14 | * | 14 | * |
15 | * @param serviceClass service class | 15 | * @param serviceClass service class |
16 | * @param <T> type of service | 16 | * @param <T> type of service |
17 | * @return service implementation | 17 | * @return service implementation |
18 | */ | 18 | */ |
19 | - static <T> T get(Class<T> serviceClass) { | 19 | + public static <T> T get(Class<T> serviceClass) { |
20 | BundleContext bc = FrameworkUtil.getBundle(AbstractShellCommand.class).getBundleContext(); | 20 | BundleContext bc = FrameworkUtil.getBundle(AbstractShellCommand.class).getBundleContext(); |
21 | return bc.getService(bc.getServiceReference(serviceClass)); | 21 | return bc.getService(bc.getServiceReference(serviceClass)); |
22 | } | 22 | } |
23 | 23 | ||
24 | + /** | ||
25 | + * Prints the arguments using the specified format. | ||
26 | + * | ||
27 | + * @param format format string; see {@link String#format} | ||
28 | + * @param args arguments | ||
29 | + */ | ||
30 | + public static void print(String format, Object... args) { | ||
31 | + System.out.println(String.format(format, args)); | ||
32 | + } | ||
33 | + | ||
24 | } | 34 | } | ... | ... |
... | @@ -2,7 +2,6 @@ package org.onlab.onos.cli; | ... | @@ -2,7 +2,6 @@ package org.onlab.onos.cli; |
2 | 2 | ||
3 | import org.apache.karaf.shell.commands.Argument; | 3 | import org.apache.karaf.shell.commands.Argument; |
4 | import org.apache.karaf.shell.commands.Command; | 4 | import org.apache.karaf.shell.commands.Command; |
5 | -import org.apache.karaf.shell.console.OsgiCommandSupport; | ||
6 | import org.onlab.onos.GreetService; | 5 | import org.onlab.onos.GreetService; |
7 | 6 | ||
8 | /** | 7 | /** |
... | @@ -10,7 +9,7 @@ import org.onlab.onos.GreetService; | ... | @@ -10,7 +9,7 @@ import org.onlab.onos.GreetService; |
10 | * use of an optional parameter as well. | 9 | * use of an optional parameter as well. |
11 | */ | 10 | */ |
12 | @Command(scope = "onos", name = "greet", description = "Issues a greeting") | 11 | @Command(scope = "onos", name = "greet", description = "Issues a greeting") |
13 | -public class GreetCommand extends OsgiCommandSupport { | 12 | +public class GreetCommand extends AbstractShellCommand { |
14 | 13 | ||
15 | @Argument(index = 0, name = "name", description = "Name to greet", | 14 | @Argument(index = 0, name = "name", description = "Name to greet", |
16 | required = false, multiValued = false) | 15 | required = false, multiValued = false) |
... | @@ -18,7 +17,7 @@ public class GreetCommand extends OsgiCommandSupport { | ... | @@ -18,7 +17,7 @@ public class GreetCommand extends OsgiCommandSupport { |
18 | 17 | ||
19 | @Override | 18 | @Override |
20 | protected Object doExecute() throws Exception { | 19 | protected Object doExecute() throws Exception { |
21 | - System.out.println(getService(GreetService.class).yo(name)); | 20 | + print(getService(GreetService.class).yo(name)); |
22 | return null; | 21 | return null; |
23 | } | 22 | } |
24 | } | 23 | } | ... | ... |
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.cli.AbstractShellCommand; | ||
6 | +import org.onlab.onos.net.Device; | ||
7 | +import org.onlab.onos.net.device.DeviceService; | ||
8 | + | ||
9 | +import java.util.Iterator; | ||
10 | +import java.util.List; | ||
11 | +import java.util.SortedSet; | ||
12 | + | ||
13 | +/** | ||
14 | + * Device ID completer. | ||
15 | + */ | ||
16 | +public class DeviceIdCompleter implements Completer { | ||
17 | + @Override | ||
18 | + public int complete(String buffer, int cursor, List<String> candidates) { | ||
19 | + // Delegate string completer | ||
20 | + StringsCompleter delegate = new StringsCompleter(); | ||
21 | + | ||
22 | + // Fetch our service and feed it's offerings to the string completer | ||
23 | + DeviceService service = AbstractShellCommand.get(DeviceService.class); | ||
24 | + Iterator<Device> it = service.getDevices().iterator(); | ||
25 | + SortedSet<String> strings = delegate.getStrings(); | ||
26 | + while (it.hasNext()) { | ||
27 | + strings.add(it.next().id().uri().toString()); | ||
28 | + } | ||
29 | + | ||
30 | + // Now let the completer do the work for figuring out what to offer. | ||
31 | + return delegate.complete(buffer, cursor, candidates); | ||
32 | + } | ||
33 | + | ||
34 | +} |
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.Port; | ||
7 | +import org.onlab.onos.net.device.DeviceService; | ||
8 | + | ||
9 | +import static org.onlab.onos.net.DeviceId.deviceId; | ||
10 | + | ||
11 | +/** | ||
12 | + * Lists all infrastructure links. | ||
13 | + */ | ||
14 | +@Command(scope = "onos", name = "ports", | ||
15 | + description = "Lists all ports of a device") | ||
16 | +public class DevicePortsListCommand extends AbstractShellCommand { | ||
17 | + | ||
18 | + private static final String FMT = "port=%s, state=%s"; | ||
19 | + | ||
20 | + @Argument(index = 0, name = "deviceId", description = "Device ID", | ||
21 | + required = true, multiValued = false) | ||
22 | + String deviceId = null; | ||
23 | + | ||
24 | + @Override | ||
25 | + protected Object doExecute() throws Exception { | ||
26 | + DeviceService service = getService(DeviceService.class); | ||
27 | + Iterable<Port> ports = service.getPorts(deviceId(deviceId)); | ||
28 | + for (Port port : ports) { | ||
29 | + print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled"); | ||
30 | + } | ||
31 | + return null; | ||
32 | + } | ||
33 | +} |
1 | +package org.onlab.onos.cli.net; | ||
2 | + | ||
3 | +import org.apache.karaf.shell.commands.Command; | ||
4 | +import org.onlab.onos.cli.AbstractShellCommand; | ||
5 | +import org.onlab.onos.net.Device; | ||
6 | +import org.onlab.onos.net.device.DeviceService; | ||
7 | + | ||
8 | +/** | ||
9 | + * Lists all infrastructure devices. | ||
10 | + */ | ||
11 | +@Command(scope = "onos", name = "devices", | ||
12 | + description = "Lists all infrastructure devices") | ||
13 | +public class DevicesListCommand extends AbstractShellCommand { | ||
14 | + | ||
15 | + private static final String FMT = | ||
16 | + "id=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s"; | ||
17 | + | ||
18 | + @Override | ||
19 | + protected Object doExecute() throws Exception { | ||
20 | + for (Device device : getService(DeviceService.class).getDevices()) { | ||
21 | + print(FMT, device.id(), device.type(), device.manufacturer(), | ||
22 | + device.hwVersion(), device.swVersion(), device.serialNumber()); | ||
23 | + } | ||
24 | + return null; | ||
25 | + } | ||
26 | +} |
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.Link; | ||
7 | +import org.onlab.onos.net.link.LinkService; | ||
8 | + | ||
9 | +import static org.onlab.onos.net.DeviceId.deviceId; | ||
10 | + | ||
11 | +/** | ||
12 | + * Lists all infrastructure links. | ||
13 | + */ | ||
14 | +@Command(scope = "onos", name = "links", | ||
15 | + description = "Lists all infrastructure links") | ||
16 | +public class LinksListCommand extends AbstractShellCommand { | ||
17 | + | ||
18 | + private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s"; | ||
19 | + | ||
20 | + @Argument(index = 0, name = "deviceId", description = "Device ID", | ||
21 | + required = false, multiValued = false) | ||
22 | + String deviceId = null; | ||
23 | + | ||
24 | + | ||
25 | + @Override | ||
26 | + protected Object doExecute() throws Exception { | ||
27 | + LinkService service = getService(LinkService.class); | ||
28 | + Iterable<Link> links = deviceId != null ? | ||
29 | + service.getDeviceLinks(deviceId(deviceId)) : service.getLinks(); | ||
30 | + for (Link link : links) { | ||
31 | + print(FMT, link.src().deviceId(), link.src().port(), | ||
32 | + link.dst().deviceId(), link.dst().port(), link.type()); | ||
33 | + } | ||
34 | + return null; | ||
35 | + } | ||
36 | +} |
... | @@ -2,13 +2,31 @@ | ... | @@ -2,13 +2,31 @@ |
2 | 2 | ||
3 | <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0"> | 3 | <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0"> |
4 | <command> | 4 | <command> |
5 | - <action class="org.onlab.onos.cli.GreetCommand"/> | 5 | + <action class="org.onlab.onos.cli.net.DevicesListCommand"/> |
6 | + </command> | ||
7 | + <command> | ||
8 | + <action class="org.onlab.onos.cli.net.DevicePortsListCommand"/> | ||
9 | + <completers> | ||
10 | + <ref component-id="deviceIdCompleter"/> | ||
11 | + </completers> | ||
12 | + </command> | ||
13 | + <command> | ||
14 | + <action class="org.onlab.onos.cli.net.LinksListCommand"/> | ||
6 | <completers> | 15 | <completers> |
7 | - <ref component-id="nameCompleter"/> | 16 | + <ref component-id="deviceIdCompleter"/> |
8 | </completers> | 17 | </completers> |
9 | </command> | 18 | </command> |
19 | + | ||
20 | + <!--<command>--> | ||
21 | + <!--<action class="org.onlab.onos.cli.GreetCommand"/>--> | ||
22 | + <!--<completers>--> | ||
23 | + <!--<ref component-id="nameCompleter"/>--> | ||
24 | + <!--</completers>--> | ||
25 | + <!--</command>--> | ||
10 | </command-bundle> | 26 | </command-bundle> |
11 | 27 | ||
28 | + <bean id="deviceIdCompleter" class="org.onlab.onos.cli.net.DeviceIdCompleter"/> | ||
29 | + | ||
12 | <bean id="nameCompleter" class="org.onlab.onos.cli.NameCompleter"/> | 30 | <bean id="nameCompleter" class="org.onlab.onos.cli.NameCompleter"/> |
13 | 31 | ||
14 | </blueprint> | 32 | </blueprint> | ... | ... |
... | @@ -5,7 +5,8 @@ | ... | @@ -5,7 +5,8 @@ |
5 | 5 | ||
6 | <feature name="onos-thirdparty-base" version="1.0.0" | 6 | <feature name="onos-thirdparty-base" version="1.0.0" |
7 | description="ONOS 3rd party dependencies"> | 7 | description="ONOS 3rd party dependencies"> |
8 | - <bundle>mvn:com.google.guava/guava/17.0</bundle> | 8 | + <bundle>mvn:commons-lang/commons-lang/2.6</bundle> |
9 | + <bundle>mvn:com.google.guava/guava/18.0</bundle> | ||
9 | </feature> | 10 | </feature> |
10 | 11 | ||
11 | <feature name="onos-thirdparty-web" version="1.0.0" | 12 | <feature name="onos-thirdparty-web" version="1.0.0" | ... | ... |
... | @@ -68,6 +68,11 @@ | ... | @@ -68,6 +68,11 @@ |
68 | <scope>test</scope> | 68 | <scope>test</scope> |
69 | </dependency> | 69 | </dependency> |
70 | 70 | ||
71 | + <dependency> | ||
72 | + <groupId>commons-lang</groupId> | ||
73 | + <artifactId>commons-lang</artifactId> | ||
74 | + <version>2.6</version> | ||
75 | + </dependency> | ||
71 | 76 | ||
72 | <!-- Web related --> | 77 | <!-- Web related --> |
73 | <dependency> | 78 | <dependency> | ... | ... |
... | @@ -20,9 +20,13 @@ | ... | @@ -20,9 +20,13 @@ |
20 | <dependency> | 20 | <dependency> |
21 | <groupId>com.google.guava</groupId> | 21 | <groupId>com.google.guava</groupId> |
22 | <artifactId>guava-testlib</artifactId> | 22 | <artifactId>guava-testlib</artifactId> |
23 | - <version>17.0</version> | ||
24 | <scope>test</scope> | 23 | <scope>test</scope> |
25 | </dependency> | 24 | </dependency> |
25 | + | ||
26 | + <dependency> | ||
27 | + <groupId>commons-lang</groupId> | ||
28 | + <artifactId>commons-lang</artifactId> | ||
29 | + </dependency> | ||
26 | </dependencies> | 30 | </dependencies> |
27 | 31 | ||
28 | </project> | 32 | </project> | ... | ... |
... | @@ -27,11 +27,6 @@ | ... | @@ -27,11 +27,6 @@ |
27 | <groupId>com.google.guava</groupId> | 27 | <groupId>com.google.guava</groupId> |
28 | <artifactId>guava</artifactId> | 28 | <artifactId>guava</artifactId> |
29 | </dependency> | 29 | </dependency> |
30 | - <dependency> | ||
31 | - <groupId>commons-lang</groupId> | ||
32 | - <artifactId>commons-lang</artifactId> | ||
33 | - <version>2.3</version> | ||
34 | - </dependency> | ||
35 | </dependencies> | 30 | </dependencies> |
36 | 31 | ||
37 | <build> | 32 | <build> | ... | ... |
-
Please register or login to post a comment