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 | */ |
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 | |
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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.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() { |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 55 | return get(CoreService.class) |
| 56 | .registerApplication("org.onosproject.cli"); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 60 | * Prints the arguments using the specified format. |
| 61 | * |
| 62 | * @param format format string; see {@link String#format} |
| 63 | * @param args arguments |
| 64 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 65 | public void print(String format, Object... args) { |
Brian O'Connor | b28f5d7 | 2015-02-23 19:29:30 +0000 | [diff] [blame] | 66 | System.out.println(String.format(format, args)); |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 67 | } |
| 68 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Prints the arguments using the specified format to error stream. |
| 71 | * |
| 72 | * @param format format string; see {@link String#format} |
| 73 | * @param args arguments |
| 74 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 75 | public void error(String format, Object... args) { |
Brian O'Connor | b28f5d7 | 2015-02-23 19:29:30 +0000 | [diff] [blame] | 76 | System.err.println(String.format(format, args)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 77 | } |
| 78 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 79 | /** |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 80 | * Produces a string image of the specified key/value annotations. |
| 81 | * |
| 82 | * @param annotations key/value annotations |
| 83 | * @return string image with ", k1=v1, k2=v2, ..." pairs |
| 84 | */ |
| 85 | public static String annotations(Annotations annotations) { |
| 86 | StringBuilder sb = new StringBuilder(); |
| 87 | for (String key : annotations.keys()) { |
| 88 | sb.append(", ").append(key).append('=').append(annotations.value(key)); |
| 89 | } |
| 90 | return sb.toString(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Produces a JSON object from the specified key/value annotations. |
| 95 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 96 | * @param mapper ObjectMapper to use while converting to JSON |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 97 | * @param annotations key/value annotations |
| 98 | * @return JSON object |
| 99 | */ |
| 100 | public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) { |
| 101 | ObjectNode result = mapper.createObjectNode(); |
| 102 | for (String key : annotations.keys()) { |
| 103 | result.put(key, annotations.value(key)); |
| 104 | } |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | /** |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 109 | * Executes this command. |
| 110 | */ |
| 111 | protected abstract void execute(); |
| 112 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Indicates whether JSON format should be output. |
| 115 | * |
| 116 | * @return true if JSON is requested |
| 117 | */ |
| 118 | protected boolean outputJson() { |
| 119 | return json; |
| 120 | } |
| 121 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 122 | @Override |
| 123 | protected Object doExecute() throws Exception { |
| 124 | try { |
| 125 | execute(); |
| 126 | } catch (ServiceNotFoundException e) { |
| 127 | error(e.getMessage()); |
| 128 | } |
| 129 | return null; |
| 130 | } |
| 131 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 132 | } |