blob: ff38857049b8db25656647f9a54002ef83981c95 [file] [log] [blame]
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08003 *
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
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080025import org.onosproject.cli.AbstractChoicesCompleter;
Saurav Dasa2d37502016-03-25 17:50:40 -070026import org.onosproject.net.Device;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080027import org.onosproject.net.DeviceId;
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070028import org.onosproject.net.PortNumber;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080029import org.onosproject.net.device.DeviceService;
30
31/**
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070032 * PortNumber completer, which returns candidates in {@link PortNumber#toString()} form.
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080033 *
34 * Assumes argument right before the one being completed is DeviceId.
35 */
36public class PortNumberCompleter extends AbstractChoicesCompleter {
37
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070038 /**
39 * Look for valid DeviceId in arguments passed so far.
40 *
41 * @return DeviceId found or null if not found
42 */
43 protected DeviceId lookForDeviceId() {
Saurav Dasa2d37502016-03-25 17:50:40 -070044 //parse argument list for deviceId
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080045 DeviceService deviceService = getService(DeviceService.class);
Saurav Dasa2d37502016-03-25 17:50:40 -070046 Device dev = null;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047 for (String str : commandLine.getArguments()) {
Saurav Dasa2d37502016-03-25 17:50:40 -070048 if (str.contains(":")) {
49 dev = deviceService.getDevice(DeviceId.deviceId(str));
50 if (dev != null) {
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070051 return dev.id();
Saurav Dasa2d37502016-03-25 17:50:40 -070052 }
53 }
54 }
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070055 return null;
56 }
Saurav Dasa2d37502016-03-25 17:50:40 -070057
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070058 @Override
59 protected List<String> choices() {
60 DeviceId deviceId = lookForDeviceId();
61
62 if (deviceId == null) {
63 return Collections.emptyList();
64 }
65
66 DeviceService deviceService = getService(DeviceService.class);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080067 return StreamSupport.stream(deviceService.getPorts(deviceId).spliterator(), false)
68 .map(port -> port.number().toString())
69 .collect(Collectors.toList());
70 }
71
72}