blob: e580503cd40848080c89d71fd4e666f86935d6fa [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/**
Jon Hall6148f362015-04-08 16:26:12 -070031 * Manages component configuration.
Thomas Vachuska6d697f12015-03-08 20:59:50 -070032 */
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",
Jon Hall6148f362015-04-08 16:26:12 -070049 description = "Command name (get|set)",
Thomas Vachuska6d697f12015-03-08 20:59:50 -070050 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);
Jon Halle3f0fcf2015-04-10 09:34:23 -070098 if (props == null) {
99 print("No properties for component " + component + " found");
100 } else if (shortOnly) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700101 props.forEach(p -> print(SHORT_FMT, p.name(), p.value()));
102 } else {
103 props.forEach(p -> print(FMT, p.name(), p.type().toString().toLowerCase(),
104 p.value(), p.defaultValue(), p.description()));
105 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700106 }
107
108 private void listComponentProperty(String component, String name) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700109 Set<ConfigProperty> props = service.getProperties(component);
Ray Milkey1cdfd8e2015-04-10 15:20:03 -0700110
111 if (props == null) {
112 return;
113 }
114
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700115 Optional<ConfigProperty> property = props.stream()
116 .filter(p -> p.name().equals(name)).findFirst();
117 if (!property.isPresent()) {
Jon Halle3f0fcf2015-04-10 09:34:23 -0700118 print("Property " + name + " for component " + component + " not found");
Thomas Vachuska8ceff302015-04-03 11:45:53 -0700119 return;
120 }
121 ConfigProperty p = property.get();
122 if (shortOnly) {
123 print(SHORT_FMT, p.name(), p.value());
124 } else {
125 print(FMT, p.name(), p.type().toString().toLowerCase(), p.value(),
126 p.defaultValue(), p.description());
127 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700128 }
129
130}