blob: 1479b9dcca40929a468631de4164019061199a16 [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
Brian O'Connore709a3b2015-04-14 15:02:27 -070018import com.google.common.collect.Sets;
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070019import org.apache.karaf.shell.console.completer.ArgumentCompleter;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080020import org.apache.karaf.shell.console.completer.StringsCompleter;
21import org.onosproject.app.ApplicationService;
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070022import org.onosproject.app.ApplicationState;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070023import org.onosproject.cli.AbstractCompleter;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080024import org.onosproject.core.Application;
25
Brian O'Connore709a3b2015-04-14 15:02:27 -070026import java.util.Arrays;
27import java.util.Collections;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080028import java.util.Iterator;
29import java.util.List;
Brian O'Connore709a3b2015-04-14 15:02:27 -070030import java.util.Set;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080031import java.util.SortedSet;
32
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070033import static org.onosproject.app.ApplicationState.ACTIVE;
34import static org.onosproject.app.ApplicationState.INSTALLED;
35import static org.onosproject.cli.AbstractShellCommand.get;
36
Thomas Vachuska90b453f2015-01-30 18:57:14 -080037/**
38 * Application name completer.
39 */
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070040public class ApplicationNameCompleter extends AbstractCompleter {
Thomas Vachuska90b453f2015-01-30 18:57:14 -080041 @Override
42 public int complete(String buffer, int cursor, List<String> candidates) {
43 // Delegate string completer
44 StringsCompleter delegate = new StringsCompleter();
45
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070046 // Command name is the second argument.
47 ArgumentCompleter.ArgumentList list = getArgumentList();
48 String cmd = list.getArguments()[1];
49
Brian O'Connore709a3b2015-04-14 15:02:27 -070050 // Grab apps already on the command (to prevent tab-completed duplicates)
51 final Set previousApps;
52 if (list.getArguments().length > 2) {
53 previousApps = Sets.newHashSet(
54 Arrays.copyOfRange(list.getArguments(), 2, list.getArguments().length));
55 } else {
56 previousApps = Collections.emptySet();
57 }
58
Thomas Vachuska90b453f2015-01-30 18:57:14 -080059 // Fetch our service and feed it's offerings to the string completer
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070060 ApplicationService service = get(ApplicationService.class);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080061 Iterator<Application> it = service.getApplications().iterator();
62 SortedSet<String> strings = delegate.getStrings();
63 while (it.hasNext()) {
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070064 Application app = it.next();
65 ApplicationState state = service.getState(app.id());
Brian O'Connore709a3b2015-04-14 15:02:27 -070066 if (previousApps.contains(app.id().name())) {
67 continue;
68 }
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070069 if (cmd.equals("uninstall") ||
70 (cmd.equals("activate") && state == INSTALLED) ||
71 (cmd.equals("deactivate") && state == ACTIVE)) {
72 strings.add(app.id().name());
73 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080074 }
75
76 // Now let the completer do the work for figuring out what to offer.
77 return delegate.complete(buffer, cursor, candidates);
78 }
79
80}