Ray Milkey | fb6655e | 2015-04-10 10:50:54 -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 java.util.List; |
Ray Milkey | 1cdfd8e | 2015-04-10 15:20:03 -0700 | [diff] [blame^] | 19 | import java.util.Set; |
Ray Milkey | fb6655e | 2015-04-10 10:50:54 -0700 | [diff] [blame] | 20 | import java.util.SortedSet; |
| 21 | |
| 22 | import org.apache.felix.service.command.CommandSession; |
| 23 | import org.apache.karaf.shell.console.CommandSessionHolder; |
| 24 | import org.apache.karaf.shell.console.Completer; |
| 25 | import org.apache.karaf.shell.console.completer.ArgumentCompleter; |
| 26 | import org.apache.karaf.shell.console.completer.StringsCompleter; |
| 27 | import org.onosproject.cfg.ComponentConfigService; |
Ray Milkey | 1cdfd8e | 2015-04-10 15:20:03 -0700 | [diff] [blame^] | 28 | import org.onosproject.cfg.ConfigProperty; |
Ray Milkey | fb6655e | 2015-04-10 10:50:54 -0700 | [diff] [blame] | 29 | import org.onosproject.cli.AbstractShellCommand; |
| 30 | |
| 31 | /** |
| 32 | * Component property name completer. |
| 33 | */ |
| 34 | public class ComponentPropertyNameCompleter implements Completer { |
| 35 | @Override |
| 36 | public int complete(String buffer, int cursor, List<String> candidates) { |
| 37 | // Delegate string completer |
| 38 | StringsCompleter delegate = new StringsCompleter(); |
| 39 | |
| 40 | CommandSession session = CommandSessionHolder.getSession(); |
| 41 | ArgumentCompleter.ArgumentList list = |
| 42 | (ArgumentCompleter.ArgumentList) session.get( |
| 43 | ArgumentCompleter.ARGUMENTS_LIST); |
| 44 | |
| 45 | // Component name is the previous argument. |
| 46 | String componentName = list.getArguments()[list.getCursorArgumentIndex() - 1]; |
| 47 | ComponentConfigService service = |
| 48 | AbstractShellCommand.get(ComponentConfigService.class); |
| 49 | |
| 50 | SortedSet<String> strings = delegate.getStrings(); |
Ray Milkey | 1cdfd8e | 2015-04-10 15:20:03 -0700 | [diff] [blame^] | 51 | Set<ConfigProperty> properties = |
| 52 | service.getProperties(componentName); |
| 53 | if (properties != null) { |
| 54 | properties.forEach(property -> strings.add(property.name())); |
| 55 | } |
Ray Milkey | fb6655e | 2015-04-10 10:50:54 -0700 | [diff] [blame] | 56 | |
| 57 | // Now let the completer do the work for figuring out what to offer. |
| 58 | return delegate.complete(buffer, cursor, candidates); |
| 59 | } |
| 60 | |
| 61 | } |