blob: 09e08fa94287153551e500041939cda8c39c4c53 [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;
Yi Tsengf18e66e2018-01-31 16:11:42 -080031import org.onosproject.net.DefaultAnnotations;
Ray Milkey3078fc02015-05-06 16:14:14 -070032
Andrea Campanella13032532016-03-31 10:50:50 -070033import java.util.Set;
34import java.util.TreeSet;
35
tom0eb04ca2014-08-25 14:34:51 -070036/**
37 * Base abstraction of Karaf shell commands.
38 */
Ray Milkey3078fc02015-05-06 16:14:14 -070039public abstract class AbstractShellCommand extends AbstractAction implements CodecContext {
tom0eb04ca2014-08-25 14:34:51 -070040
tom32085cf2014-10-16 00:04:33 -070041 @Option(name = "-j", aliases = "--json", description = "Output JSON",
42 required = false, multiValued = false)
43 private boolean json = false;
44
tom0eb04ca2014-08-25 14:34:51 -070045 /**
tom6d2a43e2014-09-08 01:50:20 -070046 * Returns the reference to the implementation of the specified service.
tom0eb04ca2014-08-25 14:34:51 -070047 *
48 * @param serviceClass service class
49 * @param <T> type of service
50 * @return service implementation
tomcaf3bf72014-09-23 13:20:53 -070051 * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable
tom0eb04ca2014-08-25 14:34:51 -070052 */
tom6d2a43e2014-09-08 01:50:20 -070053 public static <T> T get(Class<T> serviceClass) {
tom0872a172014-09-23 11:24:26 -070054 return DefaultServiceDirectory.getService(serviceClass);
tom0eb04ca2014-08-25 14:34:51 -070055 }
56
tom6d2a43e2014-09-08 01:50:20 -070057 /**
Thomas Vachuskab97cf282014-10-20 23:31:12 -070058 * Returns application ID for the CLI.
59 *
60 * @return command-line application identifier
61 */
62 protected ApplicationId appId() {
Ray Milkey02479862015-02-17 17:02:19 -080063 return get(CoreService.class)
64 .registerApplication("org.onosproject.cli");
Thomas Vachuskab97cf282014-10-20 23:31:12 -070065 }
66
67 /**
tom6d2a43e2014-09-08 01:50:20 -070068 * Prints the arguments using the specified format.
69 *
70 * @param format format string; see {@link String#format}
71 * @param args arguments
72 */
tomcaf3bf72014-09-23 13:20:53 -070073 public void print(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +000074 System.out.println(String.format(format, args));
tom6d2a43e2014-09-08 01:50:20 -070075 }
76
tom9eb57fb2014-09-11 19:42:38 -070077 /**
78 * Prints the arguments using the specified format to error stream.
79 *
80 * @param format format string; see {@link String#format}
81 * @param args arguments
82 */
tomcaf3bf72014-09-23 13:20:53 -070083 public void error(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +000084 System.err.println(String.format(format, args));
tom9eb57fb2014-09-11 19:42:38 -070085 }
86
tom0872a172014-09-23 11:24:26 -070087 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070088 * Produces a string image of the specified key/value annotations.
89 *
90 * @param annotations key/value annotations
91 * @return string image with ", k1=v1, k2=v2, ..." pairs
92 */
93 public static String annotations(Annotations annotations) {
Yi Tsengf18e66e2018-01-31 16:11:42 -080094 if (annotations == null) {
95 annotations = DefaultAnnotations.EMPTY;
96 }
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070097 StringBuilder sb = new StringBuilder();
Laszlo Papp62c3e072018-01-18 12:40:21 +000098 Set<String> keys = new TreeSet<>(annotations.keys());
99 for (String key : keys) {
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700100 sb.append(", ").append(key).append('=').append(annotations.value(key));
101 }
102 return sb.toString();
103 }
104
105 /**
Andrea Campanella13032532016-03-31 10:50:50 -0700106 * Produces a string image of the specified key/value annotations.
107 * Excludes the keys in the given Set.
108 *
109 * @param annotations key/value annotations
110 * @param excludedKeys keys not to add in the resulting string
111 * @return string image with ", k1=v1, k2=v2, ..." pairs
112 */
113 public static String annotations(Annotations annotations, Set<String> excludedKeys) {
114 StringBuilder sb = new StringBuilder();
115 Set<String> keys = new TreeSet<>(annotations.keys());
116 keys.removeAll(excludedKeys);
117 for (String key : keys) {
118 sb.append(", ").append(key).append('=').append(annotations.value(key));
119 }
120 return sb.toString();
121 }
122
123 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700124 * Produces a JSON object from the specified key/value annotations.
125 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800126 * @param mapper ObjectMapper to use while converting to JSON
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700127 * @param annotations key/value annotations
128 * @return JSON object
129 */
130 public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) {
131 ObjectNode result = mapper.createObjectNode();
132 for (String key : annotations.keys()) {
133 result.put(key, annotations.value(key));
134 }
135 return result;
136 }
137
138 /**
tom0872a172014-09-23 11:24:26 -0700139 * Executes this command.
140 */
141 protected abstract void execute();
142
tom32085cf2014-10-16 00:04:33 -0700143 /**
144 * Indicates whether JSON format should be output.
145 *
146 * @return true if JSON is requested
147 */
148 protected boolean outputJson() {
149 return json;
150 }
151
tom0872a172014-09-23 11:24:26 -0700152 @Override
153 protected Object doExecute() throws Exception {
154 try {
155 execute();
156 } catch (ServiceNotFoundException e) {
157 error(e.getMessage());
158 }
159 return null;
160 }
161
Ray Milkey3078fc02015-05-06 16:14:14 -0700162
163
164 private final ObjectMapper mapper = new ObjectMapper();
165
166 @Override
167 public ObjectMapper mapper() {
168 return mapper;
169 }
170
171 @Override
172 @SuppressWarnings("unchecked")
173 public <T> JsonCodec<T> codec(Class<T> entityClass) {
174 return get(CodecService.class).getCodec(entityClass);
175 }
176
177 @Override
178 public <T> T getService(Class<T> serviceClass) {
179 return get(serviceClass);
180 }
181
182 /**
183 * Generates a Json representation of an object.
184 *
185 * @param entity object to generate JSON for
186 * @param entityClass class to format with - this chooses which codec to use
187 * @param <T> Type of the object being formatted
188 * @return JSON object representation
189 */
190 public <T> ObjectNode jsonForEntity(T entity, Class<T> entityClass) {
191 return codec(entityClass).encode(entity, this);
192 }
tom0eb04ca2014-08-25 14:34:51 -0700193}