blob: b05ca5f31adef0314b05fd0a2fdb602553239473 [file] [log] [blame]
Jian Li138f51f2021-01-06 03:29:58 +09001/*
2 * Copyright 2020-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.kubevirtnode.cli;
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.Completer;
21import org.apache.karaf.shell.api.console.Session;
22import org.apache.karaf.shell.support.completers.StringsCompleter;
23import org.onosproject.kubevirtnode.api.KubevirtNode;
24import org.onosproject.kubevirtnode.api.KubevirtNodeService;
25
26import java.util.List;
27import java.util.Set;
28import java.util.SortedSet;
29import java.util.stream.Collectors;
30
31import static org.onosproject.cli.AbstractShellCommand.get;
32
33/**
34 * Kubevirt host completer.
35 */
36@Service
37public class KubevirtHostnameCompleter implements Completer {
38 @Override
39 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
40 StringsCompleter delegate = new StringsCompleter();
41 KubevirtNodeService nodeService = get(KubevirtNodeService.class);
42
43 Set<String> hostnames = nodeService.nodes().stream()
44 .map(KubevirtNode::hostname)
45 .collect(Collectors.toSet());
46 SortedSet<String> strings = delegate.getStrings();
47
48 strings.addAll(hostnames);
49
50 return delegate.complete(session, commandLine, candidates);
51 }
52}