blob: d6d044efe7c0988a7774ebc8795d1c83de58911a [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Option;
19import org.apache.karaf.shell.api.action.Action;
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070020import 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;
Yi Tsengf18e66e2018-01-31 16:11:42 -080031import org.onosproject.net.DefaultAnnotations;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
Ray Milkey3078fc02015-05-06 16:14:14 -070034
Andrea Campanella13032532016-03-31 10:50:50 -070035import java.util.Set;
36import java.util.TreeSet;
37
tom0eb04ca2014-08-25 14:34:51 -070038/**
39 * Base abstraction of Karaf shell commands.
40 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041public abstract class AbstractShellCommand implements Action, CodecContext {
42 protected final Logger log = LoggerFactory.getLogger(this.getClass());
tom0eb04ca2014-08-25 14:34:51 -070043
tom32085cf2014-10-16 00:04:33 -070044 @Option(name = "-j", aliases = "--json", description = "Output JSON",
45 required = false, multiValued = false)
46 private boolean json = false;
47
tom0eb04ca2014-08-25 14:34:51 -070048 /**
tom6d2a43e2014-09-08 01:50:20 -070049 * Returns the reference to the implementation of the specified service.
tom0eb04ca2014-08-25 14:34:51 -070050 *
51 * @param serviceClass service class
52 * @param <T> type of service
53 * @return service implementation
tomcaf3bf72014-09-23 13:20:53 -070054 * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable
tom0eb04ca2014-08-25 14:34:51 -070055 */
tom6d2a43e2014-09-08 01:50:20 -070056 public static <T> T get(Class<T> serviceClass) {
tom0872a172014-09-23 11:24:26 -070057 return DefaultServiceDirectory.getService(serviceClass);
tom0eb04ca2014-08-25 14:34:51 -070058 }
59
tom6d2a43e2014-09-08 01:50:20 -070060 /**
Thomas Vachuskab97cf282014-10-20 23:31:12 -070061 * Returns application ID for the CLI.
62 *
63 * @return command-line application identifier
64 */
65 protected ApplicationId appId() {
Ray Milkey02479862015-02-17 17:02:19 -080066 return get(CoreService.class)
67 .registerApplication("org.onosproject.cli");
Thomas Vachuskab97cf282014-10-20 23:31:12 -070068 }
69
70 /**
tom6d2a43e2014-09-08 01:50:20 -070071 * Prints the arguments using the specified format.
72 *
73 * @param format format string; see {@link String#format}
74 * @param args arguments
75 */
tomcaf3bf72014-09-23 13:20:53 -070076 public void print(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +000077 System.out.println(String.format(format, args));
tom6d2a43e2014-09-08 01:50:20 -070078 }
79
tom9eb57fb2014-09-11 19:42:38 -070080 /**
81 * Prints the arguments using the specified format to error stream.
82 *
83 * @param format format string; see {@link String#format}
84 * @param args arguments
85 */
tomcaf3bf72014-09-23 13:20:53 -070086 public void error(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +000087 System.err.println(String.format(format, args));
tom9eb57fb2014-09-11 19:42:38 -070088 }
89
tom0872a172014-09-23 11:24:26 -070090 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070091 * Produces a string image of the specified key/value annotations.
92 *
93 * @param annotations key/value annotations
94 * @return string image with ", k1=v1, k2=v2, ..." pairs
95 */
96 public static String annotations(Annotations annotations) {
Yi Tsengf18e66e2018-01-31 16:11:42 -080097 if (annotations == null) {
98 annotations = DefaultAnnotations.EMPTY;
99 }
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700100 StringBuilder sb = new StringBuilder();
Laszlo Papp62c3e072018-01-18 12:40:21 +0000101 Set<String> keys = new TreeSet<>(annotations.keys());
102 for (String key : keys) {
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700103 sb.append(", ").append(key).append('=').append(annotations.value(key));
104 }
105 return sb.toString();
106 }
107
108 /**
Andrea Campanella13032532016-03-31 10:50:50 -0700109 * Produces a string image of the specified key/value annotations.
110 * Excludes the keys in the given Set.
111 *
112 * @param annotations key/value annotations
113 * @param excludedKeys keys not to add in the resulting string
114 * @return string image with ", k1=v1, k2=v2, ..." pairs
115 */
116 public static String annotations(Annotations annotations, Set<String> excludedKeys) {
117 StringBuilder sb = new StringBuilder();
118 Set<String> keys = new TreeSet<>(annotations.keys());
119 keys.removeAll(excludedKeys);
120 for (String key : keys) {
121 sb.append(", ").append(key).append('=').append(annotations.value(key));
122 }
123 return sb.toString();
124 }
125
126 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700127 * Produces a JSON object from the specified key/value annotations.
128 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800129 * @param mapper ObjectMapper to use while converting to JSON
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700130 * @param annotations key/value annotations
131 * @return JSON object
132 */
133 public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) {
134 ObjectNode result = mapper.createObjectNode();
135 for (String key : annotations.keys()) {
136 result.put(key, annotations.value(key));
137 }
138 return result;
139 }
140
141 /**
tom32085cf2014-10-16 00:04:33 -0700142 * Indicates whether JSON format should be output.
143 *
144 * @return true if JSON is requested
145 */
146 protected boolean outputJson() {
147 return json;
148 }
149
tom0872a172014-09-23 11:24:26 -0700150 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700151 public Object execute() throws Exception {
tom0872a172014-09-23 11:24:26 -0700152 try {
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700153 doExecute();
tom0872a172014-09-23 11:24:26 -0700154 } catch (ServiceNotFoundException e) {
155 error(e.getMessage());
156 }
157 return null;
158 }
159
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700160 protected void doExecute() throws Exception {
161 try {
162 execute();
163 } catch (ServiceNotFoundException e) {
164 error(e.getMessage());
165 }
166 }
Ray Milkey3078fc02015-05-06 16:14:14 -0700167
168 private final ObjectMapper mapper = new ObjectMapper();
169
170 @Override
171 public ObjectMapper mapper() {
172 return mapper;
173 }
174
175 @Override
176 @SuppressWarnings("unchecked")
177 public <T> JsonCodec<T> codec(Class<T> entityClass) {
178 return get(CodecService.class).getCodec(entityClass);
179 }
180
181 @Override
182 public <T> T getService(Class<T> serviceClass) {
183 return get(serviceClass);
184 }
185
186 /**
187 * Generates a Json representation of an object.
188 *
189 * @param entity object to generate JSON for
190 * @param entityClass class to format with - this chooses which codec to use
191 * @param <T> Type of the object being formatted
192 * @return JSON object representation
193 */
194 public <T> ObjectNode jsonForEntity(T entity, Class<T> entityClass) {
195 return codec(entityClass).encode(entity, this);
196 }
tom0eb04ca2014-08-25 14:34:51 -0700197}