Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.cli.net.vnet; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 24 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 25 | import org.onosproject.utils.Comparators; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Collections; |
| 29 | import 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.") |
| 36 | public 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 | } |