blob: 4b554a75d0c288f17745564d1e03985feeadee63 [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.VirtualDevice;
24import org.onosproject.incubator.net.virtual.VirtualNetworkService;
25import org.onosproject.utils.Comparators;
26
27import java.util.ArrayList;
28import java.util.Collections;
29import java.util.List;
30import java.util.stream.Collectors;
31
32/**
33 * Virtual device completer.
34 *
35 * Assumes the first argument which can be parsed to a number is network id.
36 */
37public class VirtualDeviceCompleter extends AbstractChoicesCompleter {
38 @Override
39 protected List<String> choices() {
Harold Huang652ea832017-03-18 22:59:44 +080040 //parse argument list for network id
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041 String[] argsArray = commandLine.getArguments();
Harold Huang652ea832017-03-18 22:59:44 +080042 for (String str : argsArray) {
43 if (str.matches("[0-9]+")) {
44 long networkId = Long.valueOf(str);
45 return getSortedVirtualDevices(networkId).stream()
46 .map(virtualDevice -> virtualDevice.id().toString())
47 .collect(Collectors.toList());
48 }
49 }
50 return Collections.singletonList("Missing network id");
51 }
52
53 /**
54 * Returns the list of virtual devices sorted using the network identifier.
55 *
56 * @param networkId network id
57 * @return sorted virtual device list
58 */
59 private List<VirtualDevice> getSortedVirtualDevices(long networkId) {
60 VirtualNetworkService service = getService(VirtualNetworkService.class);
61
62 List<VirtualDevice> virtualDevices = new ArrayList<>();
63 virtualDevices.addAll(service.getVirtualDevices(NetworkId.networkId(networkId)));
64 Collections.sort(virtualDevices, Comparators.VIRTUAL_DEVICE_COMPARATOR);
65 return virtualDevices;
66 }
67
68}