Daniel Park | 95f7331 | 2018-07-31 15:48:34 +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.openstacknetworking.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; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 23 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 24 | import org.openstack4j.model.network.Port; |
| 25 | |
| 26 | import java.util.Iterator; |
| 27 | import java.util.List; |
| 28 | import java.util.Set; |
| 29 | import java.util.SortedSet; |
| 30 | import java.util.stream.Collectors; |
| 31 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 32 | import static org.onosproject.cli.AbstractShellCommand.get; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 33 | import static org.onosproject.openstacknetworking.api.Constants.DIRECT; |
| 34 | |
| 35 | /** |
| 36 | * Direct port completer. |
| 37 | */ |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 38 | @Service |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 39 | public class DirectPortListCompleter implements Completer { |
| 40 | |
| 41 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 42 | public int complete(Session session, CommandLine commandLine, List<String> candidates) { |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 43 | StringsCompleter delegate = new StringsCompleter(); |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 44 | OpenstackNetworkService osNetService = get(OpenstackNetworkService.class); |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 45 | Set<String> set = osNetService.ports().stream() |
| 46 | .filter(port -> port.getvNicType().equals(DIRECT)) |
| 47 | .map(Port::getId) |
| 48 | .collect(Collectors.toSet()); |
| 49 | |
| 50 | SortedSet<String> strings = delegate.getStrings(); |
| 51 | Iterator<String> it = set.iterator(); |
| 52 | |
| 53 | while (it.hasNext()) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 54 | strings.add(it.next()); |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 55 | } |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 56 | return delegate.complete(session, commandLine, candidates); |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 57 | } |
| 58 | } |