blob: 9d6d204e157c4a44bfb8aca3944a6b8ee8afe8fa [file] [log] [blame]
Jian Lic38e9032018-08-09 17:08:38 +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 Lic38e9032018-08-09 17:08:38 +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 floating IP address completer.
34 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070035@Service
Jian Lic38e9032018-08-09 17:08:38 +090036public class ActiveFloatingIpCompleter implements Completer {
37
38 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070039 public int complete(Session session, CommandLine commandLine, List<String> candidates) {
Jian Lic38e9032018-08-09 17:08:38 +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 .filter(p -> service.floatingIp(p.portId()) != null)
45 .map(p -> service.floatingIp(p.portId()).toString())
46 .collect(Collectors.toSet());
47
48 SortedSet<String> strings = delegate.getStrings();
49 strings.addAll(set);
50
Ray Milkey86ad7bb2018-09-27 12:32:28 -070051 return delegate.complete(session, commandLine, candidates);
Jian Lic38e9032018-08-09 17:08:38 +090052 }
53}