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; |
| 33 | import org.slf4j.LoggerFactory; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 34 | |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 35 | import java.util.Set; |
| 36 | import java.util.TreeSet; |
| 37 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Base abstraction of Karaf shell commands. |
| 40 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | public abstract class AbstractShellCommand implements Action, CodecContext { |
| 42 | protected final Logger log = LoggerFactory.getLogger(this.getClass()); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 43 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 44 | @Option(name = "-j", aliases = "--json", description = "Output JSON", |
| 45 | required = false, multiValued = false) |
| 46 | private boolean json = false; |
| 47 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 48 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 49 | * Returns the reference to the implementation of the specified service. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 50 | * |
| 51 | * @param serviceClass service class |
| 52 | * @param <T> type of service |
| 53 | * @return service implementation |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 54 | * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 55 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 56 | public static <T> T get(Class<T> serviceClass) { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 57 | return DefaultServiceDirectory.getService(serviceClass); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 58 | } |
| 59 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 60 | /** |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 61 | * Returns application ID for the CLI. |
| 62 | * |
| 63 | * @return command-line application identifier |
| 64 | */ |
| 65 | protected ApplicationId appId() { |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 66 | return get(CoreService.class) |
| 67 | .registerApplication("org.onosproject.cli"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 71 | * Prints the arguments using the specified format. |
| 72 | * |
| 73 | * @param format format string; see {@link String#format} |
| 74 | * @param args arguments |
| 75 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 76 | public void print(String format, Object... args) { |
Brian O'Connor | b28f5d7 | 2015-02-23 19:29:30 +0000 | [diff] [blame] | 77 | System.out.println(String.format(format, args)); |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 78 | } |
| 79 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Prints the arguments using the specified format to error stream. |
| 82 | * |
| 83 | * @param format format string; see {@link String#format} |
| 84 | * @param args arguments |
| 85 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 86 | public void error(String format, Object... args) { |
Brian O'Connor | b28f5d7 | 2015-02-23 19:29:30 +0000 | [diff] [blame] | 87 | System.err.println(String.format(format, args)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 88 | } |
| 89 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 90 | /** |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 91 | * Produces a string image of the specified key/value annotations. |
| 92 | * |
| 93 | * @param annotations key/value annotations |
| 94 | * @return string image with ", k1=v1, k2=v2, ..." pairs |
| 95 | */ |
| 96 | public static String annotations(Annotations annotations) { |
Yi Tseng | f18e66e | 2018-01-31 16:11:42 -0800 | [diff] [blame] | 97 | if (annotations == null) { |
| 98 | annotations = DefaultAnnotations.EMPTY; |
| 99 | } |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 100 | StringBuilder sb = new StringBuilder(); |
Laszlo Papp | 62c3e07 | 2018-01-18 12:40:21 +0000 | [diff] [blame] | 101 | Set<String> keys = new TreeSet<>(annotations.keys()); |
| 102 | for (String key : keys) { |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 103 | sb.append(", ").append(key).append('=').append(annotations.value(key)); |
| 104 | } |
| 105 | return sb.toString(); |
| 106 | } |
| 107 | |
| 108 | /** |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 109 | * Produces a string image of the specified key/value annotations. |
| 110 | * Excludes the keys in the given Set. |
| 111 | * |
| 112 | * @param annotations key/value annotations |
| 113 | * @param excludedKeys keys not to add in the resulting string |
| 114 | * @return string image with ", k1=v1, k2=v2, ..." pairs |
| 115 | */ |
| 116 | public static String annotations(Annotations annotations, Set<String> excludedKeys) { |
| 117 | StringBuilder sb = new StringBuilder(); |
| 118 | Set<String> keys = new TreeSet<>(annotations.keys()); |
| 119 | keys.removeAll(excludedKeys); |
| 120 | for (String key : keys) { |
| 121 | sb.append(", ").append(key).append('=').append(annotations.value(key)); |
| 122 | } |
| 123 | return sb.toString(); |
| 124 | } |
| 125 | |
| 126 | /** |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 127 | * Produces a JSON object from the specified key/value annotations. |
| 128 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 129 | * @param mapper ObjectMapper to use while converting to JSON |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 130 | * @param annotations key/value annotations |
| 131 | * @return JSON object |
| 132 | */ |
| 133 | public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) { |
| 134 | ObjectNode result = mapper.createObjectNode(); |
| 135 | for (String key : annotations.keys()) { |
| 136 | result.put(key, annotations.value(key)); |
| 137 | } |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | /** |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 142 | * Indicates whether JSON format should be output. |
| 143 | * |
| 144 | * @return true if JSON is requested |
| 145 | */ |
| 146 | protected boolean outputJson() { |
| 147 | return json; |
| 148 | } |
| 149 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 150 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 151 | public Object execute() throws Exception { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 152 | try { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 153 | doExecute(); |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 154 | } catch (ServiceNotFoundException e) { |
| 155 | error(e.getMessage()); |
| 156 | } |
| 157 | return null; |
| 158 | } |
| 159 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 160 | protected void doExecute() throws Exception { |
| 161 | try { |
| 162 | execute(); |
| 163 | } catch (ServiceNotFoundException e) { |
| 164 | error(e.getMessage()); |
| 165 | } |
| 166 | } |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 167 | |
| 168 | private final ObjectMapper mapper = new ObjectMapper(); |
| 169 | |
| 170 | @Override |
| 171 | public ObjectMapper mapper() { |
| 172 | return mapper; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | @SuppressWarnings("unchecked") |
| 177 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 178 | return get(CodecService.class).getCodec(entityClass); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public <T> T getService(Class<T> serviceClass) { |
| 183 | return get(serviceClass); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Generates a Json representation of an object. |
| 188 | * |
| 189 | * @param entity object to generate JSON for |
| 190 | * @param entityClass class to format with - this chooses which codec to use |
| 191 | * @param <T> Type of the object being formatted |
| 192 | * @return JSON object representation |
| 193 | */ |
| 194 | public <T> ObjectNode jsonForEntity(T entity, Class<T> entityClass) { |
| 195 | return codec(entityClass).encode(entity, this); |
| 196 | } |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 197 | } |