Committed by
Gerrit Code Review
ONOS-1602 - JSON output for cfg command in CLI
Change-Id: I439ea0982cc487417cd1d1a797d7f671ae6797f8
Showing
1 changed file
with
74 additions
and
22 deletions
| ... | @@ -15,6 +15,9 @@ | ... | @@ -15,6 +15,9 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.cli.cfg; | 16 | package org.onosproject.cli.cfg; |
| 17 | 17 | ||
| 18 | +import java.util.Optional; | ||
| 19 | +import java.util.Set; | ||
| 20 | + | ||
| 18 | import org.apache.karaf.shell.commands.Argument; | 21 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; | 22 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.apache.karaf.shell.commands.Option; | 23 | import org.apache.karaf.shell.commands.Option; |
| ... | @@ -22,8 +25,10 @@ import org.onosproject.cfg.ComponentConfigService; | ... | @@ -22,8 +25,10 @@ import org.onosproject.cfg.ComponentConfigService; |
| 22 | import org.onosproject.cfg.ConfigProperty; | 25 | import org.onosproject.cfg.ConfigProperty; |
| 23 | import org.onosproject.cli.AbstractShellCommand; | 26 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | 27 | ||
| 25 | -import java.util.Optional; | 28 | +import com.fasterxml.jackson.databind.JsonNode; |
| 26 | -import java.util.Set; | 29 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 30 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| 31 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 27 | 32 | ||
| 28 | import static com.google.common.base.Strings.isNullOrEmpty; | 33 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 29 | 34 | ||
| ... | @@ -85,23 +90,67 @@ public class ComponentConfigCommand extends AbstractShellCommand { | ... | @@ -85,23 +90,67 @@ public class ComponentConfigCommand extends AbstractShellCommand { |
| 85 | } | 90 | } |
| 86 | 91 | ||
| 87 | private void listAllComponentsProperties() { | 92 | private void listAllComponentsProperties() { |
| 88 | - service.getComponentNames().forEach(this::listComponentProperties); | 93 | + if (outputJson()) { |
| 94 | + print("%s", jsonComponentProperties()); | ||
| 95 | + } else { | ||
| 96 | + service.getComponentNames().forEach(this::listComponentProperties); | ||
| 97 | + } | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + private JsonNode jsonProperty(ConfigProperty configProperty, ObjectMapper mapper) { | ||
| 101 | + return mapper.createObjectNode() | ||
| 102 | + .put("name", configProperty.name()) | ||
| 103 | + .put("type", configProperty.type().toString().toLowerCase()) | ||
| 104 | + .put("value", configProperty.value()) | ||
| 105 | + .put("defaultValue", configProperty.defaultValue()) | ||
| 106 | + .put("description", configProperty.description()); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + private JsonNode jsonComponent(String component, ObjectMapper mapper) { | ||
| 110 | + ObjectNode node = mapper.createObjectNode() | ||
| 111 | + .put("componentName", component); | ||
| 112 | + final ArrayNode propertiesJson = node.putArray("properties"); | ||
| 113 | + Set<ConfigProperty> properties = service.getProperties(component); | ||
| 114 | + if (properties != null) { | ||
| 115 | + properties.forEach(configProperty -> propertiesJson.add( | ||
| 116 | + jsonProperty(configProperty, mapper))); | ||
| 117 | + } | ||
| 118 | + return node; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + private JsonNode jsonComponentProperties() { | ||
| 122 | + ObjectMapper mapper = new ObjectMapper(); | ||
| 123 | + ArrayNode result = mapper.createArrayNode(); | ||
| 124 | + service.getComponentNames() | ||
| 125 | + .forEach(component -> result.add(jsonComponent(component, mapper))); | ||
| 126 | + | ||
| 127 | + return result; | ||
| 89 | } | 128 | } |
| 90 | 129 | ||
| 91 | private void listComponents() { | 130 | private void listComponents() { |
| 92 | - service.getComponentNames().forEach(n -> print("%s", n)); | 131 | + if (outputJson()) { |
| 132 | + ArrayNode node = new ObjectMapper().createArrayNode(); | ||
| 133 | + service.getComponentNames().forEach(node::add); | ||
| 134 | + print("%s", node); | ||
| 135 | + } else { | ||
| 136 | + service.getComponentNames().forEach(n -> print("%s", n)); | ||
| 137 | + } | ||
| 93 | } | 138 | } |
| 94 | 139 | ||
| 95 | private void listComponentProperties(String component) { | 140 | private void listComponentProperties(String component) { |
| 96 | - Set<ConfigProperty> props = service.getProperties(component); | 141 | + if (outputJson()) { |
| 97 | - print("%s", component); | 142 | + print("%s", jsonComponent(component, new ObjectMapper())); |
| 98 | - if (props == null) { | ||
| 99 | - print("No properties for component " + component + " found"); | ||
| 100 | - } else if (shortOnly) { | ||
| 101 | - props.forEach(p -> print(SHORT_FMT, p.name(), p.value())); | ||
| 102 | } else { | 143 | } else { |
| 103 | - props.forEach(p -> print(FMT, p.name(), p.type().toString().toLowerCase(), | 144 | + Set<ConfigProperty> props = service.getProperties(component); |
| 104 | - p.value(), p.defaultValue(), p.description())); | 145 | + print("%s", component); |
| 146 | + if (props == null) { | ||
| 147 | + print("No properties for component " + component + " found"); | ||
| 148 | + } else if (shortOnly) { | ||
| 149 | + props.forEach(p -> print(SHORT_FMT, p.name(), p.value())); | ||
| 150 | + } else { | ||
| 151 | + props.forEach(p -> print(FMT, p.name(), p.type().toString().toLowerCase(), | ||
| 152 | + p.value(), p.defaultValue(), p.description())); | ||
| 153 | + } | ||
| 105 | } | 154 | } |
| 106 | } | 155 | } |
| 107 | 156 | ||
| ... | @@ -111,19 +160,22 @@ public class ComponentConfigCommand extends AbstractShellCommand { | ... | @@ -111,19 +160,22 @@ public class ComponentConfigCommand extends AbstractShellCommand { |
| 111 | if (props == null) { | 160 | if (props == null) { |
| 112 | return; | 161 | return; |
| 113 | } | 162 | } |
| 114 | - | ||
| 115 | Optional<ConfigProperty> property = props.stream() | 163 | Optional<ConfigProperty> property = props.stream() |
| 116 | .filter(p -> p.name().equals(name)).findFirst(); | 164 | .filter(p -> p.name().equals(name)).findFirst(); |
| 117 | - if (!property.isPresent()) { | 165 | + if (outputJson()) { |
| 118 | - print("Property " + name + " for component " + component + " not found"); | 166 | + print("%s", jsonProperty(property.get(), new ObjectMapper())); |
| 119 | - return; | ||
| 120 | - } | ||
| 121 | - ConfigProperty p = property.get(); | ||
| 122 | - if (shortOnly) { | ||
| 123 | - print(SHORT_FMT, p.name(), p.value()); | ||
| 124 | } else { | 167 | } else { |
| 125 | - print(FMT, p.name(), p.type().toString().toLowerCase(), p.value(), | 168 | + if (!property.isPresent()) { |
| 126 | - p.defaultValue(), p.description()); | 169 | + print("Property " + name + " for component " + component + " not found"); |
| 170 | + return; | ||
| 171 | + } | ||
| 172 | + ConfigProperty p = property.get(); | ||
| 173 | + if (shortOnly) { | ||
| 174 | + print(SHORT_FMT, p.name(), p.value()); | ||
| 175 | + } else { | ||
| 176 | + print(FMT, p.name(), p.type().toString().toLowerCase(), p.value(), | ||
| 177 | + p.defaultValue(), p.description()); | ||
| 178 | + } | ||
| 127 | } | 179 | } |
| 128 | } | 180 | } |
| 129 | 181 | ... | ... |
-
Please register or login to post a comment