blob: f8d86f190cf29bd18285decd280c3028920ccac8 [file] [log] [blame]
Thomas Vachuska90b453f2015-01-30 18:57:14 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska90b453f2015-01-30 18:57:14 -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.app;
17
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070018import org.apache.karaf.shell.console.completer.ArgumentCompleter;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080019import org.apache.karaf.shell.console.completer.StringsCompleter;
20import org.onosproject.app.ApplicationService;
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070021import org.onosproject.app.ApplicationState;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070022import org.onosproject.cli.AbstractCompleter;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080023import org.onosproject.core.Application;
24
25import java.util.Iterator;
26import java.util.List;
27import java.util.SortedSet;
28
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070029import static org.onosproject.app.ApplicationState.ACTIVE;
30import static org.onosproject.app.ApplicationState.INSTALLED;
31import static org.onosproject.cli.AbstractShellCommand.get;
32
Thomas Vachuska90b453f2015-01-30 18:57:14 -080033/**
34 * Application name completer.
35 */
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070036public class ApplicationNameCompleter extends AbstractCompleter {
Thomas Vachuska90b453f2015-01-30 18:57:14 -080037 @Override
38 public int complete(String buffer, int cursor, List<String> candidates) {
39 // Delegate string completer
40 StringsCompleter delegate = new StringsCompleter();
41
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070042 // Command name is the second argument.
43 ArgumentCompleter.ArgumentList list = getArgumentList();
44 String cmd = list.getArguments()[1];
45
Brian O'Connore709a3b2015-04-14 15:02:27 -070046 // Grab apps already on the command (to prevent tab-completed duplicates)
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070047 // FIXME: This does not work.
48// final Set previousApps;
49// if (list.getArguments().length > 2) {
50// previousApps = Sets.newHashSet(
51// Arrays.copyOfRange(list.getArguments(), 2, list.getArguments().length));
52// } else {
53// previousApps = Collections.emptySet();
54// }
Brian O'Connore709a3b2015-04-14 15:02:27 -070055
Thomas Vachuska90b453f2015-01-30 18:57:14 -080056 // Fetch our service and feed it's offerings to the string completer
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070057 ApplicationService service = get(ApplicationService.class);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080058 Iterator<Application> it = service.getApplications().iterator();
59 SortedSet<String> strings = delegate.getStrings();
60 while (it.hasNext()) {
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070061 Application app = it.next();
62 ApplicationState state = service.getState(app.id());
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070063// if (previousApps.contains(app.id().name())) {
64// continue;
65// }
Jon Halla3fcf672017-03-28 16:53:22 -070066 if ("uninstall".equals(cmd) ||
67 ("activate".equals(cmd) && state == INSTALLED) ||
68 ("deactivate".equals(cmd) && state == ACTIVE)) {
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070069 strings.add(app.id().name());
70 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080071 }
72
73 // Now let the completer do the work for figuring out what to offer.
74 return delegate.complete(buffer, cursor, candidates);
75 }
76
77}