blob: 780c0bbfae686ef6aceebe1b3d164a64df0a01f3 [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
21import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList;
22import org.onosproject.cli.AbstractChoicesCompleter;
23import org.onosproject.incubator.net.virtual.NetworkId;
24import org.onosproject.incubator.net.virtual.VirtualDevice;
25import org.onosproject.incubator.net.virtual.VirtualNetworkService;
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/**
34 * Virtual device completer.
35 *
36 * Assumes the first argument which can be parsed to a number is network id.
37 */
38public class VirtualDeviceCompleter extends AbstractChoicesCompleter {
39 @Override
40 protected List<String> choices() {
41 ArgumentList args = getArgumentList();
42 //parse argument list for network id
43 String[] argsArray = args.getArguments();
44 for (String str : argsArray) {
45 if (str.matches("[0-9]+")) {
46 long networkId = Long.valueOf(str);
47 return getSortedVirtualDevices(networkId).stream()
48 .map(virtualDevice -> virtualDevice.id().toString())
49 .collect(Collectors.toList());
50 }
51 }
52 return Collections.singletonList("Missing network id");
53 }
54
55 /**
56 * Returns the list of virtual devices sorted using the network identifier.
57 *
58 * @param networkId network id
59 * @return sorted virtual device list
60 */
61 private List<VirtualDevice> getSortedVirtualDevices(long networkId) {
62 VirtualNetworkService service = getService(VirtualNetworkService.class);
63
64 List<VirtualDevice> virtualDevices = new ArrayList<>();
65 virtualDevices.addAll(service.getVirtualDevices(NetworkId.networkId(networkId)));
66 Collections.sort(virtualDevices, Comparators.VIRTUAL_DEVICE_COMPARATOR);
67 return virtualDevices;
68 }
69
70}