blob: 6c694577be5374af053471c9fdefa4dc542c5a57 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070023import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070024import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.apache.karaf.shell.api.action.Option;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070026import org.onosproject.cfg.ComponentConfigService;
27import org.onosproject.cfg.ConfigProperty;
28import org.onosproject.cli.AbstractShellCommand;
29
Ray Milkeyb0352412015-04-13 08:31:04 -070030import com.fasterxml.jackson.databind.JsonNode;
31import com.fasterxml.jackson.databind.ObjectMapper;
32import com.fasterxml.jackson.databind.node.ArrayNode;
33import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey0068fd02018-10-11 15:45:39 -070034import org.onosproject.cli.app.ApplicationCommandCompleter;
35import org.onosproject.cli.app.ApplicationNameCompleter;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070036
37import static com.google.common.base.Strings.isNullOrEmpty;
38
39/**
Jon Hall6148f362015-04-08 16:26:12 -070040 * Manages component configuration.
Thomas Vachuska6d697f12015-03-08 20:59:50 -070041 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042@Service
Thomas Vachuska6d697f12015-03-08 20:59:50 -070043@Command(scope = "onos", name = "cfg",
44 description = "Manages component configuration")
45public class ComponentConfigCommand extends AbstractShellCommand {
46
47 static final String GET = "get";
48 static final String SET = "set";
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080049 static final String PRESET = "preset";
Thomas Vachuska6d697f12015-03-08 20:59:50 -070050
51 private static final String FMT = " name=%s, type=%s, value=%s, defaultValue=%s, description=%s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070052 private static final String SHORT_FMT = " %s=%s";
53
54 @Option(name = "-s", aliases = "--short", description = "Show short output only",
55 required = false, multiValued = false)
56 private boolean shortOnly = false;
57
Thomas Vachuska6d697f12015-03-08 20:59:50 -070058
59 @Argument(index = 0, name = "command",
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080060 description = "Command name (get|set|preset)",
Thomas Vachuska6d697f12015-03-08 20:59:50 -070061 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070062 @Completion(ApplicationCommandCompleter.class)
Thomas Vachuska6d697f12015-03-08 20:59:50 -070063 String command = null;
64
65 @Argument(index = 1, name = "component", description = "Component name",
66 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070067 @Completion(ApplicationNameCompleter.class)
Thomas Vachuska6d697f12015-03-08 20:59:50 -070068 String component = null;
69
70 @Argument(index = 2, name = "name", description = "Property name",
71 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070072 @Completion(ComponentPropertyNameCompleter.class)
Thomas Vachuska6d697f12015-03-08 20:59:50 -070073 String name = null;
74
75 @Argument(index = 3, name = "value", description = "Property value",
76 required = false, multiValued = false)
77 String value = null;
78
79 ComponentConfigService service;
80
81 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070082 protected void doExecute() {
Thomas Vachuska6d697f12015-03-08 20:59:50 -070083 service = get(ComponentConfigService.class);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053084 try {
85 if (isNullOrEmpty(command)) {
86 listComponents();
87 } else if (command.equals(GET) && isNullOrEmpty(component)) {
88 listAllComponentsProperties();
89 } else if (command.equals(GET) && isNullOrEmpty(name)) {
90 listComponentProperties(component);
91 } else if (command.equals(GET)) {
92 listComponentProperty(component, name);
93 } else if (command.equals(SET) && isNullOrEmpty(value)) {
94 service.unsetProperty(component, name);
95 } else if (command.equals(SET)) {
96 service.setProperty(component, name, value);
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080097 } else if (command.equals(PRESET)) {
98 service.preSetProperty(component, name, value);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053099 } else {
100 error("Illegal usage");
101 }
102 } catch (IllegalArgumentException e) {
103 error(e.getMessage());
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700104 }
105 }
106
107 private void listAllComponentsProperties() {
Ray Milkeyb0352412015-04-13 08:31:04 -0700108 if (outputJson()) {
109 print("%s", jsonComponentProperties());
110 } else {
111 service.getComponentNames().forEach(this::listComponentProperties);
112 }
113 }
114
115 private JsonNode jsonProperty(ConfigProperty configProperty, ObjectMapper mapper) {
116 return mapper.createObjectNode()
117 .put("name", configProperty.name())
118 .put("type", configProperty.type().toString().toLowerCase())
119 .put("value", configProperty.value())
120 .put("defaultValue", configProperty.defaultValue())
121 .put("description", configProperty.description());
122 }
123
124 private JsonNode jsonComponent(String component, ObjectMapper mapper) {
125 ObjectNode node = mapper.createObjectNode()
126 .put("componentName", component);
127 final ArrayNode propertiesJson = node.putArray("properties");
128 Set<ConfigProperty> properties = service.getProperties(component);
129 if (properties != null) {
130 properties.forEach(configProperty -> propertiesJson.add(
131 jsonProperty(configProperty, mapper)));
132 }
133 return node;
134 }
135
136 private JsonNode jsonComponentProperties() {
137 ObjectMapper mapper = new ObjectMapper();
138 ArrayNode result = mapper.createArrayNode();
139 service.getComponentNames()
140 .forEach(component -> result.add(jsonComponent(component, mapper)));
141
142 return result;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700143 }
144
145 private void listComponents() {
Ray Milkeyb0352412015-04-13 08:31:04 -0700146 if (outputJson()) {
147 ArrayNode node = new ObjectMapper().createArrayNode();
148 service.getComponentNames().forEach(node::add);
149 print("%s", node);
150 } else {
151 service.getComponentNames().forEach(n -> print("%s", n));
152 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700153 }
154
155 private void listComponentProperties(String component) {
Ray Milkeyb0352412015-04-13 08:31:04 -0700156 if (outputJson()) {
157 print("%s", jsonComponent(component, new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700158 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700159 Set<ConfigProperty> props = service.getProperties(component);
160 print("%s", component);
161 if (props == null) {
162 print("No properties for component " + component + " found");
163 } else if (shortOnly) {
164 props.forEach(p -> print(SHORT_FMT, p.name(), p.value()));
165 } else {
166 props.forEach(p -> print(FMT, p.name(), p.type().toString().toLowerCase(),
167 p.value(), p.defaultValue(), p.description()));
168 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700169 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700170 }
171
172 private void listComponentProperty(String component, String name) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700173 Set<ConfigProperty> props = service.getProperties(component);
Ray Milkey1cdfd8e2015-04-10 15:20:03 -0700174
175 if (props == null) {
176 return;
177 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700178 Optional<ConfigProperty> property = props.stream()
179 .filter(p -> p.name().equals(name)).findFirst();
Ray Milkeyb0352412015-04-13 08:31:04 -0700180 if (outputJson()) {
181 print("%s", jsonProperty(property.get(), new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700182 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700183 if (!property.isPresent()) {
184 print("Property " + name + " for component " + component + " not found");
185 return;
186 }
187 ConfigProperty p = property.get();
188 if (shortOnly) {
189 print(SHORT_FMT, p.name(), p.value());
190 } else {
191 print(FMT, p.name(), p.type().toString().toLowerCase(), p.value(),
192 p.defaultValue(), p.description());
193 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700194 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700195 }
196
197}