blob: 64f6b500e7879f89cf880f71ae5f88352c7b4485 [file] [log] [blame]
Thomas Vachuska6d697f12015-03-08 20:59:50 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska6d697f12015-03-08 20:59:50 -07003 *
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 */
16package org.onosproject.cli.cfg;
17
Ray Milkeyb0352412015-04-13 08:31:04 -070018import java.util.Optional;
19import java.util.Set;
20
Thomas Vachuska6d697f12015-03-08 20:59:50 -070021import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
Thomas Vachuska8ceff302015-04-03 11:45:53 -070023import org.apache.karaf.shell.commands.Option;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070024import org.onosproject.cfg.ComponentConfigService;
25import org.onosproject.cfg.ConfigProperty;
26import org.onosproject.cli.AbstractShellCommand;
27
Ray Milkeyb0352412015-04-13 08:31:04 -070028import com.fasterxml.jackson.databind.JsonNode;
29import com.fasterxml.jackson.databind.ObjectMapper;
30import com.fasterxml.jackson.databind.node.ArrayNode;
31import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070032
33import static com.google.common.base.Strings.isNullOrEmpty;
34
35/**
Jon Hall6148f362015-04-08 16:26:12 -070036 * Manages component configuration.
Thomas Vachuska6d697f12015-03-08 20:59:50 -070037 */
38@Command(scope = "onos", name = "cfg",
39 description = "Manages component configuration")
40public class ComponentConfigCommand extends AbstractShellCommand {
41
42 static final String GET = "get";
43 static final String SET = "set";
44
45 private static final String FMT = " name=%s, type=%s, value=%s, defaultValue=%s, description=%s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070046 private static final String SHORT_FMT = " %s=%s";
47
48 @Option(name = "-s", aliases = "--short", description = "Show short output only",
49 required = false, multiValued = false)
50 private boolean shortOnly = false;
51
Thomas Vachuska6d697f12015-03-08 20:59:50 -070052
53 @Argument(index = 0, name = "command",
Jon Hall6148f362015-04-08 16:26:12 -070054 description = "Command name (get|set)",
Thomas Vachuska6d697f12015-03-08 20:59:50 -070055 required = false, multiValued = false)
56 String command = null;
57
58 @Argument(index = 1, name = "component", description = "Component name",
59 required = false, multiValued = false)
60 String component = null;
61
62 @Argument(index = 2, name = "name", description = "Property name",
63 required = false, multiValued = false)
64 String name = null;
65
66 @Argument(index = 3, name = "value", description = "Property value",
67 required = false, multiValued = false)
68 String value = null;
69
70 ComponentConfigService service;
71
72 @Override
73 protected void execute() {
74 service = get(ComponentConfigService.class);
75 if (isNullOrEmpty(command)) {
76 listComponents();
77 } else if (command.equals(GET) && isNullOrEmpty(component)) {
78 listAllComponentsProperties();
79 } else if (command.equals(GET) && isNullOrEmpty(name)) {
80 listComponentProperties(component);
81 } else if (command.equals(GET)) {
82 listComponentProperty(component, name);
83 } else if (command.equals(SET) && isNullOrEmpty(value)) {
84 service.unsetProperty(component, name);
85 } else if (command.equals(SET)) {
86 service.setProperty(component, name, value);
87 } else {
88 error("Illegal usage");
89 }
90 }
91
92 private void listAllComponentsProperties() {
Ray Milkeyb0352412015-04-13 08:31:04 -070093 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;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700128 }
129
130 private void listComponents() {
Ray Milkeyb0352412015-04-13 08:31:04 -0700131 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 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700138 }
139
140 private void listComponentProperties(String component) {
Ray Milkeyb0352412015-04-13 08:31:04 -0700141 if (outputJson()) {
142 print("%s", jsonComponent(component, new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700143 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700144 Set<ConfigProperty> props = service.getProperties(component);
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 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700154 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700155 }
156
157 private void listComponentProperty(String component, String name) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700158 Set<ConfigProperty> props = service.getProperties(component);
Ray Milkey1cdfd8e2015-04-10 15:20:03 -0700159
160 if (props == null) {
161 return;
162 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700163 Optional<ConfigProperty> property = props.stream()
164 .filter(p -> p.name().equals(name)).findFirst();
Ray Milkeyb0352412015-04-13 08:31:04 -0700165 if (outputJson()) {
166 print("%s", jsonProperty(property.get(), new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700167 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700168 if (!property.isPresent()) {
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 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700179 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700180 }
181
182}