blob: 546a63707c35f14b20c7889d315aba52be7c4484 [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.Argument;
22import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
24import org.apache.karaf.shell.api.action.Option;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070025import org.onosproject.cfg.ComponentConfigService;
26import org.onosproject.cfg.ConfigProperty;
27import org.onosproject.cli.AbstractShellCommand;
28
Ray Milkeyb0352412015-04-13 08:31:04 -070029import com.fasterxml.jackson.databind.JsonNode;
30import com.fasterxml.jackson.databind.ObjectMapper;
31import com.fasterxml.jackson.databind.node.ArrayNode;
32import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070033
34import static com.google.common.base.Strings.isNullOrEmpty;
35
36/**
Jon Hall6148f362015-04-08 16:26:12 -070037 * Manages component configuration.
Thomas Vachuska6d697f12015-03-08 20:59:50 -070038 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039@Service
Thomas Vachuska6d697f12015-03-08 20:59:50 -070040@Command(scope = "onos", name = "cfg",
41 description = "Manages component configuration")
42public class ComponentConfigCommand extends AbstractShellCommand {
43
44 static final String GET = "get";
45 static final String SET = "set";
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080046 static final String PRESET = "preset";
Thomas Vachuska6d697f12015-03-08 20:59:50 -070047
48 private static final String FMT = " name=%s, type=%s, value=%s, defaultValue=%s, description=%s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070049 private static final String SHORT_FMT = " %s=%s";
50
51 @Option(name = "-s", aliases = "--short", description = "Show short output only",
52 required = false, multiValued = false)
53 private boolean shortOnly = false;
54
Thomas Vachuska6d697f12015-03-08 20:59:50 -070055
56 @Argument(index = 0, name = "command",
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080057 description = "Command name (get|set|preset)",
Thomas Vachuska6d697f12015-03-08 20:59:50 -070058 required = false, multiValued = false)
59 String command = null;
60
61 @Argument(index = 1, name = "component", description = "Component name",
62 required = false, multiValued = false)
63 String component = null;
64
65 @Argument(index = 2, name = "name", description = "Property name",
66 required = false, multiValued = false)
67 String name = null;
68
69 @Argument(index = 3, name = "value", description = "Property value",
70 required = false, multiValued = false)
71 String value = null;
72
73 ComponentConfigService service;
74
75 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 protected void doExecute() {
Thomas Vachuska6d697f12015-03-08 20:59:50 -070077 service = get(ComponentConfigService.class);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053078 try {
79 if (isNullOrEmpty(command)) {
80 listComponents();
81 } else if (command.equals(GET) && isNullOrEmpty(component)) {
82 listAllComponentsProperties();
83 } else if (command.equals(GET) && isNullOrEmpty(name)) {
84 listComponentProperties(component);
85 } else if (command.equals(GET)) {
86 listComponentProperty(component, name);
87 } else if (command.equals(SET) && isNullOrEmpty(value)) {
88 service.unsetProperty(component, name);
89 } else if (command.equals(SET)) {
90 service.setProperty(component, name, value);
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080091 } else if (command.equals(PRESET)) {
92 service.preSetProperty(component, name, value);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053093 } else {
94 error("Illegal usage");
95 }
96 } catch (IllegalArgumentException e) {
97 error(e.getMessage());
Thomas Vachuska6d697f12015-03-08 20:59:50 -070098 }
99 }
100
101 private void listAllComponentsProperties() {
Ray Milkeyb0352412015-04-13 08:31:04 -0700102 if (outputJson()) {
103 print("%s", jsonComponentProperties());
104 } else {
105 service.getComponentNames().forEach(this::listComponentProperties);
106 }
107 }
108
109 private JsonNode jsonProperty(ConfigProperty configProperty, ObjectMapper mapper) {
110 return mapper.createObjectNode()
111 .put("name", configProperty.name())
112 .put("type", configProperty.type().toString().toLowerCase())
113 .put("value", configProperty.value())
114 .put("defaultValue", configProperty.defaultValue())
115 .put("description", configProperty.description());
116 }
117
118 private JsonNode jsonComponent(String component, ObjectMapper mapper) {
119 ObjectNode node = mapper.createObjectNode()
120 .put("componentName", component);
121 final ArrayNode propertiesJson = node.putArray("properties");
122 Set<ConfigProperty> properties = service.getProperties(component);
123 if (properties != null) {
124 properties.forEach(configProperty -> propertiesJson.add(
125 jsonProperty(configProperty, mapper)));
126 }
127 return node;
128 }
129
130 private JsonNode jsonComponentProperties() {
131 ObjectMapper mapper = new ObjectMapper();
132 ArrayNode result = mapper.createArrayNode();
133 service.getComponentNames()
134 .forEach(component -> result.add(jsonComponent(component, mapper)));
135
136 return result;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700137 }
138
139 private void listComponents() {
Ray Milkeyb0352412015-04-13 08:31:04 -0700140 if (outputJson()) {
141 ArrayNode node = new ObjectMapper().createArrayNode();
142 service.getComponentNames().forEach(node::add);
143 print("%s", node);
144 } else {
145 service.getComponentNames().forEach(n -> print("%s", n));
146 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700147 }
148
149 private void listComponentProperties(String component) {
Ray Milkeyb0352412015-04-13 08:31:04 -0700150 if (outputJson()) {
151 print("%s", jsonComponent(component, new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700152 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700153 Set<ConfigProperty> props = service.getProperties(component);
154 print("%s", component);
155 if (props == null) {
156 print("No properties for component " + component + " found");
157 } else if (shortOnly) {
158 props.forEach(p -> print(SHORT_FMT, p.name(), p.value()));
159 } else {
160 props.forEach(p -> print(FMT, p.name(), p.type().toString().toLowerCase(),
161 p.value(), p.defaultValue(), p.description()));
162 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700163 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700164 }
165
166 private void listComponentProperty(String component, String name) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700167 Set<ConfigProperty> props = service.getProperties(component);
Ray Milkey1cdfd8e2015-04-10 15:20:03 -0700168
169 if (props == null) {
170 return;
171 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700172 Optional<ConfigProperty> property = props.stream()
173 .filter(p -> p.name().equals(name)).findFirst();
Ray Milkeyb0352412015-04-13 08:31:04 -0700174 if (outputJson()) {
175 print("%s", jsonProperty(property.get(), new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700176 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700177 if (!property.isPresent()) {
178 print("Property " + name + " for component " + component + " not found");
179 return;
180 }
181 ConfigProperty p = property.get();
182 if (shortOnly) {
183 print(SHORT_FMT, p.name(), p.value());
184 } else {
185 print(FMT, p.name(), p.type().toString().toLowerCase(), p.value(),
186 p.defaultValue(), p.description());
187 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700188 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700189 }
190
191}