blob: 5f002ff94298c5978dea7db284c8d5d7cf6603a2 [file] [log] [blame]
Ray Milkeyd84f89b2018-08-17 14:54:17 -07001/*
2 * Copyright 2015-present Open Networking Foundation
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.cli2.app;
17
18import org.apache.karaf.shell.api.action.lifecycle.Service;
19import org.apache.karaf.shell.api.console.CommandLine;
20import org.apache.karaf.shell.api.console.Session;
21import org.apache.karaf.shell.support.completers.StringsCompleter;
22import org.onosproject.app.ApplicationService;
23import org.onosproject.app.ApplicationState;
24import org.onosproject.cli2.AbstractCompleter;
25import org.onosproject.core.Application;
26
27import com.google.common.base.Strings;
28import com.google.common.collect.Lists;
29import org.slf4j.Logger;
30
31import java.util.ArrayDeque;
32import java.util.ArrayList;
33import java.util.Deque;
34import java.util.Iterator;
35import java.util.List;
36import java.util.Map.Entry;
37import java.util.SortedSet;
38import java.util.stream.Collectors;
39
40import static java.util.Arrays.asList;
41import static org.onosproject.app.ApplicationState.ACTIVE;
42import static org.onosproject.app.ApplicationState.INSTALLED;
43import static org.onosproject.cli2.AbstractShellCommand.get;
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
47 * Application name completer.
48 */
49@Service
50public class ApplicationNameCompleter extends AbstractCompleter {
51 private static final Logger log = getLogger(ApplicationNameCompleter.class);
52
53 @Override
54 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
55 // Delegate string completer
56 StringsCompleter delegate = new StringsCompleter();
57
58 // Command name is the second argument.
59 String cmd = commandLine.getArguments()[1];
60 log.info("Command is {}", cmd);
61
62 // Grab apps already on the command (to prevent tab-completed duplicates)
63 // FIXME: This does not work.
64// final Set previousApps;
65// if (list.getArguments().length > 2) {
66// previousApps = Sets.newHashSet(
67// Arrays.copyOfRange(list.getArguments(), 2, list.getArguments().length));
68// } else {
69// previousApps = Collections.emptySet();
70// }
71
72 // Fetch our service and feed it's offerings to the string completer
73 ApplicationService service = get(ApplicationService.class);
74 Iterator<Application> it = service.getApplications().iterator();
75 SortedSet<String> strings = delegate.getStrings();
76 int c = 0;
77 log.info("Processing apps");
78 while (it.hasNext()) {
79 Application app = it.next();
80 log.info("app #{} is {}", c, app.id().name());
81 c++;
82 ApplicationState state = service.getState(app.id());
83// if (previousApps.contains(app.id().name())) {
84// continue;
85// }
86 if ("uninstall".equals(cmd) || "download".equals(cmd) ||
87 ("activate".equals(cmd) && state == INSTALLED) ||
88 ("deactivate".equals(cmd) && state == ACTIVE)) {
89 strings.add(app.id().name());
90 }
91 }
92
93 // add unique suffix to candidates, if user has something in buffer
94 log.info("Command line buffer {} position {}", commandLine.getBuffer(), commandLine.getBufferPosition());
95 if (!commandLine.getBuffer().endsWith(cmd)) {
96 List<String> suffixCandidates = strings.stream()
97 // remove onos common prefix
98 .map(full -> full.replaceFirst("org\\.onosproject\\.", ""))
99 // a.b.c -> [c, b.c, a.b.c]
100 .flatMap(appName -> {
101 List<String> suffixes = new ArrayList<>();
102 Deque<String> frags = new ArrayDeque<>();
103 // a.b.c -> [c, b, a] -> [c, b.c, a.b.c]
104 Lists.reverse(asList(appName.split("\\."))).forEach(frag -> {
105 frags.addFirst(frag);
106 suffixes.add(frags.stream().collect(Collectors.joining(".")));
107 });
108 return suffixes.stream();
109 })
110 // convert to occurrence map
111 .collect(Collectors.groupingBy(e -> e, Collectors.counting()))
112 .entrySet().stream()
113 // only accept unique suffix
114 .filter(e -> e.getValue() == 1L)
115 .map(Entry::getKey)
116 .collect(Collectors.toList());
117
118 delegate.getStrings().addAll(suffixCandidates);
119 }
120
121 // Now let the completer do the work for figuring out what to offer.
122 return delegate.complete(session, commandLine, candidates);
123 }
124
125}