blob: 0a05d3175e404576c32aedafb0be2dc765bd4fc0 [file] [log] [blame]
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08001/*
2 * Copyright 2015 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 */
16package org.onosproject.cli.net;
17
18import static com.google.common.base.Preconditions.checkArgument;
19import static org.onlab.osgi.DefaultServiceDirectory.getService;
20
21import java.util.List;
22import java.util.stream.Collectors;
23import java.util.stream.StreamSupport;
24
25import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList;
26import org.onosproject.cli.AbstractChoicesCompleter;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.device.DeviceService;
29
30/**
31 * PortNumber completer.
32 *
33 * Assumes argument right before the one being completed is DeviceId.
34 */
35public class PortNumberCompleter extends AbstractChoicesCompleter {
36
37 @Override
38 protected List<String> choices() {
39 ArgumentList args = getArgumentList();
40 checkArgument(args.getCursorArgumentIndex() >= 1,
41 "Expects DeviceId as previous argument");
42
43 String deviceIdStr = args.getArguments()[args.getCursorArgumentIndex() - 1];
44 DeviceId deviceId = DeviceId.deviceId(deviceIdStr);
45
46 DeviceService deviceService = getService(DeviceService.class);
47 return StreamSupport.stream(deviceService.getPorts(deviceId).spliterator(), false)
48 .map(port -> port.number().toString())
49 .collect(Collectors.toList());
50 }
51
52}