HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 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.cli.net; |
| 17 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 18 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 19 | |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 20 | import java.util.Collections; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 21 | import java.util.List; |
| 22 | import java.util.stream.Collectors; |
| 23 | import java.util.stream.StreamSupport; |
| 24 | |
| 25 | import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList; |
| 26 | import org.onosproject.cli.AbstractChoicesCompleter; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.device.DeviceService; |
| 30 | |
| 31 | /** |
| 32 | * PortNumber completer. |
| 33 | * |
| 34 | * Assumes argument right before the one being completed is DeviceId. |
| 35 | */ |
| 36 | public class PortNumberCompleter extends AbstractChoicesCompleter { |
| 37 | |
| 38 | @Override |
| 39 | protected List<String> choices() { |
| 40 | ArgumentList args = getArgumentList(); |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 41 | //parse argument list for deviceId |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 42 | DeviceService deviceService = getService(DeviceService.class); |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 43 | Device dev = null; |
| 44 | for (String str : args.getArguments()) { |
| 45 | if (str.contains(":")) { |
| 46 | dev = deviceService.getDevice(DeviceId.deviceId(str)); |
| 47 | if (dev != null) { |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | if (dev == null) { |
| 53 | return Collections.singletonList("Missing device"); |
| 54 | } |
| 55 | DeviceId deviceId = dev.id(); |
| 56 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 57 | return StreamSupport.stream(deviceService.getPorts(deviceId).spliterator(), false) |
| 58 | .map(port -> port.number().toString()) |
| 59 | .collect(Collectors.toList()); |
| 60 | } |
| 61 | |
| 62 | } |