blob: 4dd767ba9cf70ab9d6e6d758a77ffc595063e8d3 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli;
tom0eb04ca2014-08-25 14:34:51 -070017
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070018import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
tom32085cf2014-10-16 00:04:33 -070020import org.apache.karaf.shell.commands.Option;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070021import org.apache.karaf.shell.console.AbstractAction;
22import org.onlab.osgi.DefaultServiceDirectory;
23import org.onlab.osgi.ServiceNotFoundException;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.Annotations;
tom0eb04ca2014-08-25 14:34:51 -070027
28/**
29 * Base abstraction of Karaf shell commands.
30 */
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070031public abstract class AbstractShellCommand extends AbstractAction {
tom0eb04ca2014-08-25 14:34:51 -070032
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() {
Ray Milkey02479862015-02-17 17:02:19 -080055 return get(CoreService.class)
56 .registerApplication("org.onosproject.cli");
Thomas Vachuskab97cf282014-10-20 23:31:12 -070057 }
58
59 /**
tom6d2a43e2014-09-08 01:50:20 -070060 * Prints the arguments using the specified format.
61 *
62 * @param format format string; see {@link String#format}
63 * @param args arguments
64 */
tomcaf3bf72014-09-23 13:20:53 -070065 public void print(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +000066 System.out.println(String.format(format, args));
tom6d2a43e2014-09-08 01:50:20 -070067 }
68
tom9eb57fb2014-09-11 19:42:38 -070069 /**
70 * Prints the arguments using the specified format to error stream.
71 *
72 * @param format format string; see {@link String#format}
73 * @param args arguments
74 */
tomcaf3bf72014-09-23 13:20:53 -070075 public void error(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +000076 System.err.println(String.format(format, args));
tom9eb57fb2014-09-11 19:42:38 -070077 }
78
tom0872a172014-09-23 11:24:26 -070079 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070080 * Produces a string image of the specified key/value annotations.
81 *
82 * @param annotations key/value annotations
83 * @return string image with ", k1=v1, k2=v2, ..." pairs
84 */
85 public static String annotations(Annotations annotations) {
86 StringBuilder sb = new StringBuilder();
87 for (String key : annotations.keys()) {
88 sb.append(", ").append(key).append('=').append(annotations.value(key));
89 }
90 return sb.toString();
91 }
92
93 /**
94 * Produces a JSON object from the specified key/value annotations.
95 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080096 * @param mapper ObjectMapper to use while converting to JSON
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070097 * @param annotations key/value annotations
98 * @return JSON object
99 */
100 public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) {
101 ObjectNode result = mapper.createObjectNode();
102 for (String key : annotations.keys()) {
103 result.put(key, annotations.value(key));
104 }
105 return result;
106 }
107
108 /**
tom0872a172014-09-23 11:24:26 -0700109 * Executes this command.
110 */
111 protected abstract void execute();
112
tom32085cf2014-10-16 00:04:33 -0700113 /**
114 * Indicates whether JSON format should be output.
115 *
116 * @return true if JSON is requested
117 */
118 protected boolean outputJson() {
119 return json;
120 }
121
tom0872a172014-09-23 11:24:26 -0700122 @Override
123 protected Object doExecute() throws Exception {
124 try {
125 execute();
126 } catch (ServiceNotFoundException e) {
127 error(e.getMessage());
128 }
129 return null;
130 }
131
tom0eb04ca2014-08-25 14:34:51 -0700132}