Harold Huang | 652ea83 | 2017-03-18 22:59:44 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.cli.net.vnet; |
| 18 | |
| 19 | import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList; |
| 20 | import org.onosproject.cli.AbstractChoicesCompleter; |
| 21 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 22 | import org.onosproject.incubator.net.virtual.VirtualHost; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Collections; |
| 27 | import java.util.List; |
| 28 | import java.util.stream.Collectors; |
| 29 | |
| 30 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 31 | |
| 32 | /** |
| 33 | * Virtual host completer. |
| 34 | * |
| 35 | * Assumes the first argument which can be parsed to a number is network id. |
| 36 | */ |
| 37 | public class VirtualHostCompleter extends AbstractChoicesCompleter { |
| 38 | @Override |
| 39 | protected List<String> choices() { |
| 40 | ArgumentList args = getArgumentList(); |
| 41 | //parse argument list for network id |
| 42 | String[] argsArray = args.getArguments(); |
| 43 | for (String str : argsArray) { |
| 44 | if (str.matches("[0-9]+")) { |
| 45 | long networkId = Long.valueOf(str); |
| 46 | return getSortedVirtualHosts(networkId).stream() |
| 47 | .map(virtualHost -> virtualHost.id().toString()) |
| 48 | .collect(Collectors.toList()); |
| 49 | } |
| 50 | } |
| 51 | return Collections.singletonList("Missing network id"); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the list of virtual hosts sorted using the host identifier. |
| 56 | * |
| 57 | * @param networkId network id |
| 58 | * @return virtual host list |
| 59 | */ |
| 60 | private List<VirtualHost> getSortedVirtualHosts(long networkId) { |
| 61 | VirtualNetworkService service = getService(VirtualNetworkService.class); |
| 62 | |
| 63 | List<VirtualHost> virtualHosts = new ArrayList<>(); |
| 64 | virtualHosts.addAll(service.getVirtualHosts(NetworkId.networkId(networkId))); |
| 65 | return virtualHosts; |
| 66 | } |
| 67 | |
| 68 | } |