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