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