blob: e0b63ccb1e41dc126b14350f1aa835fdba85f30f [file] [log] [blame]
Yuta HIGUCHI8de43282018-06-03 23:25:12 -07001/*
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 */
16package org.onosproject.cli.net.completer;
17
18import static org.onlab.osgi.DefaultServiceDirectory.getService;
19
20import java.util.Collections;
21import java.util.List;
22import java.util.stream.Collectors;
23import java.util.stream.StreamSupport;
24
25import org.onosproject.cli.net.PortNumberCompleter;
26import org.onosproject.net.DeviceId;
27import 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 */
34public 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}