blob: 949f4f873068a884be472d37f557503dd0518aa0 [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
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080018import static org.onlab.osgi.DefaultServiceDirectory.getService;
19
Saurav Dasa2d37502016-03-25 17:50:40 -070020import java.util.Collections;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080021import 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;
Saurav Dasa2d37502016-03-25 17:50:40 -070027import org.onosproject.net.Device;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080028import org.onosproject.net.DeviceId;
29import org.onosproject.net.device.DeviceService;
30
31/**
32 * PortNumber completer.
33 *
34 * Assumes argument right before the one being completed is DeviceId.
35 */
36public class PortNumberCompleter extends AbstractChoicesCompleter {
37
38 @Override
39 protected List<String> choices() {
40 ArgumentList args = getArgumentList();
Saurav Dasa2d37502016-03-25 17:50:40 -070041 //parse argument list for deviceId
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080042 DeviceService deviceService = getService(DeviceService.class);
Saurav Dasa2d37502016-03-25 17:50:40 -070043 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 Yuta848324f2015-12-04 21:11:26 -080057 return StreamSupport.stream(deviceService.getPorts(deviceId).spliterator(), false)
58 .map(port -> port.number().toString())
59 .collect(Collectors.toList());
60 }
61
62}