blob: ab13db9344419c99451792bd2b242f79d400ad00 [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 java.util.Collections;
19import java.util.List;
20import java.util.Set;
21import java.util.stream.Collectors;
22
23import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList;
24import org.onosproject.cli.AbstractChoicesCompleter;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.config.NetworkConfigRegistry;
27import org.onosproject.net.config.SubjectFactory;
28
29/**
30 * Network configuration subject key completer.
31 *
32 * Assumes argument right before the one being completed is SubjectClassKey.
33 */
34public class SubjectKeyCompleter extends AbstractChoicesCompleter {
35
36 @Override
37 protected List<String> choices() {
38 NetworkConfigRegistry service = AbstractShellCommand.get(NetworkConfigRegistry.class);
39 ArgumentList args = getArgumentList();
40 String subjectClassKey = args.getArguments()[args.getCursorArgumentIndex() - 1];
41
42 SubjectFactory subjectFactory = service.getSubjectFactory(subjectClassKey);
43 if (subjectFactory == null) {
44 return Collections.emptyList();
45 }
46 // get all registered subject objects.
47 Set<Object> subjects = service.getSubjects(subjectFactory.subjectClass());
48 return subjects.stream().map(subjectFactory::subjectKey).collect(Collectors.toList());
49 }
50
51}