blob: f810a977337ae69c77911ba901428d1d400f99ea [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
Ray Milkey0068fd02018-10-11 15:45:39 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Harold Huang652ea832017-03-18 22:59:44 +080022import org.onosproject.cli.AbstractChoicesCompleter;
23import org.onosproject.incubator.net.virtual.NetworkId;
24import org.onosproject.incubator.net.virtual.VirtualNetworkService;
25import org.onosproject.incubator.net.virtual.VirtualPort;
26import org.onosproject.net.DeviceId;
27import org.onosproject.utils.Comparators;
28
29import java.util.ArrayList;
30import java.util.Collections;
31import java.util.List;
32import java.util.stream.Collectors;
33/**
34 * Virtual port completer.
35 *
36 * Assumes the first argument which can be parsed to a number is network id
37 * and the argument right before the one being completed is device id
38 */
Ray Milkey0068fd02018-10-11 15:45:39 -070039@Service
Harold Huang652ea832017-03-18 22:59:44 +080040public class VirtualPortCompleter extends AbstractChoicesCompleter {
41 @Override
42 protected List<String> choices() {
Harold Huang652ea832017-03-18 22:59:44 +080043 //parse argument list for network id
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044 String[] argsArray = commandLine.getArguments();
Harold Huang652ea832017-03-18 22:59:44 +080045 for (String str : argsArray) {
46 if (str.matches("[0-9]+")) {
47 long networkId = Long.valueOf(str);
48 String deviceId = argsArray[argsArray.length - 1];
49 return getSortedVirtualPorts(networkId, deviceId).stream()
50 .map(virtualPort -> virtualPort.number().toString())
51 .collect(Collectors.toList());
52 }
53 }
54
55 return Collections.singletonList("Missing network id");
56 }
57
58 /**
59 * Returns the list of virtual ports sorted using the port number.
60 *
61 * @param networkId network id.
62 * @param deviceId device id
63 * @return sorted virtual port list
64 */
65 private List<VirtualPort> getSortedVirtualPorts(long networkId, String deviceId) {
66 VirtualNetworkService service = getService(VirtualNetworkService.class);
67
68 List<VirtualPort> virtualPorts = new ArrayList<>();
69 virtualPorts.addAll(service.getVirtualPorts(NetworkId.networkId(networkId),
70 DeviceId.deviceId(deviceId)));
71 Collections.sort(virtualPorts, Comparators.VIRTUAL_PORT_COMPARATOR);
72 return virtualPorts;
73 }
74
75}