Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli; |
| 20 | |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Option; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.console.OsgiCommandSupport; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 25 | import org.onlab.onos.core.ApplicationId; |
| 26 | import org.onlab.onos.core.CoreService; |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.Annotations; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 28 | import org.onlab.osgi.DefaultServiceDirectory; |
| 29 | import org.onlab.osgi.ServiceNotFoundException; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Base abstraction of Karaf shell commands. |
| 33 | */ |
| 34 | public abstract class AbstractShellCommand extends OsgiCommandSupport { |
| 35 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 36 | @Option(name = "-j", aliases = "--json", description = "Output JSON", |
| 37 | required = false, multiValued = false) |
| 38 | private boolean json = false; |
| 39 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 40 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 41 | * Returns the reference to the implementation of the specified service. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 42 | * |
| 43 | * @param serviceClass service class |
| 44 | * @param <T> type of service |
| 45 | * @return service implementation |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 46 | * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 47 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 48 | public static <T> T get(Class<T> serviceClass) { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 49 | return DefaultServiceDirectory.getService(serviceClass); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 50 | } |
| 51 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 52 | /** |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 53 | * Returns application ID for the CLI. |
| 54 | * |
| 55 | * @return command-line application identifier |
| 56 | */ |
| 57 | protected ApplicationId appId() { |
| 58 | return get(CoreService.class).registerApplication("org.onlab.onos.cli"); |
| 59 | } |
| 60 | |
| 61 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 62 | * Prints the arguments using the specified format. |
| 63 | * |
| 64 | * @param format format string; see {@link String#format} |
| 65 | * @param args arguments |
| 66 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 67 | public void print(String format, Object... args) { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 68 | System.out.println(String.format(format, args)); |
| 69 | } |
| 70 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Prints the arguments using the specified format to error stream. |
| 73 | * |
| 74 | * @param format format string; see {@link String#format} |
| 75 | * @param args arguments |
| 76 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 77 | public void error(String format, Object... args) { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 78 | System.err.println(String.format(format, args)); |
| 79 | } |
| 80 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 81 | /** |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 82 | * Produces a string image of the specified key/value annotations. |
| 83 | * |
| 84 | * @param annotations key/value annotations |
| 85 | * @return string image with ", k1=v1, k2=v2, ..." pairs |
| 86 | */ |
| 87 | public static String annotations(Annotations annotations) { |
| 88 | StringBuilder sb = new StringBuilder(); |
| 89 | for (String key : annotations.keys()) { |
| 90 | sb.append(", ").append(key).append('=').append(annotations.value(key)); |
| 91 | } |
| 92 | return sb.toString(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Produces a JSON object from the specified key/value annotations. |
| 97 | * |
| 98 | * @param annotations key/value annotations |
| 99 | * @return JSON object |
| 100 | */ |
| 101 | public static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) { |
| 102 | ObjectNode result = mapper.createObjectNode(); |
| 103 | for (String key : annotations.keys()) { |
| 104 | result.put(key, annotations.value(key)); |
| 105 | } |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | /** |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 110 | * Executes this command. |
| 111 | */ |
| 112 | protected abstract void execute(); |
| 113 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Indicates whether JSON format should be output. |
| 116 | * |
| 117 | * @return true if JSON is requested |
| 118 | */ |
| 119 | protected boolean outputJson() { |
| 120 | return json; |
| 121 | } |
| 122 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 123 | @Override |
| 124 | protected Object doExecute() throws Exception { |
| 125 | try { |
| 126 | execute(); |
| 127 | } catch (ServiceNotFoundException e) { |
| 128 | error(e.getMessage()); |
| 129 | } |
| 130 | return null; |
| 131 | } |
| 132 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 133 | } |