tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.cli; |
| 2 | |
| 3 | import org.apache.karaf.shell.console.Completer; |
| 4 | import org.apache.karaf.shell.console.completer.StringsCompleter; |
| 5 | import org.onlab.onos.cluster.ClusterService; |
| 6 | import org.onlab.onos.cluster.ControllerNode; |
| 7 | |
| 8 | import java.util.Iterator; |
| 9 | import java.util.List; |
| 10 | import java.util.SortedSet; |
| 11 | |
| 12 | /** |
| 13 | * Node ID completer. |
| 14 | */ |
| 15 | public class NodeIdCompleter implements Completer { |
| 16 | @Override |
| 17 | public int complete(String buffer, int cursor, List<String> candidates) { |
| 18 | // Delegate string completer |
| 19 | StringsCompleter delegate = new StringsCompleter(); |
| 20 | |
| 21 | // Fetch our service and feed it's offerings to the string completer |
| 22 | ClusterService service = AbstractShellCommand.get(ClusterService.class); |
| 23 | Iterator<ControllerNode> it = service.getNodes().iterator(); |
| 24 | SortedSet<String> strings = delegate.getStrings(); |
| 25 | while (it.hasNext()) { |
| 26 | strings.add(it.next().id().toString()); |
| 27 | } |
| 28 | |
| 29 | // Now let the completer do the work for figuring out what to offer. |
| 30 | return delegate.complete(buffer, cursor, candidates); |
| 31 | } |
| 32 | |
| 33 | } |