blob: 03409d0f3713fb9ed6fee8555f4c762115984478 [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();
Arnav Jain80f2cb82019-07-15 10:54:17 -070061 if ("install".equals(cmd)) {
62 it = service.getRegisteredApplications().iterator();
63 while (it.hasNext()) {
64 strings.add(it.next().id().name());
65 }
66 } else {
67 while (it.hasNext()) {
68 Application app = it.next();
69 ApplicationState state = service.getState(app.id());
70 if ("uninstall".equals(cmd) || "download".equals(cmd) ||
71 ("activate".equals(cmd) && state == INSTALLED) ||
72 ("deactivate".equals(cmd) && state == ACTIVE)) {
73 strings.add(app.id().name());
74 }
Thomas Vachuskaf9e0d172015-04-12 08:29:14 -070075 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080076 }
77
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070078 // add unique suffix to candidates, if user has something in buffer
Ray Milkeyd84f89b2018-08-17 14:54:17 -070079 if (!Strings.isNullOrEmpty(commandLine.getCursorArgument())) {
Yuta HIGUCHI0ef33872017-09-13 10:13:16 -070080 List<String> suffixCandidates = strings.stream()
81 // remove onos common prefix
82 .map(full -> full.replaceFirst("org\\.onosproject\\.", ""))
83 // a.b.c -> [c, b.c, a.b.c]
84 .flatMap(appName -> {
85 List<String> suffixes = new ArrayList<>();
86 Deque<String> frags = new ArrayDeque<>();
87 // a.b.c -> [c, b, a] -> [c, b.c, a.b.c]
88 Lists.reverse(asList(appName.split("\\."))).forEach(frag -> {
89 frags.addFirst(frag);
90 suffixes.add(frags.stream().collect(Collectors.joining(".")));
91 });
92 return suffixes.stream();
93 })
94 // convert to occurrence map
95 .collect(Collectors.groupingBy(e -> e, Collectors.counting()))
96 .entrySet().stream()
97 // only accept unique suffix
98 .filter(e -> e.getValue() == 1L)
99 .map(Entry::getKey)
100 .collect(Collectors.toList());
101
102 delegate.getStrings().addAll(suffixCandidates);
103 }
104
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800105 // Now let the completer do the work for figuring out what to offer.
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700106 return delegate.complete(session, commandLine, candidates);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800107 }
108
109}