blob: 2d0a6f7e64aef99edb799af77793809c93e5b812 [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 Milkey9dcc0e82018-10-01 10:26:53 -070018import org.apache.karaf.shell.api.action.lifecycle.Service;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.console.CommandLine;
20import org.apache.karaf.shell.api.console.Session;
21import org.apache.karaf.shell.support.completers.StringsCompleter;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080022import org.onosproject.app.ApplicationService;
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070023import org.onosproject.app.ApplicationState;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070024import org.onosproject.cli.AbstractCompleter;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080025import org.onosproject.core.Application;
26
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070027import com.google.common.base.Strings;
28import com.google.common.collect.Lists;
29
30import java.util.ArrayDeque;
31import java.util.ArrayList;
32import java.util.Deque;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080033import java.util.Iterator;
34import java.util.List;
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070035import java.util.Map.Entry;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080036import java.util.SortedSet;
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070037import java.util.stream.Collectors;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080038
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070039import static java.util.Arrays.asList;
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070040import static org.onosproject.app.ApplicationState.ACTIVE;
41import static org.onosproject.app.ApplicationState.INSTALLED;
42import static org.onosproject.cli.AbstractShellCommand.get;
43
Thomas Vachuska90b453f2015-01-30 18:57:14 -080044/**
45 * Application name completer.
46 */
Ray Milkey9dcc0e82018-10-01 10:26:53 -070047@Service
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070048public class ApplicationNameCompleter extends AbstractCompleter {
Thomas Vachuska90b453f2015-01-30 18:57:14 -080049 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -080051 // Delegate string completer
52 StringsCompleter delegate = new StringsCompleter();
53
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070054 // Command name is the second argument.
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055 String cmd = commandLine.getArguments()[1];
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070056
Brian O'Connore709a3b2015-04-14 15:02:27 -070057 // Grab apps already on the command (to prevent tab-completed duplicates)
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070058 // FIXME: This does not work.
59// final Set previousApps;
60// if (list.getArguments().length > 2) {
61// previousApps = Sets.newHashSet(
62// Arrays.copyOfRange(list.getArguments(), 2, list.getArguments().length));
63// } else {
64// previousApps = Collections.emptySet();
65// }
Brian O'Connore709a3b2015-04-14 15:02:27 -070066
Thomas Vachuska90b453f2015-01-30 18:57:14 -080067 // Fetch our service and feed it's offerings to the string completer
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070068 ApplicationService service = get(ApplicationService.class);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080069 Iterator<Application> it = service.getApplications().iterator();
70 SortedSet<String> strings = delegate.getStrings();
71 while (it.hasNext()) {
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070072 Application app = it.next();
73 ApplicationState state = service.getState(app.id());
Thomas Vachuskaa7a0f562015-04-14 23:27:44 -070074// if (previousApps.contains(app.id().name())) {
75// continue;
76// }
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070077 if ("uninstall".equals(cmd) || "download".equals(cmd) ||
Jon Halla3fcf672017-03-28 16:53:22 -070078 ("activate".equals(cmd) && state == INSTALLED) ||
79 ("deactivate".equals(cmd) && state == ACTIVE)) {
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070080 strings.add(app.id().name());
81 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080082 }
83
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070084 // add unique suffix to candidates, if user has something in buffer
Ray Milkeyd84f89b2018-08-17 14:54:17 -070085 if (!Strings.isNullOrEmpty(commandLine.getCursorArgument())) {
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070086 List<String> suffixCandidates = strings.stream()
87 // remove onos common prefix
88 .map(full -> full.replaceFirst("org\\.onosproject\\.", ""))
89 // a.b.c -> [c, b.c, a.b.c]
90 .flatMap(appName -> {
91 List<String> suffixes = new ArrayList<>();
92 Deque<String> frags = new ArrayDeque<>();
93 // a.b.c -> [c, b, a] -> [c, b.c, a.b.c]
94 Lists.reverse(asList(appName.split("\\."))).forEach(frag -> {
95 frags.addFirst(frag);
96 suffixes.add(frags.stream().collect(Collectors.joining(".")));
97 });
98 return suffixes.stream();
99 })
100 // convert to occurrence map
101 .collect(Collectors.groupingBy(e -> e, Collectors.counting()))
102 .entrySet().stream()
103 // only accept unique suffix
104 .filter(e -> e.getValue() == 1L)
105 .map(Entry::getKey)
106 .collect(Collectors.toList());
107
108 delegate.getStrings().addAll(suffixCandidates);
109 }
110
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800111 // Now let the completer do the work for figuring out what to offer.
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700112 return delegate.complete(session, commandLine, candidates);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800113 }
114
115}