Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.cli.cfg; |
| 17 | |
Ray Milkey | b035241 | 2015-04-13 08:31:04 -0700 | [diff] [blame] | 18 | import java.util.Optional; |
| 19 | import java.util.Set; |
| 20 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Argument; |
| 22 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.apache.karaf.shell.api.action.Option; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 26 | import org.onosproject.cfg.ComponentConfigService; |
| 27 | import org.onosproject.cfg.ConfigProperty; |
| 28 | import org.onosproject.cli.AbstractShellCommand; |
| 29 | |
Ray Milkey | b035241 | 2015-04-13 08:31:04 -0700 | [diff] [blame] | 30 | import com.fasterxml.jackson.databind.JsonNode; |
| 31 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 32 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 33 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 34 | |
| 35 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 36 | |
| 37 | /** |
Jon Hall | 6148f36 | 2015-04-08 16:26:12 -0700 | [diff] [blame] | 38 | * Manages component configuration. |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 39 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | @Service |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 41 | @Command(scope = "onos", name = "cfg", |
| 42 | description = "Manages component configuration") |
| 43 | public class ComponentConfigCommand extends AbstractShellCommand { |
| 44 | |
| 45 | static final String GET = "get"; |
| 46 | static final String SET = "set"; |
Thomas Vachuska | 4b32fcb | 2018-02-20 12:35:19 -0800 | [diff] [blame] | 47 | static final String PRESET = "preset"; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 48 | |
| 49 | private static final String FMT = " name=%s, type=%s, value=%s, defaultValue=%s, description=%s"; |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 50 | private static final String SHORT_FMT = " %s=%s"; |
| 51 | |
| 52 | @Option(name = "-s", aliases = "--short", description = "Show short output only", |
| 53 | required = false, multiValued = false) |
| 54 | private boolean shortOnly = false; |
| 55 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 56 | |
| 57 | @Argument(index = 0, name = "command", |
Thomas Vachuska | 4b32fcb | 2018-02-20 12:35:19 -0800 | [diff] [blame] | 58 | description = "Command name (get|set|preset)", |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 59 | required = false, multiValued = false) |
Ray Milkey | ae965fe | 2018-10-23 14:20:10 -0700 | [diff] [blame] | 60 | @Completion(ComponentConfigCommandCompleter.class) |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 61 | String command = null; |
| 62 | |
| 63 | @Argument(index = 1, name = "component", description = "Component name", |
| 64 | required = false, multiValued = false) |
Ray Milkey | ae965fe | 2018-10-23 14:20:10 -0700 | [diff] [blame] | 65 | @Completion(ComponentNameCompleter.class) |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 66 | String component = null; |
| 67 | |
| 68 | @Argument(index = 2, name = "name", description = "Property name", |
| 69 | required = false, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 70 | @Completion(ComponentPropertyNameCompleter.class) |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 71 | String name = null; |
| 72 | |
| 73 | @Argument(index = 3, name = "value", description = "Property value", |
| 74 | required = false, multiValued = false) |
| 75 | String value = null; |
| 76 | |
| 77 | ComponentConfigService service; |
| 78 | |
| 79 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 80 | protected void doExecute() { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 81 | service = get(ComponentConfigService.class); |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 82 | try { |
| 83 | if (isNullOrEmpty(command)) { |
| 84 | listComponents(); |
| 85 | } else if (command.equals(GET) && isNullOrEmpty(component)) { |
| 86 | listAllComponentsProperties(); |
| 87 | } else if (command.equals(GET) && isNullOrEmpty(name)) { |
| 88 | listComponentProperties(component); |
| 89 | } else if (command.equals(GET)) { |
| 90 | listComponentProperty(component, name); |
| 91 | } else if (command.equals(SET) && isNullOrEmpty(value)) { |
| 92 | service.unsetProperty(component, name); |
| 93 | } else if (command.equals(SET)) { |
| 94 | service.setProperty(component, name, value); |
Thomas Vachuska | 4b32fcb | 2018-02-20 12:35:19 -0800 | [diff] [blame] | 95 | } else if (command.equals(PRESET)) { |
| 96 | service.preSetProperty(component, name, value); |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 97 | } else { |
| 98 | error("Illegal usage"); |
| 99 | } |
| 100 | } catch (IllegalArgumentException e) { |
| 101 | error(e.getMessage()); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | private void listAllComponentsProperties() { |
Ray Milkey | b035241 | 2015-04-13 08:31:04 -0700 | [diff] [blame] | 106 | if (outputJson()) { |
| 107 | print("%s", jsonComponentProperties()); |
| 108 | } else { |
| 109 | service.getComponentNames().forEach(this::listComponentProperties); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private JsonNode jsonProperty(ConfigProperty configProperty, ObjectMapper mapper) { |
| 114 | return mapper.createObjectNode() |
| 115 | .put("name", configProperty.name()) |
| 116 | .put("type", configProperty.type().toString().toLowerCase()) |
| 117 | .put("value", configProperty.value()) |
| 118 | .put("defaultValue", configProperty.defaultValue()) |
| 119 | .put("description", configProperty.description()); |
| 120 | } |
| 121 | |
| 122 | private JsonNode jsonComponent(String component, ObjectMapper mapper) { |
| 123 | ObjectNode node = mapper.createObjectNode() |
| 124 | .put("componentName", component); |
| 125 | final ArrayNode propertiesJson = node.putArray("properties"); |
| 126 | Set<ConfigProperty> properties = service.getProperties(component); |
| 127 | if (properties != null) { |
| 128 | properties.forEach(configProperty -> propertiesJson.add( |
| 129 | jsonProperty(configProperty, mapper))); |
| 130 | } |
| 131 | return node; |
| 132 | } |
| 133 | |
| 134 | private JsonNode jsonComponentProperties() { |
| 135 | ObjectMapper mapper = new ObjectMapper(); |
| 136 | ArrayNode result = mapper.createArrayNode(); |
| 137 | service.getComponentNames() |
| 138 | .forEach(component -> result.add(jsonComponent(component, mapper))); |
| 139 | |
| 140 | return result; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | private void listComponents() { |
Ray Milkey | b035241 | 2015-04-13 08:31:04 -0700 | [diff] [blame] | 144 | if (outputJson()) { |
| 145 | ArrayNode node = new ObjectMapper().createArrayNode(); |
| 146 | service.getComponentNames().forEach(node::add); |
| 147 | print("%s", node); |
| 148 | } else { |
| 149 | service.getComponentNames().forEach(n -> print("%s", n)); |
| 150 | } |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private void listComponentProperties(String component) { |
Ray Milkey | b035241 | 2015-04-13 08:31:04 -0700 | [diff] [blame] | 154 | if (outputJson()) { |
| 155 | print("%s", jsonComponent(component, new ObjectMapper())); |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 156 | } else { |
Ray Milkey | b035241 | 2015-04-13 08:31:04 -0700 | [diff] [blame] | 157 | Set<ConfigProperty> props = service.getProperties(component); |
| 158 | print("%s", component); |
| 159 | if (props == null) { |
| 160 | print("No properties for component " + component + " found"); |
| 161 | } else if (shortOnly) { |
| 162 | props.forEach(p -> print(SHORT_FMT, p.name(), p.value())); |
| 163 | } else { |
| 164 | props.forEach(p -> print(FMT, p.name(), p.type().toString().toLowerCase(), |
| 165 | p.value(), p.defaultValue(), p.description())); |
| 166 | } |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 167 | } |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private void listComponentProperty(String component, String name) { |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 171 | Set<ConfigProperty> props = service.getProperties(component); |
Ray Milkey | 1cdfd8e | 2015-04-10 15:20:03 -0700 | [diff] [blame] | 172 | |
| 173 | if (props == null) { |
| 174 | return; |
| 175 | } |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 176 | Optional<ConfigProperty> property = props.stream() |
| 177 | .filter(p -> p.name().equals(name)).findFirst(); |
Ray Milkey | b035241 | 2015-04-13 08:31:04 -0700 | [diff] [blame] | 178 | if (outputJson()) { |
| 179 | print("%s", jsonProperty(property.get(), new ObjectMapper())); |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 180 | } else { |
Ray Milkey | b035241 | 2015-04-13 08:31:04 -0700 | [diff] [blame] | 181 | if (!property.isPresent()) { |
| 182 | print("Property " + name + " for component " + component + " not found"); |
| 183 | return; |
| 184 | } |
| 185 | ConfigProperty p = property.get(); |
| 186 | if (shortOnly) { |
| 187 | print(SHORT_FMT, p.name(), p.value()); |
| 188 | } else { |
| 189 | print(FMT, p.name(), p.type().toString().toLowerCase(), p.value(), |
| 190 | p.defaultValue(), p.description()); |
| 191 | } |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 192 | } |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | } |