tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli; |
| 2 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 3 | import org.apache.karaf.shell.commands.Option; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 4 | import org.apache.karaf.shell.console.OsgiCommandSupport; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 5 | import org.onlab.onos.ApplicationId; |
| 6 | import org.onlab.onos.CoreService; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 7 | import org.onlab.osgi.DefaultServiceDirectory; |
| 8 | import org.onlab.osgi.ServiceNotFoundException; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Base abstraction of Karaf shell commands. |
| 12 | */ |
| 13 | public abstract class AbstractShellCommand extends OsgiCommandSupport { |
| 14 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 15 | @Option(name = "-j", aliases = "--json", description = "Output JSON", |
| 16 | required = false, multiValued = false) |
| 17 | private boolean json = false; |
| 18 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 19 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 20 | * Returns the reference to the implementation of the specified service. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 21 | * |
| 22 | * @param serviceClass service class |
| 23 | * @param <T> type of service |
| 24 | * @return service implementation |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 25 | * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 26 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 27 | public static <T> T get(Class<T> serviceClass) { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 28 | return DefaultServiceDirectory.getService(serviceClass); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 29 | } |
| 30 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 31 | /** |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 32 | * 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 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 41 | * Prints the arguments using the specified format. |
| 42 | * |
| 43 | * @param format format string; see {@link String#format} |
| 44 | * @param args arguments |
| 45 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 46 | public void print(String format, Object... args) { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 47 | System.out.println(String.format(format, args)); |
| 48 | } |
| 49 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 50 | /** |
| 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 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 56 | public void error(String format, Object... args) { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 57 | System.err.println(String.format(format, args)); |
| 58 | } |
| 59 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Executes this command. |
| 62 | */ |
| 63 | protected abstract void execute(); |
| 64 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 65 | /** |
| 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 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 74 | @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 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 84 | } |