blob: 5c68a2278fce97c2153e873dfbd4255c707090dc [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.onlab.onos.cli;
2
tom32085cf2014-10-16 00:04:33 -07003import org.apache.karaf.shell.commands.Option;
tom0eb04ca2014-08-25 14:34:51 -07004import org.apache.karaf.shell.console.OsgiCommandSupport;
Thomas Vachuskab97cf282014-10-20 23:31:12 -07005import org.onlab.onos.ApplicationId;
6import org.onlab.onos.CoreService;
tom0872a172014-09-23 11:24:26 -07007import org.onlab.osgi.DefaultServiceDirectory;
8import org.onlab.osgi.ServiceNotFoundException;
tom0eb04ca2014-08-25 14:34:51 -07009
10/**
11 * Base abstraction of Karaf shell commands.
12 */
13public abstract class AbstractShellCommand extends OsgiCommandSupport {
14
tom32085cf2014-10-16 00:04:33 -070015 @Option(name = "-j", aliases = "--json", description = "Output JSON",
16 required = false, multiValued = false)
17 private boolean json = false;
18
tom0eb04ca2014-08-25 14:34:51 -070019 /**
tom6d2a43e2014-09-08 01:50:20 -070020 * Returns the reference to the implementation of the specified service.
tom0eb04ca2014-08-25 14:34:51 -070021 *
22 * @param serviceClass service class
23 * @param <T> type of service
24 * @return service implementation
tomcaf3bf72014-09-23 13:20:53 -070025 * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable
tom0eb04ca2014-08-25 14:34:51 -070026 */
tom6d2a43e2014-09-08 01:50:20 -070027 public static <T> T get(Class<T> serviceClass) {
tom0872a172014-09-23 11:24:26 -070028 return DefaultServiceDirectory.getService(serviceClass);
tom0eb04ca2014-08-25 14:34:51 -070029 }
30
tom6d2a43e2014-09-08 01:50:20 -070031 /**
Thomas Vachuskab97cf282014-10-20 23:31:12 -070032 * Returns application ID for the CLI.
33 *
34 * @return command-line application identifier
35 */
36 protected ApplicationId appId() {
37 return get(CoreService.class).registerApplication("org.onlab.onos.cli");
38 }
39
40 /**
tom6d2a43e2014-09-08 01:50:20 -070041 * Prints the arguments using the specified format.
42 *
43 * @param format format string; see {@link String#format}
44 * @param args arguments
45 */
tomcaf3bf72014-09-23 13:20:53 -070046 public void print(String format, Object... args) {
tom6d2a43e2014-09-08 01:50:20 -070047 System.out.println(String.format(format, args));
48 }
49
tom9eb57fb2014-09-11 19:42:38 -070050 /**
51 * Prints the arguments using the specified format to error stream.
52 *
53 * @param format format string; see {@link String#format}
54 * @param args arguments
55 */
tomcaf3bf72014-09-23 13:20:53 -070056 public void error(String format, Object... args) {
tom9eb57fb2014-09-11 19:42:38 -070057 System.err.println(String.format(format, args));
58 }
59
tom0872a172014-09-23 11:24:26 -070060 /**
61 * Executes this command.
62 */
63 protected abstract void execute();
64
tom32085cf2014-10-16 00:04:33 -070065 /**
66 * Indicates whether JSON format should be output.
67 *
68 * @return true if JSON is requested
69 */
70 protected boolean outputJson() {
71 return json;
72 }
73
tom0872a172014-09-23 11:24:26 -070074 @Override
75 protected Object doExecute() throws Exception {
76 try {
77 execute();
78 } catch (ServiceNotFoundException e) {
79 error(e.getMessage());
80 }
81 return null;
82 }
83
tom0eb04ca2014-08-25 14:34:51 -070084}