Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cli.cfg; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Option; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 21 | import org.onosproject.cfg.ComponentConfigService; |
| 22 | import org.onosproject.cfg.ConfigProperty; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 25 | import java.util.Optional; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 26 | import java.util.Set; |
| 27 | |
| 28 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 29 | |
| 30 | /** |
Jon Hall | 6148f36 | 2015-04-08 16:26:12 -0700 | [diff] [blame] | 31 | * Manages component configuration. |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 32 | */ |
| 33 | @Command(scope = "onos", name = "cfg", |
| 34 | description = "Manages component configuration") |
| 35 | public 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 Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 41 | 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 Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 47 | |
| 48 | @Argument(index = 0, name = "command", |
Jon Hall | 6148f36 | 2015-04-08 16:26:12 -0700 | [diff] [blame] | 49 | description = "Command name (get|set)", |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 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); |
Jon Hall | e3f0fcf | 2015-04-10 09:34:23 -0700 | [diff] [blame] | 98 | if (props == null) { |
| 99 | print("No properties for component " + component + " found"); |
| 100 | } else if (shortOnly) { |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 101 | 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 Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | private void listComponentProperty(String component, String name) { |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 109 | Set<ConfigProperty> props = service.getProperties(component); |
Ray Milkey | 1cdfd8e | 2015-04-10 15:20:03 -0700 | [diff] [blame^] | 110 | |
| 111 | if (props == null) { |
| 112 | return; |
| 113 | } |
| 114 | |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 115 | Optional<ConfigProperty> property = props.stream() |
| 116 | .filter(p -> p.name().equals(name)).findFirst(); |
| 117 | if (!property.isPresent()) { |
Jon Hall | e3f0fcf | 2015-04-10 09:34:23 -0700 | [diff] [blame] | 118 | print("Property " + name + " for component " + component + " not found"); |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 119 | 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 Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | } |