Thomas Vachuska

Enhanced application name completer to consider the command context.

Change-Id: Ib77a53830d4b6d14ef99115309ea25707a8281b4
...@@ -15,31 +15,46 @@ ...@@ -15,31 +15,46 @@
15 */ 15 */
16 package org.onosproject.cli.app; 16 package org.onosproject.cli.app;
17 17
18 -import org.apache.karaf.shell.console.Completer; 18 +import org.apache.karaf.shell.console.completer.ArgumentCompleter;
19 import org.apache.karaf.shell.console.completer.StringsCompleter; 19 import org.apache.karaf.shell.console.completer.StringsCompleter;
20 import org.onosproject.app.ApplicationService; 20 import org.onosproject.app.ApplicationService;
21 -import org.onosproject.cli.AbstractShellCommand; 21 +import org.onosproject.app.ApplicationState;
22 +import org.onosproject.cli.net.AbstractCompleter;
22 import org.onosproject.core.Application; 23 import org.onosproject.core.Application;
23 24
24 import java.util.Iterator; 25 import java.util.Iterator;
25 import java.util.List; 26 import java.util.List;
26 import java.util.SortedSet; 27 import java.util.SortedSet;
27 28
29 +import static org.onosproject.app.ApplicationState.ACTIVE;
30 +import static org.onosproject.app.ApplicationState.INSTALLED;
31 +import static org.onosproject.cli.AbstractShellCommand.get;
32 +
28 /** 33 /**
29 * Application name completer. 34 * Application name completer.
30 */ 35 */
31 -public class ApplicationNameCompleter implements Completer { 36 +public class ApplicationNameCompleter extends AbstractCompleter {
32 @Override 37 @Override
33 public int complete(String buffer, int cursor, List<String> candidates) { 38 public int complete(String buffer, int cursor, List<String> candidates) {
34 // Delegate string completer 39 // Delegate string completer
35 StringsCompleter delegate = new StringsCompleter(); 40 StringsCompleter delegate = new StringsCompleter();
36 41
42 + // Command name is the second argument.
43 + ArgumentCompleter.ArgumentList list = getArgumentList();
44 + String cmd = list.getArguments()[1];
45 +
37 // Fetch our service and feed it's offerings to the string completer 46 // Fetch our service and feed it's offerings to the string completer
38 - ApplicationService service = AbstractShellCommand.get(ApplicationService.class); 47 + ApplicationService service = get(ApplicationService.class);
39 Iterator<Application> it = service.getApplications().iterator(); 48 Iterator<Application> it = service.getApplications().iterator();
40 SortedSet<String> strings = delegate.getStrings(); 49 SortedSet<String> strings = delegate.getStrings();
41 while (it.hasNext()) { 50 while (it.hasNext()) {
42 - strings.add(it.next().id().name()); 51 + Application app = it.next();
52 + ApplicationState state = service.getState(app.id());
53 + if (cmd.equals("uninstall") ||
54 + (cmd.equals("activate") && state == INSTALLED) ||
55 + (cmd.equals("deactivate") && state == ACTIVE)) {
56 + strings.add(app.id().name());
57 + }
43 } 58 }
44 59
45 // Now let the completer do the work for figuring out what to offer. 60 // Now let the completer do the work for figuring out what to offer.
......
...@@ -15,41 +15,35 @@ ...@@ -15,41 +15,35 @@
15 */ 15 */
16 package org.onosproject.cli.cfg; 16 package org.onosproject.cli.cfg;
17 17
18 -import java.util.List;
19 -import java.util.Set;
20 -import java.util.SortedSet;
21 -
22 -import org.apache.felix.service.command.CommandSession;
23 -import org.apache.karaf.shell.console.CommandSessionHolder;
24 -import org.apache.karaf.shell.console.Completer;
25 import org.apache.karaf.shell.console.completer.ArgumentCompleter; 18 import org.apache.karaf.shell.console.completer.ArgumentCompleter;
26 import org.apache.karaf.shell.console.completer.StringsCompleter; 19 import org.apache.karaf.shell.console.completer.StringsCompleter;
27 import org.onosproject.cfg.ComponentConfigService; 20 import org.onosproject.cfg.ComponentConfigService;
28 import org.onosproject.cfg.ConfigProperty; 21 import org.onosproject.cfg.ConfigProperty;
29 -import org.onosproject.cli.AbstractShellCommand; 22 +import org.onosproject.cli.net.AbstractCompleter;
23 +
24 +import java.util.List;
25 +import java.util.Set;
26 +import java.util.SortedSet;
27 +
28 +import static org.onosproject.cli.AbstractShellCommand.get;
30 29
31 /** 30 /**
32 * Component property name completer. 31 * Component property name completer.
33 */ 32 */
34 -public class ComponentPropertyNameCompleter implements Completer { 33 +public class ComponentPropertyNameCompleter extends AbstractCompleter {
35 @Override 34 @Override
36 public int complete(String buffer, int cursor, List<String> candidates) { 35 public int complete(String buffer, int cursor, List<String> candidates) {
37 // Delegate string completer 36 // Delegate string completer
38 StringsCompleter delegate = new StringsCompleter(); 37 StringsCompleter delegate = new StringsCompleter();
39 38
40 - CommandSession session = CommandSessionHolder.getSession();
41 - ArgumentCompleter.ArgumentList list =
42 - (ArgumentCompleter.ArgumentList) session.get(
43 - ArgumentCompleter.ARGUMENTS_LIST);
44 -
45 // Component name is the previous argument. 39 // Component name is the previous argument.
40 + ArgumentCompleter.ArgumentList list = getArgumentList();
46 String componentName = list.getArguments()[list.getCursorArgumentIndex() - 1]; 41 String componentName = list.getArguments()[list.getCursorArgumentIndex() - 1];
47 - ComponentConfigService service = 42 + ComponentConfigService service = get(ComponentConfigService.class);
48 - AbstractShellCommand.get(ComponentConfigService.class);
49 43
50 SortedSet<String> strings = delegate.getStrings(); 44 SortedSet<String> strings = delegate.getStrings();
51 Set<ConfigProperty> properties = 45 Set<ConfigProperty> properties =
52 - service.getProperties(componentName); 46 + service.getProperties(componentName);
53 if (properties != null) { 47 if (properties != null) {
54 properties.forEach(property -> strings.add(property.name())); 48 properties.forEach(property -> strings.add(property.name()));
55 } 49 }
......
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.net;
17 +
18 +import org.apache.felix.service.command.CommandSession;
19 +import org.apache.karaf.shell.console.CommandSessionHolder;
20 +import org.apache.karaf.shell.console.Completer;
21 +import org.apache.karaf.shell.console.completer.ArgumentCompleter;
22 +
23 +/**
24 + * Abstract argument completer.
25 + */
26 +public abstract class AbstractCompleter implements Completer {
27 +
28 + /**
29 + * Returns the argument list.
30 + *
31 + * @return argument list
32 + */
33 + protected ArgumentCompleter.ArgumentList getArgumentList() {
34 + CommandSession session = CommandSessionHolder.getSession();
35 + return (ArgumentCompleter.ArgumentList)
36 + session.get(ArgumentCompleter.ARGUMENTS_LIST);
37 + }
38 +
39 +}