blob: b121dcbf26894768eb7660508647efb993eed363 [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
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070021import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.node.ObjectNode;
tom32085cf2014-10-16 00:04:33 -070023import org.apache.karaf.shell.commands.Option;
tom0eb04ca2014-08-25 14:34:51 -070024import org.apache.karaf.shell.console.OsgiCommandSupport;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070025import org.onlab.onos.core.ApplicationId;
26import org.onlab.onos.core.CoreService;
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070027import org.onlab.onos.net.Annotations;
tom0872a172014-09-23 11:24:26 -070028import org.onlab.osgi.DefaultServiceDirectory;
29import org.onlab.osgi.ServiceNotFoundException;
tom0eb04ca2014-08-25 14:34:51 -070030
31/**
32 * Base abstraction of Karaf shell commands.
33 */
34public abstract class AbstractShellCommand extends OsgiCommandSupport {
35
tom32085cf2014-10-16 00:04:33 -070036 @Option(name = "-j", aliases = "--json", description = "Output JSON",
37 required = false, multiValued = false)
38 private boolean json = false;
39
tom0eb04ca2014-08-25 14:34:51 -070040 /**
tom6d2a43e2014-09-08 01:50:20 -070041 * Returns the reference to the implementation of the specified service.
tom0eb04ca2014-08-25 14:34:51 -070042 *
43 * @param serviceClass service class
44 * @param <T> type of service
45 * @return service implementation
tomcaf3bf72014-09-23 13:20:53 -070046 * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable
tom0eb04ca2014-08-25 14:34:51 -070047 */
tom6d2a43e2014-09-08 01:50:20 -070048 public static <T> T get(Class<T> serviceClass) {
tom0872a172014-09-23 11:24:26 -070049 return DefaultServiceDirectory.getService(serviceClass);
tom0eb04ca2014-08-25 14:34:51 -070050 }
51
tom6d2a43e2014-09-08 01:50:20 -070052 /**
Thomas Vachuskab97cf282014-10-20 23:31:12 -070053 * Returns application ID for the CLI.
54 *
55 * @return command-line application identifier
56 */
57 protected ApplicationId appId() {
58 return get(CoreService.class).registerApplication("org.onlab.onos.cli");
59 }
60
61 /**
tom6d2a43e2014-09-08 01:50:20 -070062 * Prints the arguments using the specified format.
63 *
64 * @param format format string; see {@link String#format}
65 * @param args arguments
66 */
tomcaf3bf72014-09-23 13:20:53 -070067 public void print(String format, Object... args) {
tom6d2a43e2014-09-08 01:50:20 -070068 System.out.println(String.format(format, args));
69 }
70
tom9eb57fb2014-09-11 19:42:38 -070071 /**
72 * Prints the arguments using the specified format to error stream.
73 *
74 * @param format format string; see {@link String#format}
75 * @param args arguments
76 */
tomcaf3bf72014-09-23 13:20:53 -070077 public void error(String format, Object... args) {
tom9eb57fb2014-09-11 19:42:38 -070078 System.err.println(String.format(format, args));
79 }
80
tom0872a172014-09-23 11:24:26 -070081 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070082 * Produces a string image of the specified key/value annotations.
83 *
84 * @param annotations key/value annotations
85 * @return string image with ", k1=v1, k2=v2, ..." pairs
86 */
87 public static String annotations(Annotations annotations) {
88 StringBuilder sb = new StringBuilder();
89 for (String key : annotations.keys()) {
90 sb.append(", ").append(key).append('=').append(annotations.value(key));
91 }
92 return sb.toString();
93 }
94
95 /**
96 * Produces a JSON object from the specified key/value annotations.
97 *
98 * @param annotations key/value annotations
99 * @return JSON object
100 */
101 public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) {
102 ObjectNode result = mapper.createObjectNode();
103 for (String key : annotations.keys()) {
104 result.put(key, annotations.value(key));
105 }
106 return result;
107 }
108
109 /**
tom0872a172014-09-23 11:24:26 -0700110 * Executes this command.
111 */
112 protected abstract void execute();
113
tom32085cf2014-10-16 00:04:33 -0700114 /**
115 * Indicates whether JSON format should be output.
116 *
117 * @return true if JSON is requested
118 */
119 protected boolean outputJson() {
120 return json;
121 }
122
tom0872a172014-09-23 11:24:26 -0700123 @Override
124 protected Object doExecute() throws Exception {
125 try {
126 execute();
127 } catch (ServiceNotFoundException e) {
128 error(e.getMessage());
129 }
130 return null;
131 }
132
tom0eb04ca2014-08-25 14:34:51 -0700133}