Yuta HIGUCHI | 8de4328 | 2018-06-03 23:25:12 -0700 | [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.cli.net.completer; |
| 17 | |
| 18 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 19 | |
| 20 | import java.util.Collections; |
| 21 | import java.util.List; |
| 22 | import java.util.stream.Collectors; |
| 23 | import java.util.stream.StreamSupport; |
| 24 | |
| 25 | import org.onosproject.cli.net.PortNumberCompleter; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | |
| 29 | /** |
| 30 | * PortNumber completer, which returns candidates in decimal form. |
| 31 | * |
| 32 | * Assumes argument right before the one being completed is DeviceId. |
| 33 | */ |
| 34 | public class NumericPortNumberCompleter extends PortNumberCompleter { |
| 35 | |
| 36 | @Override |
| 37 | protected List<String> choices() { |
| 38 | DeviceId deviceId = lookForDeviceId(); |
| 39 | |
| 40 | if (deviceId == null) { |
| 41 | return Collections.emptyList(); |
| 42 | } |
| 43 | |
| 44 | DeviceService deviceService = getService(DeviceService.class); |
| 45 | return StreamSupport.stream(deviceService.getPorts(deviceId).spliterator(), false) |
| 46 | .map(port -> Long.toString(port.number().toLong())) |
| 47 | .collect(Collectors.toList()); |
| 48 | } |
| 49 | } |