blob: 628754acebbca576dda36376567988a5a95e321b [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom0eb04ca2014-08-25 14:34:51 -070019package org.onlab.onos.cli;
20
tom32085cf2014-10-16 00:04:33 -070021import org.apache.karaf.shell.commands.Option;
tom0eb04ca2014-08-25 14:34:51 -070022import org.apache.karaf.shell.console.OsgiCommandSupport;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070023import org.onlab.onos.ApplicationId;
24import org.onlab.onos.CoreService;
tom0872a172014-09-23 11:24:26 -070025import org.onlab.osgi.DefaultServiceDirectory;
26import org.onlab.osgi.ServiceNotFoundException;
tom0eb04ca2014-08-25 14:34:51 -070027
28/**
29 * Base abstraction of Karaf shell commands.
30 */
31public abstract class AbstractShellCommand extends OsgiCommandSupport {
32
tom32085cf2014-10-16 00:04:33 -070033 @Option(name = "-j", aliases = "--json", description = "Output JSON",
34 required = false, multiValued = false)
35 private boolean json = false;
36
tom0eb04ca2014-08-25 14:34:51 -070037 /**
tom6d2a43e2014-09-08 01:50:20 -070038 * Returns the reference to the implementation of the specified service.
tom0eb04ca2014-08-25 14:34:51 -070039 *
40 * @param serviceClass service class
41 * @param <T> type of service
42 * @return service implementation
tomcaf3bf72014-09-23 13:20:53 -070043 * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable
tom0eb04ca2014-08-25 14:34:51 -070044 */
tom6d2a43e2014-09-08 01:50:20 -070045 public static <T> T get(Class<T> serviceClass) {
tom0872a172014-09-23 11:24:26 -070046 return DefaultServiceDirectory.getService(serviceClass);
tom0eb04ca2014-08-25 14:34:51 -070047 }
48
tom6d2a43e2014-09-08 01:50:20 -070049 /**
Thomas Vachuskab97cf282014-10-20 23:31:12 -070050 * Returns application ID for the CLI.
51 *
52 * @return command-line application identifier
53 */
54 protected ApplicationId appId() {
55 return get(CoreService.class).registerApplication("org.onlab.onos.cli");
56 }
57
58 /**
tom6d2a43e2014-09-08 01:50:20 -070059 * Prints the arguments using the specified format.
60 *
61 * @param format format string; see {@link String#format}
62 * @param args arguments
63 */
tomcaf3bf72014-09-23 13:20:53 -070064 public void print(String format, Object... args) {
tom6d2a43e2014-09-08 01:50:20 -070065 System.out.println(String.format(format, args));
66 }
67
tom9eb57fb2014-09-11 19:42:38 -070068 /**
69 * Prints the arguments using the specified format to error stream.
70 *
71 * @param format format string; see {@link String#format}
72 * @param args arguments
73 */
tomcaf3bf72014-09-23 13:20:53 -070074 public void error(String format, Object... args) {
tom9eb57fb2014-09-11 19:42:38 -070075 System.err.println(String.format(format, args));
76 }
77
tom0872a172014-09-23 11:24:26 -070078 /**
79 * Executes this command.
80 */
81 protected abstract void execute();
82
tom32085cf2014-10-16 00:04:33 -070083 /**
84 * Indicates whether JSON format should be output.
85 *
86 * @return true if JSON is requested
87 */
88 protected boolean outputJson() {
89 return json;
90 }
91
tom0872a172014-09-23 11:24:26 -070092 @Override
93 protected Object doExecute() throws Exception {
94 try {
95 execute();
96 } catch (ServiceNotFoundException e) {
97 error(e.getMessage());
98 }
99 return null;
100 }
101
tom0eb04ca2014-08-25 14:34:51 -0700102}