Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.cli.app; |
| 17 | |
Ray Milkey | 9dcc0e8 | 2018-10-01 10:26:53 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.console.CommandLine; |
| 20 | import org.apache.karaf.shell.api.console.Session; |
| 21 | import org.apache.karaf.shell.support.completers.StringsCompleter; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 22 | import org.onosproject.app.ApplicationService; |
Thomas Vachuska | f9e0d17 | 2015-04-12 08:29:14 -0700 | [diff] [blame] | 23 | import org.onosproject.app.ApplicationState; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractCompleter; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 25 | import org.onosproject.core.Application; |
| 26 | |
Yuta HIGUCHI | 0ef3387 | 2017-09-13 10:13:16 -0700 | [diff] [blame] | 27 | import com.google.common.base.Strings; |
| 28 | import com.google.common.collect.Lists; |
| 29 | |
| 30 | import java.util.ArrayDeque; |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.Deque; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 33 | import java.util.Iterator; |
| 34 | import java.util.List; |
Yuta HIGUCHI | 0ef3387 | 2017-09-13 10:13:16 -0700 | [diff] [blame] | 35 | import java.util.Map.Entry; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 36 | import java.util.SortedSet; |
Yuta HIGUCHI | 0ef3387 | 2017-09-13 10:13:16 -0700 | [diff] [blame] | 37 | import java.util.stream.Collectors; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 38 | |
Yuta HIGUCHI | 0ef3387 | 2017-09-13 10:13:16 -0700 | [diff] [blame] | 39 | import static java.util.Arrays.asList; |
Thomas Vachuska | f9e0d17 | 2015-04-12 08:29:14 -0700 | [diff] [blame] | 40 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 41 | import static org.onosproject.app.ApplicationState.INSTALLED; |
| 42 | import static org.onosproject.cli.AbstractShellCommand.get; |
| 43 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Application name completer. |
| 46 | */ |
Ray Milkey | 9dcc0e8 | 2018-10-01 10:26:53 -0700 | [diff] [blame] | 47 | @Service |
Thomas Vachuska | f9e0d17 | 2015-04-12 08:29:14 -0700 | [diff] [blame] | 48 | public class ApplicationNameCompleter extends AbstractCompleter { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 49 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 50 | public int complete(Session session, CommandLine commandLine, List<String> candidates) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 51 | // Delegate string completer |
| 52 | StringsCompleter delegate = new StringsCompleter(); |
| 53 | |
Thomas Vachuska | f9e0d17 | 2015-04-12 08:29:14 -0700 | [diff] [blame] | 54 | // Command name is the second argument. |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | String cmd = commandLine.getArguments()[1]; |
Thomas Vachuska | f9e0d17 | 2015-04-12 08:29:14 -0700 | [diff] [blame] | 56 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 57 | // Fetch our service and feed it's offerings to the string completer |
Thomas Vachuska | f9e0d17 | 2015-04-12 08:29:14 -0700 | [diff] [blame] | 58 | ApplicationService service = get(ApplicationService.class); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 59 | Iterator<Application> it = service.getApplications().iterator(); |
| 60 | SortedSet<String> strings = delegate.getStrings(); |
Arnav Jain | 80f2cb8 | 2019-07-15 10:54:17 -0700 | [diff] [blame] | 61 | 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 Vachuska | f9e0d17 | 2015-04-12 08:29:14 -0700 | [diff] [blame] | 75 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Yuta HIGUCHI | 0ef3387 | 2017-09-13 10:13:16 -0700 | [diff] [blame] | 78 | // add unique suffix to candidates, if user has something in buffer |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | if (!Strings.isNullOrEmpty(commandLine.getCursorArgument())) { |
Yuta HIGUCHI | 0ef3387 | 2017-09-13 10:13:16 -0700 | [diff] [blame] | 80 | 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 Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 105 | // Now let the completer do the work for figuring out what to offer. |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 106 | return delegate.complete(session, commandLine, candidates); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } |