blob: de73c91f5247279a5287328a8de6ada1460471be [file] [log] [blame]
Thomas Vachuska6d697f12015-03-08 20:59:50 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
Thomas Vachuska8ceff302015-04-03 11:45:53 -070020import org.apache.karaf.shell.commands.Option;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070021import org.onosproject.cfg.ComponentConfigService;
22import org.onosproject.cfg.ConfigProperty;
23import org.onosproject.cli.AbstractShellCommand;
24
Thomas Vachuska8ceff302015-04-03 11:45:53 -070025import java.util.Optional;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070026import java.util.Set;
27
28import static com.google.common.base.Strings.isNullOrEmpty;
29
30/**
31 * Manages application inventory.
32 */
33@Command(scope = "onos", name = "cfg",
34 description = "Manages component configuration")
35public class ComponentConfigCommand extends AbstractShellCommand {
36
37 static final String GET = "get";
38 static final String SET = "set";
39
40 private static final String FMT = " name=%s, type=%s, value=%s, defaultValue=%s, description=%s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070041 private static final String SHORT_FMT = " %s=%s";
42
43 @Option(name = "-s", aliases = "--short", description = "Show short output only",
44 required = false, multiValued = false)
45 private boolean shortOnly = false;
46
Thomas Vachuska6d697f12015-03-08 20:59:50 -070047
48 @Argument(index = 0, name = "command",
49 description = "Command name (activate|deactivate|uninstall)",
50 required = false, multiValued = false)
51 String command = null;
52
53 @Argument(index = 1, name = "component", description = "Component name",
54 required = false, multiValued = false)
55 String component = null;
56
57 @Argument(index = 2, name = "name", description = "Property name",
58 required = false, multiValued = false)
59 String name = null;
60
61 @Argument(index = 3, name = "value", description = "Property value",
62 required = false, multiValued = false)
63 String value = null;
64
65 ComponentConfigService service;
66
67 @Override
68 protected void execute() {
69 service = get(ComponentConfigService.class);
70 if (isNullOrEmpty(command)) {
71 listComponents();
72 } else if (command.equals(GET) && isNullOrEmpty(component)) {
73 listAllComponentsProperties();
74 } else if (command.equals(GET) && isNullOrEmpty(name)) {
75 listComponentProperties(component);
76 } else if (command.equals(GET)) {
77 listComponentProperty(component, name);
78 } else if (command.equals(SET) && isNullOrEmpty(value)) {
79 service.unsetProperty(component, name);
80 } else if (command.equals(SET)) {
81 service.setProperty(component, name, value);
82 } else {
83 error("Illegal usage");
84 }
85 }
86
87 private void listAllComponentsProperties() {
88 service.getComponentNames().forEach(this::listComponentProperties);
89 }
90
91 private void listComponents() {
92 service.getComponentNames().forEach(n -> print("%s", n));
93 }
94
95 private void listComponentProperties(String component) {
96 Set<ConfigProperty> props = service.getProperties(component);
97 print("%s", component);
Thomas Vachuska8ceff302015-04-03 11:45:53 -070098 if (shortOnly) {
99 props.forEach(p -> print(SHORT_FMT, p.name(), p.value()));
100 } else {
101 props.forEach(p -> print(FMT, p.name(), p.type().toString().toLowerCase(),
102 p.value(), p.defaultValue(), p.description()));
103 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700104 }
105
106 private void listComponentProperty(String component, String name) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700107 Set<ConfigProperty> props = service.getProperties(component);
108 Optional<ConfigProperty> property = props.stream()
109 .filter(p -> p.name().equals(name)).findFirst();
110 if (!property.isPresent()) {
111 System.err.println("Property " + name + " for component " + component + " not found");
112 return;
113 }
114 ConfigProperty p = property.get();
115 if (shortOnly) {
116 print(SHORT_FMT, p.name(), p.value());
117 } else {
118 print(FMT, p.name(), p.type().toString().toLowerCase(), p.value(),
119 p.defaultValue(), p.description());
120 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700121 }
122
123}