HIGUCHI Yuta | 4f074c0 | 2015-11-23 10:15:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
HIGUCHI Yuta | 4f074c0 | 2015-11-23 10:15:53 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.cli.cfg; |
| 17 | |
| 18 | import java.util.Collections; |
| 19 | import java.util.List; |
| 20 | import java.util.Set; |
| 21 | import java.util.stream.Collectors; |
| 22 | |
HIGUCHI Yuta | 4f074c0 | 2015-11-23 10:15:53 -0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractChoicesCompleter; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 26 | import org.onosproject.net.config.SubjectFactory; |
| 27 | |
| 28 | /** |
| 29 | * Network configuration subject key completer. |
| 30 | * |
| 31 | * Assumes argument right before the one being completed is SubjectClassKey. |
| 32 | */ |
| 33 | public class SubjectKeyCompleter extends AbstractChoicesCompleter { |
| 34 | |
| 35 | @Override |
| 36 | protected List<String> choices() { |
| 37 | NetworkConfigRegistry service = AbstractShellCommand.get(NetworkConfigRegistry.class); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 38 | String subjectClassKey = commandLine.getArguments()[commandLine.getCursorArgumentIndex() - 1]; |
HIGUCHI Yuta | 4f074c0 | 2015-11-23 10:15:53 -0800 | [diff] [blame] | 39 | |
| 40 | SubjectFactory subjectFactory = service.getSubjectFactory(subjectClassKey); |
| 41 | if (subjectFactory == null) { |
| 42 | return Collections.emptyList(); |
| 43 | } |
| 44 | // get all registered subject objects. |
| 45 | Set<Object> subjects = service.getSubjects(subjectFactory.subjectClass()); |
| 46 | return subjects.stream().map(subjectFactory::subjectKey).collect(Collectors.toList()); |
| 47 | } |
| 48 | |
| 49 | } |