blob: 0a85ae037b88d5feb406859e8537ddf4a9585ed9 [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);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053075 try {
76 if (isNullOrEmpty(command)) {
77 listComponents();
78 } else if (command.equals(GET) && isNullOrEmpty(component)) {
79 listAllComponentsProperties();
80 } else if (command.equals(GET) && isNullOrEmpty(name)) {
81 listComponentProperties(component);
82 } else if (command.equals(GET)) {
83 listComponentProperty(component, name);
84 } else if (command.equals(SET) && isNullOrEmpty(value)) {
85 service.unsetProperty(component, name);
86 } else if (command.equals(SET)) {
87 service.setProperty(component, name, value);
88 } else {
89 error("Illegal usage");
90 }
91 } catch (IllegalArgumentException e) {
92 error(e.getMessage());
Thomas Vachuska6d697f12015-03-08 20:59:50 -070093 }
94 }
95
96 private void listAllComponentsProperties() {
Ray Milkeyb0352412015-04-13 08:31:04 -070097 if (outputJson()) {
98 print("%s", jsonComponentProperties());
99 } else {
100 service.getComponentNames().forEach(this::listComponentProperties);
101 }
102 }
103
104 private JsonNode jsonProperty(ConfigProperty configProperty, ObjectMapper mapper) {
105 return mapper.createObjectNode()
106 .put("name", configProperty.name())
107 .put("type", configProperty.type().toString().toLowerCase())
108 .put("value", configProperty.value())
109 .put("defaultValue", configProperty.defaultValue())
110 .put("description", configProperty.description());
111 }
112
113 private JsonNode jsonComponent(String component, ObjectMapper mapper) {
114 ObjectNode node = mapper.createObjectNode()
115 .put("componentName", component);
116 final ArrayNode propertiesJson = node.putArray("properties");
117 Set<ConfigProperty> properties = service.getProperties(component);
118 if (properties != null) {
119 properties.forEach(configProperty -> propertiesJson.add(
120 jsonProperty(configProperty, mapper)));
121 }
122 return node;
123 }
124
125 private JsonNode jsonComponentProperties() {
126 ObjectMapper mapper = new ObjectMapper();
127 ArrayNode result = mapper.createArrayNode();
128 service.getComponentNames()
129 .forEach(component -> result.add(jsonComponent(component, mapper)));
130
131 return result;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700132 }
133
134 private void listComponents() {
Ray Milkeyb0352412015-04-13 08:31:04 -0700135 if (outputJson()) {
136 ArrayNode node = new ObjectMapper().createArrayNode();
137 service.getComponentNames().forEach(node::add);
138 print("%s", node);
139 } else {
140 service.getComponentNames().forEach(n -> print("%s", n));
141 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700142 }
143
144 private void listComponentProperties(String component) {
Ray Milkeyb0352412015-04-13 08:31:04 -0700145 if (outputJson()) {
146 print("%s", jsonComponent(component, new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700147 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700148 Set<ConfigProperty> props = service.getProperties(component);
149 print("%s", component);
150 if (props == null) {
151 print("No properties for component " + component + " found");
152 } else if (shortOnly) {
153 props.forEach(p -> print(SHORT_FMT, p.name(), p.value()));
154 } else {
155 props.forEach(p -> print(FMT, p.name(), p.type().toString().toLowerCase(),
156 p.value(), p.defaultValue(), p.description()));
157 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700158 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700159 }
160
161 private void listComponentProperty(String component, String name) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700162 Set<ConfigProperty> props = service.getProperties(component);
Ray Milkey1cdfd8e2015-04-10 15:20:03 -0700163
164 if (props == null) {
165 return;
166 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700167 Optional<ConfigProperty> property = props.stream()
168 .filter(p -> p.name().equals(name)).findFirst();
Ray Milkeyb0352412015-04-13 08:31:04 -0700169 if (outputJson()) {
170 print("%s", jsonProperty(property.get(), new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700171 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700172 if (!property.isPresent()) {
173 print("Property " + name + " for component " + component + " not found");
174 return;
175 }
176 ConfigProperty p = property.get();
177 if (shortOnly) {
178 print(SHORT_FMT, p.name(), p.value());
179 } else {
180 print(FMT, p.name(), p.type().toString().toLowerCase(), p.value(),
181 p.defaultValue(), p.description());
182 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700183 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700184 }
185
186}