blob: 3e6d803d9a74e06ec95169d9f4b1b64bb2c7cf10 [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;
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -080032import org.onosproject.security.AuditService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033import org.slf4j.Logger;
arjunek1992f6353d42018-11-20 08:56:29 -050034
35import static org.slf4j.LoggerFactory.getLogger;
Ray Milkey3078fc02015-05-06 16:14:14 -070036
Andrea Campanella13032532016-03-31 10:50:50 -070037import java.util.Set;
38import java.util.TreeSet;
39
tom0eb04ca2014-08-25 14:34:51 -070040/**
41 * Base abstraction of Karaf shell commands.
42 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070043public abstract class AbstractShellCommand implements Action, CodecContext {
arjunek1992f6353d42018-11-20 08:56:29 -050044
45 protected static final Logger log = getLogger(AbstractShellCommand.class);
tom0eb04ca2014-08-25 14:34:51 -070046
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -080047 private final ObjectMapper mapper = new ObjectMapper();
48
tom32085cf2014-10-16 00:04:33 -070049 @Option(name = "-j", aliases = "--json", description = "Output JSON",
50 required = false, multiValued = false)
51 private boolean json = false;
52
tom0eb04ca2014-08-25 14:34:51 -070053 /**
tom6d2a43e2014-09-08 01:50:20 -070054 * Returns the reference to the implementation of the specified service.
tom0eb04ca2014-08-25 14:34:51 -070055 *
56 * @param serviceClass service class
57 * @param <T> type of service
58 * @return service implementation
tomcaf3bf72014-09-23 13:20:53 -070059 * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable
tom0eb04ca2014-08-25 14:34:51 -070060 */
tom6d2a43e2014-09-08 01:50:20 -070061 public static <T> T get(Class<T> serviceClass) {
tom0872a172014-09-23 11:24:26 -070062 return DefaultServiceDirectory.getService(serviceClass);
tom0eb04ca2014-08-25 14:34:51 -070063 }
64
tom6d2a43e2014-09-08 01:50:20 -070065 /**
Thomas Vachuskab97cf282014-10-20 23:31:12 -070066 * Returns application ID for the CLI.
67 *
68 * @return command-line application identifier
69 */
70 protected ApplicationId appId() {
Ray Milkey02479862015-02-17 17:02:19 -080071 return get(CoreService.class)
arjunek1992f6353d42018-11-20 08:56:29 -050072 .registerApplication("org.onosproject.cli");
Thomas Vachuskab97cf282014-10-20 23:31:12 -070073 }
74
75 /**
tom6d2a43e2014-09-08 01:50:20 -070076 * Prints the arguments using the specified format.
77 *
78 * @param format format string; see {@link String#format}
79 * @param args arguments
80 */
tomcaf3bf72014-09-23 13:20:53 -070081 public void print(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +000082 System.out.println(String.format(format, args));
tom6d2a43e2014-09-08 01:50:20 -070083 }
84
tom9eb57fb2014-09-11 19:42:38 -070085 /**
86 * Prints the arguments using the specified format to error stream.
87 *
88 * @param format format string; see {@link String#format}
89 * @param args arguments
90 */
tomcaf3bf72014-09-23 13:20:53 -070091 public void error(String format, Object... args) {
Brian O'Connorb28f5d72015-02-23 19:29:30 +000092 System.err.println(String.format(format, args));
tom9eb57fb2014-09-11 19:42:38 -070093 }
94
tom0872a172014-09-23 11:24:26 -070095 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070096 * Produces a string image of the specified key/value annotations.
97 *
98 * @param annotations key/value annotations
99 * @return string image with ", k1=v1, k2=v2, ..." pairs
100 */
101 public static String annotations(Annotations annotations) {
Yi Tsengf18e66e2018-01-31 16:11:42 -0800102 if (annotations == null) {
103 annotations = DefaultAnnotations.EMPTY;
104 }
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700105 StringBuilder sb = new StringBuilder();
Laszlo Papp62c3e072018-01-18 12:40:21 +0000106 Set<String> keys = new TreeSet<>(annotations.keys());
107 for (String key : keys) {
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700108 sb.append(", ").append(key).append('=').append(annotations.value(key));
109 }
110 return sb.toString();
111 }
112
113 /**
Andrea Campanella13032532016-03-31 10:50:50 -0700114 * Produces a string image of the specified key/value annotations.
115 * Excludes the keys in the given Set.
116 *
117 * @param annotations key/value annotations
118 * @param excludedKeys keys not to add in the resulting string
119 * @return string image with ", k1=v1, k2=v2, ..." pairs
120 */
121 public static String annotations(Annotations annotations, Set<String> excludedKeys) {
122 StringBuilder sb = new StringBuilder();
123 Set<String> keys = new TreeSet<>(annotations.keys());
124 keys.removeAll(excludedKeys);
125 for (String key : keys) {
126 sb.append(", ").append(key).append('=').append(annotations.value(key));
127 }
128 return sb.toString();
129 }
130
131 /**
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700132 * Produces a JSON object from the specified key/value annotations.
133 *
arjunek1992f6353d42018-11-20 08:56:29 -0500134 * @param mapper ObjectMapper to use while converting to JSON
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700135 * @param annotations key/value annotations
136 * @return JSON object
137 */
138 public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) {
139 ObjectNode result = mapper.createObjectNode();
140 for (String key : annotations.keys()) {
141 result.put(key, annotations.value(key));
142 }
143 return result;
144 }
145
146 /**
tom32085cf2014-10-16 00:04:33 -0700147 * Indicates whether JSON format should be output.
148 *
149 * @return true if JSON is requested
150 */
151 protected boolean outputJson() {
152 return json;
153 }
154
tom0872a172014-09-23 11:24:26 -0700155 @Override
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -0800156 public final Object execute() throws Exception {
tom0872a172014-09-23 11:24:26 -0700157 try {
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -0800158 auditCommand();
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700159 doExecute();
tom0872a172014-09-23 11:24:26 -0700160 } catch (ServiceNotFoundException e) {
161 error(e.getMessage());
162 }
163 return null;
164 }
165
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -0800166 // Handles auditing
167 private void auditCommand() {
168 AuditService auditService = get(AuditService.class);
169 if (auditService != null && auditService.isAuditing()) {
170 // FIXME: Compose and log audit message here; this is a hack
171 String user = "foo"; // FIXME
Arjun E K53a20082018-12-18 05:13:05 -0500172 String action = "{\"command\" : \"" + Thread.currentThread().getName().substring(5) + "\"}";
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -0800173 auditService.logUserAction(user, action);
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700174 }
175 }
Ray Milkey3078fc02015-05-06 16:14:14 -0700176
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -0800177 /**
178 * Body of the shell command.
179 *
180 * @throws Exception thrown when problem is encountered
181 */
182 protected abstract void doExecute() throws Exception;
Ray Milkey3078fc02015-05-06 16:14:14 -0700183
184 @Override
185 public ObjectMapper mapper() {
186 return mapper;
187 }
188
189 @Override
190 @SuppressWarnings("unchecked")
191 public <T> JsonCodec<T> codec(Class<T> entityClass) {
192 return get(CodecService.class).getCodec(entityClass);
193 }
194
195 @Override
196 public <T> T getService(Class<T> serviceClass) {
197 return get(serviceClass);
198 }
199
200 /**
201 * Generates a Json representation of an object.
202 *
arjunek1992f6353d42018-11-20 08:56:29 -0500203 * @param entity object to generate JSON for
Ray Milkey3078fc02015-05-06 16:14:14 -0700204 * @param entityClass class to format with - this chooses which codec to use
arjunek1992f6353d42018-11-20 08:56:29 -0500205 * @param <T> Type of the object being formatted
Ray Milkey3078fc02015-05-06 16:14:14 -0700206 * @return JSON object representation
207 */
208 public <T> ObjectNode jsonForEntity(T entity, Class<T> entityClass) {
209 return codec(entityClass).encode(entity, this);
210 }
tom0eb04ca2014-08-25 14:34:51 -0700211}