blob: 8294c8a331c4720161cee7f034d22aab2546c341 [file] [log] [blame]
HIGUCHI Yuta4f074c02015-11-23 10:15:53 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
HIGUCHI Yuta4f074c02015-11-23 10:15:53 -08003 *
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 static com.google.common.base.Preconditions.checkArgument;
19
20import java.util.List;
21import java.util.Set;
22import java.util.stream.Collectors;
23
24import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList;
25import org.onosproject.cli.AbstractChoicesCompleter;
26import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.net.config.Config;
28import org.onosproject.net.config.NetworkConfigRegistry;
29import org.onosproject.net.config.SubjectFactory;
30
31import com.google.common.collect.ImmutableList;
32
33/**
34 * Network configuration config key completer.
35 *
36 * Assumes 2 argument before the one being completed is SubjectClassKey
37 * and argument right before the one being completed is SubjectKey.
38 */
39public class ConfigKeyCompleter extends AbstractChoicesCompleter {
40
41 // FIXME ConfigKeyCompleter never gets called??
42 @Override
43 protected List<String> choices() {
44 NetworkConfigRegistry service = AbstractShellCommand.get(NetworkConfigRegistry.class);
45 ArgumentList args = getArgumentList();
46
47 checkArgument(args.getCursorArgumentIndex() >= 2);
48 String subjectClassKey = args.getArguments()[args.getCursorArgumentIndex() - 2];
49
50 SubjectFactory<?> subjectFactory = service.getSubjectFactory(subjectClassKey);
51 if (subjectFactory == null) {
52 return ImmutableList.of();
53 }
54
55 String subjectKey = args.getArguments()[args.getCursorArgumentIndex() - 1];
56
57 Object subject = subjectFactory.createSubject(subjectKey);
58 Set<? extends Config<Object>> configs = service.getConfigs(subject);
59 return configs.stream().map(Config::key).collect(Collectors.toList());
60 }
61
62}