blob: de8530efe945eaa611f311d7e4d4c9f4e83b64fc [file] [log] [blame]
Jian Li62dbb6a2018-02-27 15:18:53 +09001/*
2 * Copyright 2018-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.openstacknode.cli;
17
Ray Milkey7a2dee52018-09-28 10:58:28 -070018import org.apache.karaf.shell.api.action.lifecycle.Service;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import 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;
Jian Li62dbb6a2018-02-27 15:18:53 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknode.api.OpenstackNode;
25import org.onosproject.openstacknode.api.OpenstackNodeService;
26
27import java.util.List;
28import java.util.Set;
29import java.util.SortedSet;
30import java.util.stream.Collectors;
31
32/**
33 * Openstack host completer.
34 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070035@Service
Jian Li62dbb6a2018-02-27 15:18:53 +090036public class OpenstackHostnameCompleter implements Completer {
37
38 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070039 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
Jian Li62dbb6a2018-02-27 15:18:53 +090040 StringsCompleter delegate = new StringsCompleter();
41 OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class);
42
43 Set<String> hostnames = osNodeService.nodes().stream()
44 .map(OpenstackNode::hostname)
45 .collect(Collectors.toSet());
46 SortedSet<String> strings = delegate.getStrings();
47
48 strings.addAll(hostnames);
49
Ray Milkey86ad7bb2018-09-27 12:32:28 -070050 return delegate.complete(session, commandLine, candidates);
Jian Li62dbb6a2018-02-27 15:18:53 +090051 }
52}