blob: 5984603b0dc19c0e7b5dee60c319dc50c189e1ff [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
Thomas Vachuska90b453f2015-01-30 18:57:14 -080057 // Fetch our service and feed it's offerings to the string completer
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070058 ApplicationService service = get(ApplicationService.class);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080059 Iterator<Application> it = service.getApplications().iterator();
60 SortedSet<String> strings = delegate.getStrings();
pierventre2018fa52022-01-31 09:11:32 +010061 while (it.hasNext()) {
62 Application app = it.next();
63 ApplicationState state = service.getState(app.id());
64 if ("uninstall".equals(cmd) || "download".equals(cmd) ||
65 ("activate".equals(cmd) && state == INSTALLED) ||
66 ("deactivate".equals(cmd) && state == ACTIVE)) {
67 strings.add(app.id().name());
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070068 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080069 }
70
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070071 // add unique suffix to candidates, if user has something in buffer
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 if (!Strings.isNullOrEmpty(commandLine.getCursorArgument())) {
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070073 List<String> suffixCandidates = strings.stream()
74 // remove onos common prefix
75 .map(full -> full.replaceFirst("org\\.onosproject\\.", ""))
76 // a.b.c -> [c, b.c, a.b.c]
77 .flatMap(appName -> {
78 List<String> suffixes = new ArrayList<>();
79 Deque<String> frags = new ArrayDeque<>();
80 // a.b.c -> [c, b, a] -> [c, b.c, a.b.c]
81 Lists.reverse(asList(appName.split("\\."))).forEach(frag -> {
82 frags.addFirst(frag);
83 suffixes.add(frags.stream().collect(Collectors.joining(".")));
84 });
85 return suffixes.stream();
86 })
87 // convert to occurrence map
88 .collect(Collectors.groupingBy(e -> e, Collectors.counting()))
89 .entrySet().stream()
90 // only accept unique suffix
91 .filter(e -> e.getValue() == 1L)
92 .map(Entry::getKey)
93 .collect(Collectors.toList());
94
95 delegate.getStrings().addAll(suffixCandidates);
96 }
97
Thomas Vachuska90b453f2015-01-30 18:57:14 -080098 // Now let the completer do the work for figuring out what to offer.
Ray Milkeyd84f89b2018-08-17 14:54:17 -070099 return delegate.complete(session, commandLine, candidates);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800100 }
101
102}