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; |
| 20 | import org.onosproject.cfg.ComponentConfigService; |
| 21 | import org.onosproject.cfg.ConfigProperty; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 27 | |
| 28 | /** |
| 29 | * Manages application inventory. |
| 30 | */ |
| 31 | @Command(scope = "onos", name = "cfg", |
| 32 | description = "Manages component configuration") |
| 33 | public class ComponentConfigCommand extends AbstractShellCommand { |
| 34 | |
| 35 | static final String GET = "get"; |
| 36 | static final String SET = "set"; |
| 37 | |
| 38 | private static final String FMT = " name=%s, type=%s, value=%s, defaultValue=%s, description=%s"; |
| 39 | |
| 40 | @Argument(index = 0, name = "command", |
| 41 | description = "Command name (activate|deactivate|uninstall)", |
| 42 | required = false, multiValued = false) |
| 43 | String command = null; |
| 44 | |
| 45 | @Argument(index = 1, name = "component", description = "Component name", |
| 46 | required = false, multiValued = false) |
| 47 | String component = null; |
| 48 | |
| 49 | @Argument(index = 2, name = "name", description = "Property name", |
| 50 | required = false, multiValued = false) |
| 51 | String name = null; |
| 52 | |
| 53 | @Argument(index = 3, name = "value", description = "Property value", |
| 54 | required = false, multiValued = false) |
| 55 | String value = null; |
| 56 | |
| 57 | ComponentConfigService service; |
| 58 | |
| 59 | @Override |
| 60 | protected void execute() { |
| 61 | service = get(ComponentConfigService.class); |
| 62 | if (isNullOrEmpty(command)) { |
| 63 | listComponents(); |
| 64 | } else if (command.equals(GET) && isNullOrEmpty(component)) { |
| 65 | listAllComponentsProperties(); |
| 66 | } else if (command.equals(GET) && isNullOrEmpty(name)) { |
| 67 | listComponentProperties(component); |
| 68 | } else if (command.equals(GET)) { |
| 69 | listComponentProperty(component, name); |
| 70 | } else if (command.equals(SET) && isNullOrEmpty(value)) { |
| 71 | service.unsetProperty(component, name); |
| 72 | } else if (command.equals(SET)) { |
| 73 | service.setProperty(component, name, value); |
| 74 | } else { |
| 75 | error("Illegal usage"); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private void listAllComponentsProperties() { |
| 80 | service.getComponentNames().forEach(this::listComponentProperties); |
| 81 | } |
| 82 | |
| 83 | private void listComponents() { |
| 84 | service.getComponentNames().forEach(n -> print("%s", n)); |
| 85 | } |
| 86 | |
| 87 | private void listComponentProperties(String component) { |
| 88 | Set<ConfigProperty> props = service.getProperties(component); |
| 89 | print("%s", component); |
| 90 | props.forEach(p -> print(FMT, p.name(), p.type().toString().toLowerCase(), |
| 91 | p.value(), p.defaultValue(), p.description())); |
| 92 | } |
| 93 | |
| 94 | private void listComponentProperty(String component, String name) { |
| 95 | // FIXME: implement after getProperty is defined and implemented |
| 96 | } |
| 97 | |
| 98 | } |