blob: 494d7d8d6edcbb436bea1e24a6de7a1b8833ac1b [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;
arjunek1992f6353d42018-11-20 08:56:29 -050033
34import static org.slf4j.LoggerFactory.getLogger;
Ray Milkey3078fc02015-05-06 16:14:14 -070035
Andrea Campanella13032532016-03-31 10:50:50 -070036import java.util.Set;
37import java.util.TreeSet;
38
tom0eb04ca2014-08-25 14:34:51 -070039/**
40 * Base abstraction of Karaf shell commands.
41 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042public abstract class AbstractShellCommand implements Action, CodecContext {
arjunek1992f6353d42018-11-20 08:56:29 -050043
44 protected static final Logger log = getLogger(AbstractShellCommand.class);
tom0eb04ca2014-08-25 14:34:51 -070045
tom32085cf2014-10-16 00:04:33 -070046 @Option(name = "-j", aliases = "--json", description = "Output JSON",
47 required = false, multiValued = false)
48 private boolean json = false;
49
arjunek1992f6353d42018-11-20 08:56:29 -050050 private static String auditFile = "all";
51 private static boolean auditEnabled = false;
52
53 /**
54 * To check if CLI Audit is enabled.
55 *
56 * @return true if the CLI Audit is enabled.
57 */
58 private static boolean isEnabled() {
59 return auditEnabled;
60 }
61
62 /**
63 * To enable CLI Audit.
64 */
65 public static void enableAudit() {
66 auditEnabled = true;
67 }
68
69 /**
70 * To disable CLI Audit.
71 */
72 public static void disableAudit() {
73 auditEnabled = false;
74 }
75
76 /**
77 * To set audit file type which CLI Audit logs must be saved.
78 *
79 * @param auditFile file that CLI Audit logs must be saved.
80 */
81 public static void setAuditFile(String auditFile) {
82 AbstractShellCommand.auditFile = auditFile;
83 }
84
85 /**
86 * To save audit logs into the log file.
87 *
88 * @param msg audit message.
89 */
90 private static void saveAuditLog(String msg) {
91 if (isEnabled()) {
92 if (auditFile.equals("all")) {
93 log.info(msg);
94 log.info("AuditLog : " + msg);
95 } else if (auditFile.equals("karaf")) {
96 log.info(msg);
97 } else if (auditFile.equals("audit")) {
98 log.info("AuditLog : " + msg);
99 }
100 }
101 }
102
tom0eb04ca2014-08-25 14:34:51 -0700103 /**
tom6d2a43e2014-09-08 01:50:20 -0700104 * Returns the reference to the implementation of the specified service.
tom0eb04ca2014-08-25 14:34:51 -0700105 *
106 * @param serviceClass service class
107 * @param <T> type of service
108 * @return service implementation
tomcaf3bf72014-09-23 13:20:53 -0700109 * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable
tom0eb04ca2014-08-25 14:34:51 -0700110 */
tom6d2a43e2014-09-08 01:50:20 -0700111 public static <T> T get(Class<T> serviceClass) {
arjunek1992f6353d42018-11-20 08:56:29 -0500112 saveAuditLog("Audit ");
tom0872a172014-09-23 11:24:26 -0700113 return DefaultServiceDirectory.getService(serviceClass);
tom0eb04ca2014-08-25 14:34:51 -0700114 }
115
tom6d2a43e2014-09-08 01:50:20 -0700116 /**
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700117 * Returns application ID for the CLI.
118 *
119 * @return command-line application identifier
120 */
121 protected ApplicationId appId() {
Ray Milkey02479862015-02-17 17:02:19 -0800122 return get(CoreService.class)
arjunek1992f6353d42018-11-20 08:56:29 -0500123 .registerApplication("org.onosproject.cli");
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700124 }
125
126 /**
tom6d2a43e2014-09-08 01:50:20 -0700127 * Prints the arguments using the specified format.
128 *
129 * @param format format string; see {@link String#format}
130 * @param args arguments
131 */
tomcaf3bf72014-09-23 13:20:53 -0700132 public void print(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +0000133 System.out.println(String.format(format, args));
tom6d2a43e2014-09-08 01:50:20 -0700134 }
135
tom9eb57fb2014-09-11 19:42:38 -0700136 /**
137 * Prints the arguments using the specified format to error stream.
138 *
139 * @param format format string; see {@link String#format}
140 * @param args arguments
141 */
tomcaf3bf72014-09-23 13:20:53 -0700142 public void error(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +0000143 System.err.println(String.format(format, args));
tom9eb57fb2014-09-11 19:42:38 -0700144 }
145
tom0872a172014-09-23 11:24:26 -0700146 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700147 * Produces a string image of the specified key/value annotations.
148 *
149 * @param annotations key/value annotations
150 * @return string image with ", k1=v1, k2=v2, ..." pairs
151 */
152 public static String annotations(Annotations annotations) {
Yi Tsengf18e66e2018-01-31 16:11:42 -0800153 if (annotations == null) {
154 annotations = DefaultAnnotations.EMPTY;
155 }
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700156 StringBuilder sb = new StringBuilder();
Laszlo Papp62c3e072018-01-18 12:40:21 +0000157 Set<String> keys = new TreeSet<>(annotations.keys());
158 for (String key : keys) {
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700159 sb.append(", ").append(key).append('=').append(annotations.value(key));
160 }
161 return sb.toString();
162 }
163
164 /**
Andrea Campanella13032532016-03-31 10:50:50 -0700165 * Produces a string image of the specified key/value annotations.
166 * Excludes the keys in the given Set.
167 *
168 * @param annotations key/value annotations
169 * @param excludedKeys keys not to add in the resulting string
170 * @return string image with ", k1=v1, k2=v2, ..." pairs
171 */
172 public static String annotations(Annotations annotations, Set<String> excludedKeys) {
173 StringBuilder sb = new StringBuilder();
174 Set<String> keys = new TreeSet<>(annotations.keys());
175 keys.removeAll(excludedKeys);
176 for (String key : keys) {
177 sb.append(", ").append(key).append('=').append(annotations.value(key));
178 }
179 return sb.toString();
180 }
181
182 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700183 * Produces a JSON object from the specified key/value annotations.
184 *
arjunek1992f6353d42018-11-20 08:56:29 -0500185 * @param mapper ObjectMapper to use while converting to JSON
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700186 * @param annotations key/value annotations
187 * @return JSON object
188 */
189 public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) {
190 ObjectNode result = mapper.createObjectNode();
191 for (String key : annotations.keys()) {
192 result.put(key, annotations.value(key));
193 }
194 return result;
195 }
196
197 /**
tom32085cf2014-10-16 00:04:33 -0700198 * Indicates whether JSON format should be output.
199 *
200 * @return true if JSON is requested
201 */
202 protected boolean outputJson() {
203 return json;
204 }
205
tom0872a172014-09-23 11:24:26 -0700206 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700207 public Object execute() throws Exception {
tom0872a172014-09-23 11:24:26 -0700208 try {
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700209 doExecute();
tom0872a172014-09-23 11:24:26 -0700210 } catch (ServiceNotFoundException e) {
211 error(e.getMessage());
212 }
213 return null;
214 }
215
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700216 protected void doExecute() throws Exception {
217 try {
218 execute();
219 } catch (ServiceNotFoundException e) {
220 error(e.getMessage());
221 }
222 }
Ray Milkey3078fc02015-05-06 16:14:14 -0700223
224 private final ObjectMapper mapper = new ObjectMapper();
225
226 @Override
227 public ObjectMapper mapper() {
228 return mapper;
229 }
230
231 @Override
232 @SuppressWarnings("unchecked")
233 public <T> JsonCodec<T> codec(Class<T> entityClass) {
234 return get(CodecService.class).getCodec(entityClass);
235 }
236
237 @Override
238 public <T> T getService(Class<T> serviceClass) {
239 return get(serviceClass);
240 }
241
242 /**
243 * Generates a Json representation of an object.
244 *
arjunek1992f6353d42018-11-20 08:56:29 -0500245 * @param entity object to generate JSON for
Ray Milkey3078fc02015-05-06 16:14:14 -0700246 * @param entityClass class to format with - this chooses which codec to use
arjunek1992f6353d42018-11-20 08:56:29 -0500247 * @param <T> Type of the object being formatted
Ray Milkey3078fc02015-05-06 16:14:14 -0700248 * @return JSON object representation
249 */
250 public <T> ObjectNode jsonForEntity(T entity, Class<T> entityClass) {
251 return codec(entityClass).encode(entity, this);
252 }
tom0eb04ca2014-08-25 14:34:51 -0700253}