Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacktroubleshoot.cli; |
| 17 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 18 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 19 | import org.apache.karaf.shell.api.console.CommandLine; |
| 20 | import org.apache.karaf.shell.api.console.Completer; |
| 21 | import org.apache.karaf.shell.api.console.Session; |
| 22 | import org.apache.karaf.shell.support.completers.StringsCompleter; |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 25 | import org.onosproject.openstacknetworking.api.InstancePortService; |
| 26 | |
| 27 | import java.util.List; |
| 28 | import java.util.Set; |
| 29 | import java.util.SortedSet; |
| 30 | import java.util.stream.Collectors; |
| 31 | |
| 32 | /** |
| 33 | * Active VM floating IP address completer. |
| 34 | */ |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 35 | @Service |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 36 | public class ActiveFloatingIpCompleter implements Completer { |
| 37 | |
| 38 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 39 | public int complete(Session session, CommandLine commandLine, List<String> candidates) { |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 40 | 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 51 | return delegate.complete(session, commandLine, candidates); |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 52 | } |
| 53 | } |