blob: 5192cc47e55b9fcd46810eed10675c27101d1474 [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
Ray Milkey0068fd02018-10-11 15:45:39 -070025import org.apache.karaf.shell.api.action.lifecycle.Service;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080026import 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;
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070029import org.onosproject.net.PortNumber;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080030import org.onosproject.net.device.DeviceService;
31
32/**
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070033 * PortNumber completer, which returns candidates in {@link PortNumber#toString()} form.
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080034 *
35 * Assumes argument right before the one being completed is DeviceId.
36 */
Ray Milkey0068fd02018-10-11 15:45:39 -070037@Service
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080038public class PortNumberCompleter extends AbstractChoicesCompleter {
39
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070040 /**
41 * Look for valid DeviceId in arguments passed so far.
42 *
43 * @return DeviceId found or null if not found
44 */
45 protected DeviceId lookForDeviceId() {
Saurav Dasa2d37502016-03-25 17:50:40 -070046 //parse argument list for deviceId
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080047 DeviceService deviceService = getService(DeviceService.class);
Saurav Dasa2d37502016-03-25 17:50:40 -070048 Device dev = null;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049 for (String str : commandLine.getArguments()) {
Saurav Dasa2d37502016-03-25 17:50:40 -070050 if (str.contains(":")) {
51 dev = deviceService.getDevice(DeviceId.deviceId(str));
52 if (dev != null) {
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070053 return dev.id();
Saurav Dasa2d37502016-03-25 17:50:40 -070054 }
55 }
56 }
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070057 return null;
58 }
Saurav Dasa2d37502016-03-25 17:50:40 -070059
Yuta HIGUCHI8de43282018-06-03 23:25:12 -070060 @Override
61 protected List<String> choices() {
62 DeviceId deviceId = lookForDeviceId();
63
64 if (deviceId == null) {
65 return Collections.emptyList();
66 }
67
68 DeviceService deviceService = getService(DeviceService.class);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080069 return StreamSupport.stream(deviceService.getPorts(deviceId).spliterator(), false)
70 .map(port -> port.number().toString())
71 .collect(Collectors.toList());
72 }
73
74}