blob: 97dbabf776f4babd0f85f62a5c5d37eba2bcfc95 [file] [log] [blame]
Jian Lie189c1c2018-08-08 15:55:08 +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.openstacktroubleshoot.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import 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;
Jian Lie189c1c2018-08-08 15:55:08 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.InstancePort;
25import org.onosproject.openstacknetworking.api.InstancePortService;
26
27import java.util.List;
28import java.util.Set;
29import java.util.SortedSet;
30import java.util.stream.Collectors;
31
32/**
33 * Active VM IP address completer.
34 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070035@Service
Jian Lie189c1c2018-08-08 15:55:08 +090036public class ActiveVmIpCompleter implements Completer {
37
38 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070039 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
Jian Lie189c1c2018-08-08 15:55:08 +090040 StringsCompleter delegate = new StringsCompleter();
41 InstancePortService service = AbstractShellCommand.get(InstancePortService.class);
42 Set<String> set = service.instancePorts().stream()
43 .filter(p -> p.state() == InstancePort.State.ACTIVE)
44 .map(p -> p.ipAddress().getIp4Address().toString())
45 .collect(Collectors.toSet());
46
47 SortedSet<String> strings = delegate.getStrings();
48 strings.addAll(set);
49
Ray Milkey86ad7bb2018-09-27 12:32:28 -070050 return delegate.complete(session, commandLine, candidates);
Jian Lie189c1c2018-08-08 15:55:08 +090051 }
52}