blob: 9342721cb2ab8b4a306d9472225add8188202523 [file] [log] [blame]
Thomas Vachuska90b453f2015-01-30 18:57:14 -08001/*
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 */
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
Thomas Vachuska90b453f2015-01-30 18:57:14 -080046 // Fetch our service and feed it's offerings to the string completer
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070047 ApplicationService service = get(ApplicationService.class);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080048 Iterator<Application> it = service.getApplications().iterator();
49 SortedSet<String> strings = delegate.getStrings();
50 while (it.hasNext()) {
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070051 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 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080058 }
59
60 // Now let the completer do the work for figuring out what to offer.
61 return delegate.complete(buffer, cursor, candidates);
62 }
63
64}