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