Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Option; |
| 19 | import org.apache.karaf.shell.api.action.Action; |
Thomas Vachuska | a7a0f56 | 2015-04-14 23:27:44 -0700 | [diff] [blame] | 20 | import org.onlab.osgi.DefaultServiceDirectory; |
| 21 | import org.onlab.osgi.ServiceNotFoundException; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.CodecService; |
| 24 | import org.onosproject.codec.JsonCodec; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.Annotations; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 28 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 29 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 30 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Yi Tseng | f18e66e | 2018-01-31 16:11:42 -0800 | [diff] [blame] | 31 | import org.onosproject.net.DefaultAnnotations; |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 32 | import org.onosproject.security.AuditService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 34 | |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 36 | |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 37 | import java.util.Set; |
| 38 | import java.util.TreeSet; |
| 39 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Base abstraction of Karaf shell commands. |
| 42 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 43 | public abstract class AbstractShellCommand implements Action, CodecContext { |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 44 | |
| 45 | protected static final Logger log = getLogger(AbstractShellCommand.class); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 46 | |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 47 | private final ObjectMapper mapper = new ObjectMapper(); |
| 48 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 49 | @Option(name = "-j", aliases = "--json", description = "Output JSON", |
| 50 | required = false, multiValued = false) |
| 51 | private boolean json = false; |
| 52 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 53 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 54 | * Returns the reference to the implementation of the specified service. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 55 | * |
| 56 | * @param serviceClass service class |
| 57 | * @param <T> type of service |
| 58 | * @return service implementation |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 59 | * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 60 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 61 | public static <T> T get(Class<T> serviceClass) { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 62 | return DefaultServiceDirectory.getService(serviceClass); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 63 | } |
| 64 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 65 | /** |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 66 | * Returns application ID for the CLI. |
| 67 | * |
| 68 | * @return command-line application identifier |
| 69 | */ |
| 70 | protected ApplicationId appId() { |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 71 | return get(CoreService.class) |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 72 | .registerApplication("org.onosproject.cli"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 76 | * Prints the arguments using the specified format. |
| 77 | * |
| 78 | * @param format format string; see {@link String#format} |
| 79 | * @param args arguments |
| 80 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 81 | public void print(String format, Object... args) { |
Brian O'Connor | b28f5d7 | 2015-02-23 19:29:30 +0000 | [diff] [blame] | 82 | System.out.println(String.format(format, args)); |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 83 | } |
| 84 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 85 | /** |
| 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 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 91 | public void error(String format, Object... args) { |
Brian O'Connor | b28f5d7 | 2015-02-23 19:29:30 +0000 | [diff] [blame] | 92 | System.err.println(String.format(format, args)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 93 | } |
| 94 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 95 | /** |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 96 | * 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 Tseng | f18e66e | 2018-01-31 16:11:42 -0800 | [diff] [blame] | 102 | if (annotations == null) { |
| 103 | annotations = DefaultAnnotations.EMPTY; |
| 104 | } |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 105 | StringBuilder sb = new StringBuilder(); |
Laszlo Papp | 62c3e07 | 2018-01-18 12:40:21 +0000 | [diff] [blame] | 106 | Set<String> keys = new TreeSet<>(annotations.keys()); |
| 107 | for (String key : keys) { |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 108 | sb.append(", ").append(key).append('=').append(annotations.value(key)); |
| 109 | } |
| 110 | return sb.toString(); |
| 111 | } |
| 112 | |
| 113 | /** |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 114 | * 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 Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 132 | * Produces a JSON object from the specified key/value annotations. |
| 133 | * |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 134 | * @param mapper ObjectMapper to use while converting to JSON |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 135 | * @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 | /** |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 147 | * 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 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 155 | @Override |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 156 | public final Object execute() throws Exception { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 157 | try { |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 158 | auditCommand(); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 159 | doExecute(); |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 160 | } catch (ServiceNotFoundException e) { |
| 161 | error(e.getMessage()); |
| 162 | } |
| 163 | return null; |
| 164 | } |
| 165 | |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 166 | // 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 K | 53a2008 | 2018-12-18 05:13:05 -0500 | [diff] [blame] | 172 | String action = "{\"command\" : \"" + Thread.currentThread().getName().substring(5) + "\"}"; |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 173 | auditService.logUserAction(user, action); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 176 | |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 177 | /** |
| 178 | * Body of the shell command. |
| 179 | * |
| 180 | * @throws Exception thrown when problem is encountered |
| 181 | */ |
| 182 | protected abstract void doExecute() throws Exception; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 183 | |
| 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 | * |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 203 | * @param entity object to generate JSON for |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 204 | * @param entityClass class to format with - this chooses which codec to use |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 205 | * @param <T> Type of the object being formatted |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 206 | * @return JSON object representation |
| 207 | */ |
| 208 | public <T> ObjectNode jsonForEntity(T entity, Class<T> entityClass) { |
| 209 | return codec(entityClass).encode(entity, this); |
| 210 | } |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 211 | } |