blob: 4d6161a771c45b166f3ec23d3f0f9ea00fde6cd5 [file] [log] [blame]
Ray Milkeyfb6655e2015-04-10 10:50:54 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyfb6655e2015-04-10 10:50:54 -07003 *
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.console.CommandLine;
19import org.apache.karaf.shell.api.console.Session;
20import org.apache.karaf.shell.support.completers.StringsCompleter;
Ray Milkeyfb6655e2015-04-10 10:50:54 -070021import org.onosproject.cfg.ComponentConfigService;
Ray Milkey1cdfd8e2015-04-10 15:20:03 -070022import org.onosproject.cfg.ConfigProperty;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070023import org.onosproject.cli.AbstractCompleter;
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070024
25import java.util.List;
26import java.util.Set;
27import java.util.SortedSet;
28
29import static org.onosproject.cli.AbstractShellCommand.get;
Ray Milkeyfb6655e2015-04-10 10:50:54 -070030
31/**
32 * Component property name completer.
33 */
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070034public class ComponentPropertyNameCompleter extends AbstractCompleter {
Ray Milkeyfb6655e2015-04-10 10:50:54 -070035 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
Ray Milkeyfb6655e2015-04-10 10:50:54 -070037 // Delegate string completer
38 StringsCompleter delegate = new StringsCompleter();
39
Ray Milkeyfb6655e2015-04-10 10:50:54 -070040 // Component name is the previous argument.
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041 String componentName = commandLine.getArguments()[commandLine.getCursorArgumentIndex() - 1];
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070042 ComponentConfigService service = get(ComponentConfigService.class);
Ray Milkeyfb6655e2015-04-10 10:50:54 -070043
44 SortedSet<String> strings = delegate.getStrings();
Ray Milkey1cdfd8e2015-04-10 15:20:03 -070045 Set<ConfigProperty> properties =
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070046 service.getProperties(componentName);
Ray Milkey1cdfd8e2015-04-10 15:20:03 -070047 if (properties != null) {
48 properties.forEach(property -> strings.add(property.name()));
49 }
Ray Milkeyfb6655e2015-04-10 10:50:54 -070050
51 // Now let the completer do the work for figuring out what to offer.
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 return delegate.complete(session, commandLine, candidates);
Ray Milkeyfb6655e2015-04-10 10:50:54 -070053 }
54
55}