blob: fbb535000b5a654cb8ff540131bc295fcba5e964 [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.openstacknode.api.OpenstackNode;
24import org.onosproject.openstacknode.api.OpenstackNodeService;
25
26import java.util.List;
27import java.util.Set;
28import java.util.SortedSet;
29import java.util.stream.Collectors;
30
Jian Li5ecfd1a2018-12-10 11:41:03 +090031import static org.onosproject.cli.AbstractShellCommand.get;
32
Jian Li62dbb6a2018-02-27 15:18:53 +090033/**
34 * Openstack host completer.
35 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070036@Service
Jian Li62dbb6a2018-02-27 15:18:53 +090037public class OpenstackHostnameCompleter implements Completer {
38
39 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070040 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
Jian Li62dbb6a2018-02-27 15:18:53 +090041 StringsCompleter delegate = new StringsCompleter();
Jian Li5ecfd1a2018-12-10 11:41:03 +090042 OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
Jian Li62dbb6a2018-02-27 15:18:53 +090043
44 Set<String> hostnames = osNodeService.nodes().stream()
45 .map(OpenstackNode::hostname)
46 .collect(Collectors.toSet());
47 SortedSet<String> strings = delegate.getStrings();
48
49 strings.addAll(hostnames);
50
Ray Milkey86ad7bb2018-09-27 12:32:28 -070051 return delegate.complete(session, commandLine, candidates);
Jian Li62dbb6a2018-02-27 15:18:53 +090052 }
53}