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