blob: f83c5fad6eb00cf179c5d52371aaa9e9570a4334 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
tom32085cf2014-10-16 00:04:33 -070018import org.apache.karaf.shell.commands.Option;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070019import org.apache.karaf.shell.console.AbstractAction;
20import org.onlab.osgi.DefaultServiceDirectory;
21import org.onlab.osgi.ServiceNotFoundException;
Ray Milkey3078fc02015-05-06 16:14:14 -070022import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.CodecService;
24import org.onosproject.codec.JsonCodec;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.net.Annotations;
tom0eb04ca2014-08-25 14:34:51 -070028
Ray Milkey3078fc02015-05-06 16:14:14 -070029import com.fasterxml.jackson.databind.ObjectMapper;
30import com.fasterxml.jackson.databind.node.ObjectNode;
31
Andrea Campanella13032532016-03-31 10:50:50 -070032import java.util.Set;
33import java.util.TreeSet;
34
tom0eb04ca2014-08-25 14:34:51 -070035/**
36 * Base abstraction of Karaf shell commands.
37 */
Ray Milkey3078fc02015-05-06 16:14:14 -070038public abstract class AbstractShellCommand extends AbstractAction implements CodecContext {
tom0eb04ca2014-08-25 14:34:51 -070039
tom32085cf2014-10-16 00:04:33 -070040 @Option(name = "-j", aliases = "--json", description = "Output JSON",
41 required = false, multiValued = false)
42 private boolean json = false;
43
tom0eb04ca2014-08-25 14:34:51 -070044 /**
tom6d2a43e2014-09-08 01:50:20 -070045 * Returns the reference to the implementation of the specified service.
tom0eb04ca2014-08-25 14:34:51 -070046 *
47 * @param serviceClass service class
48 * @param <T> type of service
49 * @return service implementation
tomcaf3bf72014-09-23 13:20:53 -070050 * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable
tom0eb04ca2014-08-25 14:34:51 -070051 */
tom6d2a43e2014-09-08 01:50:20 -070052 public static <T> T get(Class<T> serviceClass) {
tom0872a172014-09-23 11:24:26 -070053 return DefaultServiceDirectory.getService(serviceClass);
tom0eb04ca2014-08-25 14:34:51 -070054 }
55
tom6d2a43e2014-09-08 01:50:20 -070056 /**
Thomas Vachuskab97cf282014-10-20 23:31:12 -070057 * Returns application ID for the CLI.
58 *
59 * @return command-line application identifier
60 */
61 protected ApplicationId appId() {
Ray Milkey02479862015-02-17 17:02:19 -080062 return get(CoreService.class)
63 .registerApplication("org.onosproject.cli");
Thomas Vachuskab97cf282014-10-20 23:31:12 -070064 }
65
66 /**
tom6d2a43e2014-09-08 01:50:20 -070067 * Prints the arguments using the specified format.
68 *
69 * @param format format string; see {@link String#format}
70 * @param args arguments
71 */
tomcaf3bf72014-09-23 13:20:53 -070072 public void print(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +000073 System.out.println(String.format(format, args));
tom6d2a43e2014-09-08 01:50:20 -070074 }
75
tom9eb57fb2014-09-11 19:42:38 -070076 /**
77 * Prints the arguments using the specified format to error stream.
78 *
79 * @param format format string; see {@link String#format}
80 * @param args arguments
81 */
tomcaf3bf72014-09-23 13:20:53 -070082 public void error(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +000083 System.err.println(String.format(format, args));
tom9eb57fb2014-09-11 19:42:38 -070084 }
85
tom0872a172014-09-23 11:24:26 -070086 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070087 * Produces a string image of the specified key/value annotations.
88 *
89 * @param annotations key/value annotations
90 * @return string image with ", k1=v1, k2=v2, ..." pairs
91 */
92 public static String annotations(Annotations annotations) {
93 StringBuilder sb = new StringBuilder();
Laszlo Papp62c3e072018-01-18 12:40:21 +000094 Set<String> keys = new TreeSet<>(annotations.keys());
95 for (String key : keys) {
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070096 sb.append(", ").append(key).append('=').append(annotations.value(key));
97 }
98 return sb.toString();
99 }
100
101 /**
Andrea Campanella13032532016-03-31 10:50:50 -0700102 * Produces a string image of the specified key/value annotations.
103 * Excludes the keys in the given Set.
104 *
105 * @param annotations key/value annotations
106 * @param excludedKeys keys not to add in the resulting string
107 * @return string image with ", k1=v1, k2=v2, ..." pairs
108 */
109 public static String annotations(Annotations annotations, Set<String> excludedKeys) {
110 StringBuilder sb = new StringBuilder();
111 Set<String> keys = new TreeSet<>(annotations.keys());
112 keys.removeAll(excludedKeys);
113 for (String key : keys) {
114 sb.append(", ").append(key).append('=').append(annotations.value(key));
115 }
116 return sb.toString();
117 }
118
119 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700120 * Produces a JSON object from the specified key/value annotations.
121 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800122 * @param mapper ObjectMapper to use while converting to JSON
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700123 * @param annotations key/value annotations
124 * @return JSON object
125 */
126 public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) {
127 ObjectNode result = mapper.createObjectNode();
128 for (String key : annotations.keys()) {
129 result.put(key, annotations.value(key));
130 }
131 return result;
132 }
133
134 /**
tom0872a172014-09-23 11:24:26 -0700135 * Executes this command.
136 */
137 protected abstract void execute();
138
tom32085cf2014-10-16 00:04:33 -0700139 /**
140 * Indicates whether JSON format should be output.
141 *
142 * @return true if JSON is requested
143 */
144 protected boolean outputJson() {
145 return json;
146 }
147
tom0872a172014-09-23 11:24:26 -0700148 @Override
149 protected Object doExecute() throws Exception {
150 try {
151 execute();
152 } catch (ServiceNotFoundException e) {
153 error(e.getMessage());
154 }
155 return null;
156 }
157
Ray Milkey3078fc02015-05-06 16:14:14 -0700158
159
160 private final ObjectMapper mapper = new ObjectMapper();
161
162 @Override
163 public ObjectMapper mapper() {
164 return mapper;
165 }
166
167 @Override
168 @SuppressWarnings("unchecked")
169 public <T> JsonCodec<T> codec(Class<T> entityClass) {
170 return get(CodecService.class).getCodec(entityClass);
171 }
172
173 @Override
174 public <T> T getService(Class<T> serviceClass) {
175 return get(serviceClass);
176 }
177
178 /**
179 * Generates a Json representation of an object.
180 *
181 * @param entity object to generate JSON for
182 * @param entityClass class to format with - this chooses which codec to use
183 * @param <T> Type of the object being formatted
184 * @return JSON object representation
185 */
186 public <T> ObjectNode jsonForEntity(T entity, Class<T> entityClass) {
187 return codec(entityClass).encode(entity, this);
188 }
tom0eb04ca2014-08-25 14:34:51 -0700189}