blob: 410b483316a3a01451abef15dbd868fe120bdd2f [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
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080017package org.onosproject.incubator.net.virtual.cli;
Harold Huang652ea832017-03-18 22:59:44 +080018
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;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080023import org.onosproject.incubator.net.virtual.Comparators;
Harold Huang652ea832017-03-18 22:59:44 +080024import org.onosproject.incubator.net.virtual.NetworkId;
25import org.onosproject.incubator.net.virtual.VirtualDevice;
26import org.onosproject.incubator.net.virtual.VirtualNetworkService;
Harold Huang652ea832017-03-18 22:59:44 +080027
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 */
Ray Milkey0068fd02018-10-11 15:45:39 -070038@Service
Harold Huang652ea832017-03-18 22:59:44 +080039public class VirtualDeviceCompleter extends AbstractChoicesCompleter {
40 @Override
41 protected List<String> choices() {
Harold Huang652ea832017-03-18 22:59:44 +080042 //parse argument list for network id
Ray Milkeyd84f89b2018-08-17 14:54:17 -070043 String[] argsArray = commandLine.getArguments();
Harold Huang652ea832017-03-18 22:59:44 +080044 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}