daniel park | b581710 | 2018-02-15 00:18:51 +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 | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacknetworking.api.ExternalPeerRouter; |
| 25 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 26 | |
| 27 | import java.util.Iterator; |
| 28 | import java.util.List; |
| 29 | import java.util.Set; |
| 30 | import java.util.SortedSet; |
| 31 | import java.util.stream.Collectors; |
| 32 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 33 | import static org.onosproject.cli.AbstractShellCommand.get; |
| 34 | |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 35 | /** |
| 36 | * IP Address Completer. |
| 37 | */ |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 38 | @Service |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 39 | public class IpAddressCompleter 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 | b581710 | 2018-02-15 00:18:51 +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 | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 45 | Set<IpAddress> set = osNetService.externalPeerRouters().stream() |
Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame] | 46 | .map(ExternalPeerRouter::ipAddress) |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 47 | .collect(Collectors.toSet()); |
| 48 | SortedSet<String> strings = delegate.getStrings(); |
| 49 | |
| 50 | Iterator<IpAddress> it = set.iterator(); |
| 51 | |
| 52 | while (it.hasNext()) { |
| 53 | strings.add(it.next().toString()); |
| 54 | } |
| 55 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 56 | return delegate.complete(session, commandLine, candidates); |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 57 | |
| 58 | } |
| 59 | } |