Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | */ |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli; |
| 17 | |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Option; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.console.OsgiCommandSupport; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 22 | import org.onlab.onos.core.ApplicationId; |
| 23 | import org.onlab.onos.core.CoreService; |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.Annotations; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 25 | import org.onlab.osgi.DefaultServiceDirectory; |
| 26 | import org.onlab.osgi.ServiceNotFoundException; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Base abstraction of Karaf shell commands. |
| 30 | */ |
| 31 | public abstract class AbstractShellCommand extends OsgiCommandSupport { |
| 32 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 33 | @Option(name = "-j", aliases = "--json", description = "Output JSON", |
| 34 | required = false, multiValued = false) |
| 35 | private boolean json = false; |
| 36 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 37 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 38 | * Returns the reference to the implementation of the specified service. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 39 | * |
| 40 | * @param serviceClass service class |
| 41 | * @param <T> type of service |
| 42 | * @return service implementation |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 43 | * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 44 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 45 | public static <T> T get(Class<T> serviceClass) { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 46 | return DefaultServiceDirectory.getService(serviceClass); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 47 | } |
| 48 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 49 | /** |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 50 | * Returns application ID for the CLI. |
| 51 | * |
| 52 | * @return command-line application identifier |
| 53 | */ |
| 54 | protected ApplicationId appId() { |
| 55 | return get(CoreService.class).registerApplication("org.onlab.onos.cli"); |
| 56 | } |
| 57 | |
| 58 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 59 | * Prints the arguments using the specified format. |
| 60 | * |
| 61 | * @param format format string; see {@link String#format} |
| 62 | * @param args arguments |
| 63 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 64 | public void print(String format, Object... args) { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 65 | System.out.println(String.format(format, args)); |
| 66 | } |
| 67 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Prints the arguments using the specified format to error stream. |
| 70 | * |
| 71 | * @param format format string; see {@link String#format} |
| 72 | * @param args arguments |
| 73 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 74 | public void error(String format, Object... args) { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 75 | System.err.println(String.format(format, args)); |
| 76 | } |
| 77 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 78 | /** |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 79 | * Produces a string image of the specified key/value annotations. |
| 80 | * |
| 81 | * @param annotations key/value annotations |
| 82 | * @return string image with ", k1=v1, k2=v2, ..." pairs |
| 83 | */ |
| 84 | public static String annotations(Annotations annotations) { |
| 85 | StringBuilder sb = new StringBuilder(); |
| 86 | for (String key : annotations.keys()) { |
| 87 | sb.append(", ").append(key).append('=').append(annotations.value(key)); |
| 88 | } |
| 89 | return sb.toString(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Produces a JSON object from the specified key/value annotations. |
| 94 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 95 | * @param mapper ObjectMapper to use while converting to JSON |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 96 | * @param annotations key/value annotations |
| 97 | * @return JSON object |
| 98 | */ |
| 99 | public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) { |
| 100 | ObjectNode result = mapper.createObjectNode(); |
| 101 | for (String key : annotations.keys()) { |
| 102 | result.put(key, annotations.value(key)); |
| 103 | } |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | /** |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 108 | * Executes this command. |
| 109 | */ |
| 110 | protected abstract void execute(); |
| 111 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Indicates whether JSON format should be output. |
| 114 | * |
| 115 | * @return true if JSON is requested |
| 116 | */ |
| 117 | protected boolean outputJson() { |
| 118 | return json; |
| 119 | } |
| 120 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 121 | @Override |
| 122 | protected Object doExecute() throws Exception { |
| 123 | try { |
| 124 | execute(); |
| 125 | } catch (ServiceNotFoundException e) { |
| 126 | error(e.getMessage()); |
| 127 | } |
| 128 | return null; |
| 129 | } |
| 130 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 131 | } |