blob: 3f93cedc24ca0ca2705701c3a6040338fb619f33 [file] [log] [blame]
Thomas Vachuska90b453f2015-01-30 18:57:14 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070025import com.google.common.base.Strings;
26import com.google.common.collect.Lists;
27
28import java.util.ArrayDeque;
29import java.util.ArrayList;
30import java.util.Deque;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080031import java.util.Iterator;
32import java.util.List;
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070033import java.util.Map.Entry;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080034import java.util.SortedSet;
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070035import java.util.stream.Collectors;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080036
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070037import static java.util.Arrays.asList;
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070038import static org.onosproject.app.ApplicationState.ACTIVE;
39import static org.onosproject.app.ApplicationState.INSTALLED;
40import static org.onosproject.cli.AbstractShellCommand.get;
41
Thomas Vachuska90b453f2015-01-30 18:57:14 -080042/**
43 * Application name completer.
44 */
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070045public class ApplicationNameCompleter extends AbstractCompleter {
Thomas Vachuska90b453f2015-01-30 18:57:14 -080046 @Override
47 public int complete(String buffer, int cursor, List<String> candidates) {
48 // Delegate string completer
49 StringsCompleter delegate = new StringsCompleter();
50
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070051 // Command name is the second argument.
52 ArgumentCompleter.ArgumentList list = getArgumentList();
53 String cmd = list.getArguments()[1];
54
Brian O'Connore709a3b2015-04-14 15:02:27 -070055 // Grab apps already on the command (to prevent tab-completed duplicates)
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070056 // FIXME: This does not work.
57// final Set previousApps;
58// if (list.getArguments().length > 2) {
59// previousApps = Sets.newHashSet(
60// Arrays.copyOfRange(list.getArguments(), 2, list.getArguments().length));
61// } else {
62// previousApps = Collections.emptySet();
63// }
Brian O'Connore709a3b2015-04-14 15:02:27 -070064
Thomas Vachuska90b453f2015-01-30 18:57:14 -080065 // Fetch our service and feed it's offerings to the string completer
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070066 ApplicationService service = get(ApplicationService.class);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080067 Iterator<Application> it = service.getApplications().iterator();
68 SortedSet<String> strings = delegate.getStrings();
69 while (it.hasNext()) {
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070070 Application app = it.next();
71 ApplicationState state = service.getState(app.id());
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070072// if (previousApps.contains(app.id().name())) {
73// continue;
74// }
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070075 if ("uninstall".equals(cmd) || "download".equals(cmd) ||
Jon Halla3fcf672017-03-28 16:53:22 -070076 ("activate".equals(cmd) && state == INSTALLED) ||
77 ("deactivate".equals(cmd) && state == ACTIVE)) {
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070078 strings.add(app.id().name());
79 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080080 }
81
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070082 // add unique suffix to candidates, if user has something in buffer
83 if (!Strings.isNullOrEmpty(buffer)) {
84 List<String> suffixCandidates = strings.stream()
85 // remove onos common prefix
86 .map(full -> full.replaceFirst("org\\.onosproject\\.", ""))
87 // a.b.c -> [c, b.c, a.b.c]
88 .flatMap(appName -> {
89 List<String> suffixes = new ArrayList<>();
90 Deque<String> frags = new ArrayDeque<>();
91 // a.b.c -> [c, b, a] -> [c, b.c, a.b.c]
92 Lists.reverse(asList(appName.split("\\."))).forEach(frag -> {
93 frags.addFirst(frag);
94 suffixes.add(frags.stream().collect(Collectors.joining(".")));
95 });
96 return suffixes.stream();
97 })
98 // convert to occurrence map
99 .collect(Collectors.groupingBy(e -> e, Collectors.counting()))
100 .entrySet().stream()
101 // only accept unique suffix
102 .filter(e -> e.getValue() == 1L)
103 .map(Entry::getKey)
104 .collect(Collectors.toList());
105
106 delegate.getStrings().addAll(suffixCandidates);
107 }
108
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800109 // Now let the completer do the work for figuring out what to offer.
110 return delegate.complete(buffer, cursor, candidates);
111 }
112
113}