blob: 266042828d13cf55dd47334a3035a9bb2fb42c11 [file] [log] [blame]
Harold Huang652ea832017-03-18 22:59:44 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Harold Huang652ea832017-03-18 22:59:44 +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 */
16
17package org.onosproject.cli.net.vnet;
18
19import static org.onlab.osgi.DefaultServiceDirectory.getService;
20
Harold Huang652ea832017-03-18 22:59:44 +080021import org.onosproject.cli.AbstractChoicesCompleter;
22import org.onosproject.incubator.net.virtual.NetworkId;
23import org.onosproject.incubator.net.virtual.VirtualNetworkService;
24import org.onosproject.incubator.net.virtual.VirtualPort;
25import org.onosproject.net.DeviceId;
26import org.onosproject.utils.Comparators;
27
28import java.util.ArrayList;
29import java.util.Collections;
30import java.util.List;
31import java.util.stream.Collectors;
32/**
33 * Virtual port completer.
34 *
35 * Assumes the first argument which can be parsed to a number is network id
36 * and the argument right before the one being completed is device id
37 */
38public class VirtualPortCompleter extends AbstractChoicesCompleter {
39 @Override
40 protected List<String> choices() {
Harold Huang652ea832017-03-18 22:59:44 +080041 //parse argument list for network id
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042 String[] argsArray = commandLine.getArguments();
Harold Huang652ea832017-03-18 22:59:44 +080043 for (String str : argsArray) {
44 if (str.matches("[0-9]+")) {
45 long networkId = Long.valueOf(str);
46 String deviceId = argsArray[argsArray.length - 1];
47 return getSortedVirtualPorts(networkId, deviceId).stream()
48 .map(virtualPort -> virtualPort.number().toString())
49 .collect(Collectors.toList());
50 }
51 }
52
53 return Collections.singletonList("Missing network id");
54 }
55
56 /**
57 * Returns the list of virtual ports sorted using the port number.
58 *
59 * @param networkId network id.
60 * @param deviceId device id
61 * @return sorted virtual port list
62 */
63 private List<VirtualPort> getSortedVirtualPorts(long networkId, String deviceId) {
64 VirtualNetworkService service = getService(VirtualNetworkService.class);
65
66 List<VirtualPort> virtualPorts = new ArrayList<>();
67 virtualPorts.addAll(service.getVirtualPorts(NetworkId.networkId(networkId),
68 DeviceId.deviceId(deviceId)));
69 Collections.sort(virtualPorts, Comparators.VIRTUAL_PORT_COMPARATOR);
70 return virtualPorts;
71 }
72
73}