blob: 74bcdc27b9e4bc0a9968a0adb1a641df881afb84 [file] [log] [blame]
HIGUCHI Yuta4f074c02015-11-23 10:15:53 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkey0068fd02018-10-11 15:45:39 -070024import org.apache.karaf.shell.api.action.lifecycle.Service;
HIGUCHI Yuta4f074c02015-11-23 10:15:53 -080025import 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 */
Ray Milkey0068fd02018-10-11 15:45:39 -070039@Service
HIGUCHI Yuta4f074c02015-11-23 10:15:53 -080040public class ConfigKeyCompleter extends AbstractChoicesCompleter {
41
42 // FIXME ConfigKeyCompleter never gets called??
43 @Override
44 protected List<String> choices() {
45 NetworkConfigRegistry service = AbstractShellCommand.get(NetworkConfigRegistry.class);
HIGUCHI Yuta4f074c02015-11-23 10:15:53 -080046
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047 checkArgument(commandLine.getCursorArgumentIndex() >= 2);
48 String subjectClassKey = commandLine.getArguments()[commandLine.getCursorArgumentIndex() - 2];
HIGUCHI Yuta4f074c02015-11-23 10:15:53 -080049
50 SubjectFactory<?> subjectFactory = service.getSubjectFactory(subjectClassKey);
51 if (subjectFactory == null) {
52 return ImmutableList.of();
53 }
54
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055 String subjectKey = commandLine.getArguments()[commandLine.getCursorArgumentIndex() - 1];
HIGUCHI Yuta4f074c02015-11-23 10:15:53 -080056
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}