Daniel Park | a73c236 | 2018-09-17 17:43:25 +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 | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 26 | import org.onosproject.openstacknetworking.api.InstancePortService; |
| 27 | |
| 28 | import java.util.Iterator; |
| 29 | import java.util.List; |
| 30 | import java.util.Set; |
| 31 | import java.util.SortedSet; |
| 32 | import java.util.stream.Collectors; |
| 33 | |
| 34 | /** |
| 35 | * Instance port ip address completer. |
| 36 | */ |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 37 | @Service |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 38 | public class InstanceIpAddressCompleter implements Completer { |
| 39 | private static final String EXTERNAL_IP = "8.8.8.8"; |
| 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 | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 43 | StringsCompleter delegate = new StringsCompleter(); |
| 44 | InstancePortService instancePortService = |
| 45 | AbstractShellCommand.get(InstancePortService.class); |
| 46 | |
| 47 | Set<IpAddress> set = instancePortService.instancePorts().stream() |
| 48 | .map(InstancePort::ipAddress) |
| 49 | .collect(Collectors.toSet()); |
| 50 | set.add(IpAddress.valueOf(EXTERNAL_IP)); |
| 51 | |
| 52 | SortedSet<String> strings = delegate.getStrings(); |
| 53 | |
| 54 | Iterator<IpAddress> it = set.iterator(); |
| 55 | |
| 56 | while (it.hasNext()) { |
| 57 | strings.add(it.next().toString()); |
| 58 | } |
| 59 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 60 | return delegate.complete(session, commandLine, candidates); |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 61 | |
| 62 | |
| 63 | } |
| 64 | } |