blob: 5b72493efc7f70e0933833b50be0da6c75438a3f [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;
Ray Milkey1cdfd8e2015-04-10 15:20:03 -070019import java.util.Set;
Ray Milkeyfb6655e2015-04-10 10:50:54 -070020import java.util.SortedSet;
21
22import org.apache.felix.service.command.CommandSession;
23import org.apache.karaf.shell.console.CommandSessionHolder;
24import org.apache.karaf.shell.console.Completer;
25import org.apache.karaf.shell.console.completer.ArgumentCompleter;
26import org.apache.karaf.shell.console.completer.StringsCompleter;
27import org.onosproject.cfg.ComponentConfigService;
Ray Milkey1cdfd8e2015-04-10 15:20:03 -070028import org.onosproject.cfg.ConfigProperty;
Ray Milkeyfb6655e2015-04-10 10:50:54 -070029import org.onosproject.cli.AbstractShellCommand;
30
31/**
32 * Component property name completer.
33 */
34public 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 Milkey1cdfd8e2015-04-10 15:20:03 -070051 Set<ConfigProperty> properties =
52 service.getProperties(componentName);
53 if (properties != null) {
54 properties.forEach(property -> strings.add(property.name()));
55 }
Ray Milkeyfb6655e2015-04-10 10:50:54 -070056
57 // Now let the completer do the work for figuring out what to offer.
58 return delegate.complete(buffer, cursor, candidates);
59 }
60
61}