blob: 9cfc3aeb7e7f970788b54015a2915414e9ed9e7f [file] [log] [blame]
Brian Stanke5df14472016-03-11 19:34:38 -05001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Brian Stanke5df14472016-03-11 19:34:38 -05003 *
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 org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onosproject.cli.AbstractShellCommand;
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;
30
31/**
32 * Lists all virtual devices for the network ID.
33 */
34@Command(scope = "onos", name = "vnet-devices",
35 description = "Lists all virtual devices in a virtual network.")
36public class VirtualDeviceListCommand extends AbstractShellCommand {
37
38 private static final String FMT_VIRTUAL_DEVICE =
39 "deviceId=%s";
40
41 @Argument(index = 0, name = "networkId", description = "Network ID",
42 required = true, multiValued = false)
43 Long networkId = null;
44
45 @Override
46 protected void execute() {
47
48 getSortedVirtualDevices().forEach(this::printVirtualDevice);
49 }
50
51 /**
52 * Returns the list of virtual devices sorted using the device identifier.
53 *
54 * @return sorted virtual device list
55 */
56 private List<VirtualDevice> getSortedVirtualDevices() {
57 VirtualNetworkService service = get(VirtualNetworkService.class);
58
59 List<VirtualDevice> virtualDevices = new ArrayList<>();
60 virtualDevices.addAll(service.getVirtualDevices(NetworkId.networkId(networkId)));
61 Collections.sort(virtualDevices, Comparators.VIRTUAL_DEVICE_COMPARATOR);
62 return virtualDevices;
63 }
64
65 /**
66 * Prints out each virtual device.
67 *
68 * @param virtualDevice virtual device
69 */
70 private void printVirtualDevice(VirtualDevice virtualDevice) {
71 print(FMT_VIRTUAL_DEVICE, virtualDevice.id());
72 }
73}