blob: badbcd8390913873a73b7ea3687dd170324caaf5 [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.console.CommandLine;
19import org.apache.karaf.shell.api.console.Session;
20import org.apache.karaf.shell.support.completers.StringsCompleter;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080021import 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
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070026import com.google.common.base.Strings;
27import com.google.common.collect.Lists;
28
29import java.util.ArrayDeque;
30import java.util.ArrayList;
31import java.util.Deque;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080032import java.util.Iterator;
33import java.util.List;
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070034import java.util.Map.Entry;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080035import java.util.SortedSet;
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070036import java.util.stream.Collectors;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080037
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070038import static java.util.Arrays.asList;
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070039import static org.onosproject.app.ApplicationState.ACTIVE;
40import static org.onosproject.app.ApplicationState.INSTALLED;
41import static org.onosproject.cli.AbstractShellCommand.get;
42
Thomas Vachuska90b453f2015-01-30 18:57:14 -080043/**
44 * Application name completer.
45 */
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070046public class ApplicationNameCompleter extends AbstractCompleter {
Thomas Vachuska90b453f2015-01-30 18:57:14 -080047 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -080049 // Delegate string completer
50 StringsCompleter delegate = new StringsCompleter();
51
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070052 // Command name is the second argument.
Ray Milkeyd84f89b2018-08-17 14:54:17 -070053 String cmd = commandLine.getArguments()[1];
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070054
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070083 if (!Strings.isNullOrEmpty(commandLine.getCursorArgument())) {
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070084 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.
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700110 return delegate.complete(session, commandLine, candidates);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800111 }
112
113}