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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 33 | |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 35 | |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 36 | import java.util.Set; |
| 37 | import java.util.TreeSet; |
| 38 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Base abstraction of Karaf shell commands. |
| 41 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | public abstract class AbstractShellCommand implements Action, CodecContext { |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 43 | |
| 44 | protected static final Logger log = getLogger(AbstractShellCommand.class); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 45 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 46 | @Option(name = "-j", aliases = "--json", description = "Output JSON", |
| 47 | required = false, multiValued = false) |
| 48 | private boolean json = false; |
| 49 | |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 50 | 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 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 103 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 104 | * Returns the reference to the implementation of the specified service. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 105 | * |
| 106 | * @param serviceClass service class |
| 107 | * @param <T> type of service |
| 108 | * @return service implementation |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 109 | * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 110 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 111 | public static <T> T get(Class<T> serviceClass) { |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 112 | saveAuditLog("Audit "); |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 113 | return DefaultServiceDirectory.getService(serviceClass); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 114 | } |
| 115 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 116 | /** |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 117 | * Returns application ID for the CLI. |
| 118 | * |
| 119 | * @return command-line application identifier |
| 120 | */ |
| 121 | protected ApplicationId appId() { |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 122 | return get(CoreService.class) |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 123 | .registerApplication("org.onosproject.cli"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 127 | * Prints the arguments using the specified format. |
| 128 | * |
| 129 | * @param format format string; see {@link String#format} |
| 130 | * @param args arguments |
| 131 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 132 | public void print(String format, Object... args) { |
Brian O'Connor | b28f5d7 | 2015-02-23 19:29:30 +0000 | [diff] [blame] | 133 | System.out.println(String.format(format, args)); |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 134 | } |
| 135 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 136 | /** |
| 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 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 142 | public void error(String format, Object... args) { |
Brian O'Connor | b28f5d7 | 2015-02-23 19:29:30 +0000 | [diff] [blame] | 143 | System.err.println(String.format(format, args)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 144 | } |
| 145 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 146 | /** |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 147 | * 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 Tseng | f18e66e | 2018-01-31 16:11:42 -0800 | [diff] [blame] | 153 | if (annotations == null) { |
| 154 | annotations = DefaultAnnotations.EMPTY; |
| 155 | } |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 156 | StringBuilder sb = new StringBuilder(); |
Laszlo Papp | 62c3e07 | 2018-01-18 12:40:21 +0000 | [diff] [blame] | 157 | Set<String> keys = new TreeSet<>(annotations.keys()); |
| 158 | for (String key : keys) { |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 159 | sb.append(", ").append(key).append('=').append(annotations.value(key)); |
| 160 | } |
| 161 | return sb.toString(); |
| 162 | } |
| 163 | |
| 164 | /** |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 165 | * 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 Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 183 | * Produces a JSON object from the specified key/value annotations. |
| 184 | * |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 185 | * @param mapper ObjectMapper to use while converting to JSON |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 186 | * @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 | /** |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 198 | * 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 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 206 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 207 | public Object execute() throws Exception { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 208 | try { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 209 | doExecute(); |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 210 | } catch (ServiceNotFoundException e) { |
| 211 | error(e.getMessage()); |
| 212 | } |
| 213 | return null; |
| 214 | } |
| 215 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 216 | protected void doExecute() throws Exception { |
| 217 | try { |
| 218 | execute(); |
| 219 | } catch (ServiceNotFoundException e) { |
| 220 | error(e.getMessage()); |
| 221 | } |
| 222 | } |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 223 | |
| 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 | * |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 245 | * @param entity object to generate JSON for |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 246 | * @param entityClass class to format with - this chooses which codec to use |
arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame] | 247 | * @param <T> Type of the object being formatted |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 248 | * @return JSON object representation |
| 249 | */ |
| 250 | public <T> ObjectNode jsonForEntity(T entity, Class<T> entityClass) { |
| 251 | return codec(entityClass).encode(entity, this); |
| 252 | } |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 253 | } |