blob: 2ac3a1164e18b16a9fbb8089c2b614dc58a47dea [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;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070034
35import static com.google.common.base.Strings.isNullOrEmpty;
36
37/**
Jon Hall6148f362015-04-08 16:26:12 -070038 * Manages component configuration.
Thomas Vachuska6d697f12015-03-08 20:59:50 -070039 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
Thomas Vachuska6d697f12015-03-08 20:59:50 -070041@Command(scope = "onos", name = "cfg",
42 description = "Manages component configuration")
43public class ComponentConfigCommand extends AbstractShellCommand {
44
45 static final String GET = "get";
46 static final String SET = "set";
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080047 static final String PRESET = "preset";
Thomas Vachuska6d697f12015-03-08 20:59:50 -070048
49 private static final String FMT = " name=%s, type=%s, value=%s, defaultValue=%s, description=%s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070050 private static final String SHORT_FMT = " %s=%s";
51
52 @Option(name = "-s", aliases = "--short", description = "Show short output only",
53 required = false, multiValued = false)
54 private boolean shortOnly = false;
55
Thomas Vachuska6d697f12015-03-08 20:59:50 -070056
57 @Argument(index = 0, name = "command",
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080058 description = "Command name (get|set|preset)",
Thomas Vachuska6d697f12015-03-08 20:59:50 -070059 required = false, multiValued = false)
Ray Milkeyae965fe2018-10-23 14:20:10 -070060 @Completion(ComponentConfigCommandCompleter.class)
Thomas Vachuska6d697f12015-03-08 20:59:50 -070061 String command = null;
62
63 @Argument(index = 1, name = "component", description = "Component name",
64 required = false, multiValued = false)
Ray Milkeyae965fe2018-10-23 14:20:10 -070065 @Completion(ComponentNameCompleter.class)
Thomas Vachuska6d697f12015-03-08 20:59:50 -070066 String component = null;
67
68 @Argument(index = 2, name = "name", description = "Property name",
69 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070070 @Completion(ComponentPropertyNameCompleter.class)
Thomas Vachuska6d697f12015-03-08 20:59:50 -070071 String name = null;
72
73 @Argument(index = 3, name = "value", description = "Property value",
74 required = false, multiValued = false)
75 String value = null;
76
77 ComponentConfigService service;
78
79 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070080 protected void doExecute() {
Thomas Vachuska6d697f12015-03-08 20:59:50 -070081 service = get(ComponentConfigService.class);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053082 try {
83 if (isNullOrEmpty(command)) {
84 listComponents();
85 } else if (command.equals(GET) && isNullOrEmpty(component)) {
86 listAllComponentsProperties();
87 } else if (command.equals(GET) && isNullOrEmpty(name)) {
88 listComponentProperties(component);
89 } else if (command.equals(GET)) {
90 listComponentProperty(component, name);
91 } else if (command.equals(SET) && isNullOrEmpty(value)) {
92 service.unsetProperty(component, name);
93 } else if (command.equals(SET)) {
94 service.setProperty(component, name, value);
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -080095 } else if (command.equals(PRESET)) {
96 service.preSetProperty(component, name, value);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053097 } else {
98 error("Illegal usage");
99 }
100 } catch (IllegalArgumentException e) {
101 error(e.getMessage());
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700102 }
103 }
104
105 private void listAllComponentsProperties() {
Ray Milkeyb0352412015-04-13 08:31:04 -0700106 if (outputJson()) {
107 print("%s", jsonComponentProperties());
108 } else {
109 service.getComponentNames().forEach(this::listComponentProperties);
110 }
111 }
112
113 private JsonNode jsonProperty(ConfigProperty configProperty, ObjectMapper mapper) {
114 return mapper.createObjectNode()
115 .put("name", configProperty.name())
116 .put("type", configProperty.type().toString().toLowerCase())
117 .put("value", configProperty.value())
118 .put("defaultValue", configProperty.defaultValue())
119 .put("description", configProperty.description());
120 }
121
122 private JsonNode jsonComponent(String component, ObjectMapper mapper) {
123 ObjectNode node = mapper.createObjectNode()
124 .put("componentName", component);
125 final ArrayNode propertiesJson = node.putArray("properties");
126 Set<ConfigProperty> properties = service.getProperties(component);
127 if (properties != null) {
128 properties.forEach(configProperty -> propertiesJson.add(
129 jsonProperty(configProperty, mapper)));
130 }
131 return node;
132 }
133
134 private JsonNode jsonComponentProperties() {
135 ObjectMapper mapper = new ObjectMapper();
136 ArrayNode result = mapper.createArrayNode();
137 service.getComponentNames()
138 .forEach(component -> result.add(jsonComponent(component, mapper)));
139
140 return result;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700141 }
142
143 private void listComponents() {
Ray Milkeyb0352412015-04-13 08:31:04 -0700144 if (outputJson()) {
145 ArrayNode node = new ObjectMapper().createArrayNode();
146 service.getComponentNames().forEach(node::add);
147 print("%s", node);
148 } else {
149 service.getComponentNames().forEach(n -> print("%s", n));
150 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700151 }
152
153 private void listComponentProperties(String component) {
Ray Milkeyb0352412015-04-13 08:31:04 -0700154 if (outputJson()) {
155 print("%s", jsonComponent(component, new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700156 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700157 Set<ConfigProperty> props = service.getProperties(component);
158 print("%s", component);
159 if (props == null) {
160 print("No properties for component " + component + " found");
161 } else if (shortOnly) {
162 props.forEach(p -> print(SHORT_FMT, p.name(), p.value()));
163 } else {
164 props.forEach(p -> print(FMT, p.name(), p.type().toString().toLowerCase(),
165 p.value(), p.defaultValue(), p.description()));
166 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700167 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700168 }
169
170 private void listComponentProperty(String component, String name) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700171 Set<ConfigProperty> props = service.getProperties(component);
Ray Milkey1cdfd8e2015-04-10 15:20:03 -0700172
173 if (props == null) {
174 return;
175 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700176 Optional<ConfigProperty> property = props.stream()
177 .filter(p -> p.name().equals(name)).findFirst();
Ray Milkeyb0352412015-04-13 08:31:04 -0700178 if (outputJson()) {
179 print("%s", jsonProperty(property.get(), new ObjectMapper()));
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700180 } else {
Ray Milkeyb0352412015-04-13 08:31:04 -0700181 if (!property.isPresent()) {
182 print("Property " + name + " for component " + component + " not found");
183 return;
184 }
185 ConfigProperty p = property.get();
186 if (shortOnly) {
187 print(SHORT_FMT, p.name(), p.value());
188 } else {
189 print(FMT, p.name(), p.type().toString().toLowerCase(), p.value(),
190 p.defaultValue(), p.description());
191 }
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700192 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700193 }
194
195}