Committed by
Yuta HIGUCHI
Completers for netcfg command
Change-Id: I925825f3ddb3ffde8fb59d8d0d4ccb9faf92fe37
Showing
4 changed files
with
163 additions
and
0 deletions
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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 static com.google.common.base.Preconditions.checkArgument; | ||
19 | + | ||
20 | +import java.util.List; | ||
21 | +import java.util.Set; | ||
22 | +import java.util.stream.Collectors; | ||
23 | + | ||
24 | +import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList; | ||
25 | +import org.onosproject.cli.AbstractChoicesCompleter; | ||
26 | +import org.onosproject.cli.AbstractShellCommand; | ||
27 | +import org.onosproject.net.config.Config; | ||
28 | +import org.onosproject.net.config.NetworkConfigRegistry; | ||
29 | +import org.onosproject.net.config.SubjectFactory; | ||
30 | + | ||
31 | +import 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 | + */ | ||
39 | +public 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 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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.List; | ||
19 | +import java.util.stream.Collectors; | ||
20 | + | ||
21 | +import org.onosproject.cli.AbstractChoicesCompleter; | ||
22 | +import org.onosproject.cli.AbstractShellCommand; | ||
23 | +import org.onosproject.net.config.NetworkConfigRegistry; | ||
24 | +import org.onosproject.net.config.SubjectFactory; | ||
25 | + | ||
26 | +/** | ||
27 | + * Network configuration subject class key completer. | ||
28 | + */ | ||
29 | +public class SubjectClassKeyCompleter extends AbstractChoicesCompleter { | ||
30 | + | ||
31 | + @Override | ||
32 | + protected List<String> choices() { | ||
33 | + NetworkConfigRegistry service = AbstractShellCommand.get(NetworkConfigRegistry.class); | ||
34 | + return service.getSubjectClasses() | ||
35 | + .stream() | ||
36 | + .map(service::getSubjectFactory) | ||
37 | + .map(SubjectFactory::subjectClassKey) | ||
38 | + .collect(Collectors.toList()); | ||
39 | + } | ||
40 | + | ||
41 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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 | + | ||
23 | +import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList; | ||
24 | +import org.onosproject.cli.AbstractChoicesCompleter; | ||
25 | +import org.onosproject.cli.AbstractShellCommand; | ||
26 | +import org.onosproject.net.config.NetworkConfigRegistry; | ||
27 | +import 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 | + */ | ||
34 | +public 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 | +} |
... | @@ -57,6 +57,11 @@ | ... | @@ -57,6 +57,11 @@ |
57 | 57 | ||
58 | <command> | 58 | <command> |
59 | <action class="org.onosproject.cli.cfg.NetworkConfigCommand"/> | 59 | <action class="org.onosproject.cli.cfg.NetworkConfigCommand"/> |
60 | + <completers> | ||
61 | + <ref component-id="subjectClassKeyCompleter"/> | ||
62 | + <ref component-id="subjectKeyCompleter"/> | ||
63 | + <ref component-id="configKeyCompleter"/> | ||
64 | + </completers> | ||
60 | </command> | 65 | </command> |
61 | 66 | ||
62 | <command> | 67 | <command> |
... | @@ -532,4 +537,8 @@ | ... | @@ -532,4 +537,8 @@ |
532 | 537 | ||
533 | <bean id="placeholderCompleter" class="org.onosproject.cli.PlaceholderCompleter"/> | 538 | <bean id="placeholderCompleter" class="org.onosproject.cli.PlaceholderCompleter"/> |
534 | 539 | ||
540 | + <bean id="subjectClassKeyCompleter" class="org.onosproject.cli.cfg.SubjectClassKeyCompleter"/> | ||
541 | + <bean id="subjectKeyCompleter" class="org.onosproject.cli.cfg.SubjectKeyCompleter"/> | ||
542 | + <bean id="configKeyCompleter" class="org.onosproject.cli.cfg.ConfigKeyCompleter"/> | ||
543 | + | ||
535 | </blueprint> | 544 | </blueprint> | ... | ... |
-
Please register or login to post a comment