blob: ef20d0723fe7cf4a7ebed225643657ad4696e325 [file] [log] [blame]
Thomas Vachuska6d697f12015-03-08 20:59:50 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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";
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080044 static final String PRESET = "preset";
Thomas Vachuska6d697f12015-03-08 20:59:50 -070045
46 private static final String FMT = " name=%s, type=%s, value=%s, defaultValue=%s, description=%s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070047 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 Vachuska6d697f12015-03-08 20:59:50 -070053
54 @Argument(index = 0, name = "command",
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080055 description = "Command name (get|set|preset)",
Thomas Vachuska6d697f12015-03-08 20:59:50 -070056 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 subramanian8c9a7012017-04-26 14:59:02 +053076 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 Vachuska4b32fcb2018-02-20 12:35:19 -080089 } else if (command.equals(PRESET)) {
90 service.preSetProperty(component, name, value);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053091 } else {
92 error("Illegal usage");
93 }
94 } catch (IllegalArgumentException e) {
95 error(e.getMessage());
Thomas Vachuska6d697f12015-03-08 20:59:50 -070096 }
97 }
98
99 private void listAllComponentsProperties() {
Ray Milkeyb0352412015-04-13 08:31:04 -0700100 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 Vachuska6d697f12015-03-08 20:59:50 -0700135 }
136
137 private void listComponents() {
Ray Milkeyb0352412015-04-13 08:31:04 -0700138 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 Vachuska6d697f12015-03-08 20:59:50 -0700145 }
146
147 private void listComponentProperties(String component) {
Ray Milkeyb0352412015-04-13 08:31:04 -0700148 if (outputJson()) {
149 print("%s", jsonComponent(component, new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700150 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700151 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 Vachuska8ceff302015-04-03 11:45:53 -0700161 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700162 }
163
164 private void listComponentProperty(String component, String name) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700165 Set<ConfigProperty> props = service.getProperties(component);
Ray Milkey1cdfd8e2015-04-10 15:20:03 -0700166
167 if (props == null) {
168 return;
169 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700170 Optional<ConfigProperty> property = props.stream()
171 .filter(p -> p.name().equals(name)).findFirst();
Ray Milkeyb0352412015-04-13 08:31:04 -0700172 if (outputJson()) {
173 print("%s", jsonProperty(property.get(), new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700174 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700175 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 Vachuska8ceff302015-04-03 11:45:53 -0700186 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700187 }
188
189}