Ray Milkey
Committed by Gerrit Code Review

ONOS-1602 - JSON output for cfg command in CLI

Change-Id: I439ea0982cc487417cd1d1a797d7f671ae6797f8
...@@ -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,14 +90,57 @@ public class ComponentConfigCommand extends AbstractShellCommand { ...@@ -85,14 +90,57 @@ public class ComponentConfigCommand extends AbstractShellCommand {
85 } 90 }
86 91
87 private void listAllComponentsProperties() { 92 private void listAllComponentsProperties() {
93 + if (outputJson()) {
94 + print("%s", jsonComponentProperties());
95 + } else {
88 service.getComponentNames().forEach(this::listComponentProperties); 96 service.getComponentNames().forEach(this::listComponentProperties);
89 } 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;
128 + }
90 129
91 private void listComponents() { 130 private void listComponents() {
131 + if (outputJson()) {
132 + ArrayNode node = new ObjectMapper().createArrayNode();
133 + service.getComponentNames().forEach(node::add);
134 + print("%s", node);
135 + } else {
92 service.getComponentNames().forEach(n -> print("%s", n)); 136 service.getComponentNames().forEach(n -> print("%s", n));
93 } 137 }
138 + }
94 139
95 private void listComponentProperties(String component) { 140 private void listComponentProperties(String component) {
141 + if (outputJson()) {
142 + print("%s", jsonComponent(component, new ObjectMapper()));
143 + } else {
96 Set<ConfigProperty> props = service.getProperties(component); 144 Set<ConfigProperty> props = service.getProperties(component);
97 print("%s", component); 145 print("%s", component);
98 if (props == null) { 146 if (props == null) {
...@@ -104,6 +152,7 @@ public class ComponentConfigCommand extends AbstractShellCommand { ...@@ -104,6 +152,7 @@ public class ComponentConfigCommand extends AbstractShellCommand {
104 p.value(), p.defaultValue(), p.description())); 152 p.value(), p.defaultValue(), p.description()));
105 } 153 }
106 } 154 }
155 + }
107 156
108 private void listComponentProperty(String component, String name) { 157 private void listComponentProperty(String component, String name) {
109 Set<ConfigProperty> props = service.getProperties(component); 158 Set<ConfigProperty> props = service.getProperties(component);
...@@ -111,9 +160,11 @@ public class ComponentConfigCommand extends AbstractShellCommand { ...@@ -111,9 +160,11 @@ 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();
165 + if (outputJson()) {
166 + print("%s", jsonProperty(property.get(), new ObjectMapper()));
167 + } else {
117 if (!property.isPresent()) { 168 if (!property.isPresent()) {
118 print("Property " + name + " for component " + component + " not found"); 169 print("Property " + name + " for component " + component + " not found");
119 return; 170 return;
...@@ -126,5 +177,6 @@ public class ComponentConfigCommand extends AbstractShellCommand { ...@@ -126,5 +177,6 @@ public class ComponentConfigCommand extends AbstractShellCommand {
126 p.defaultValue(), p.description()); 177 p.defaultValue(), p.description());
127 } 178 }
128 } 179 }
180 + }
129 181
130 } 182 }
......