blob: facb3ec39a0c22b1f95a03798453720b7242d914 [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.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.incubator.net.virtual.TenantId;
22import org.onosproject.incubator.net.virtual.VirtualNetwork;
23import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
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;
30import java.util.Set;
31
32/**
33 * Lists all virtual networks for the tenant ID.
34 */
35@Command(scope = "onos", name = "vnets",
36 description = "Lists all virtual networks.")
37public class VirtualNetworkListCommand extends AbstractShellCommand {
38
39 private static final String FMT_VIRTUAL_NETWORK =
40 "tenantId=%s, networkId=%s";
41
42 @Override
43 protected void execute() {
44
45 getSortedVirtualNetworks().forEach(this::printVirtualNetwork);
46 }
47
48 /**
49 * Returns the list of virtual networks sorted using the tenant identifier.
50 *
51 * @return sorted virtual network list
52 */
53 private List<VirtualNetwork> getSortedVirtualNetworks() {
54 VirtualNetworkService service = get(VirtualNetworkService.class);
55 VirtualNetworkAdminService adminService = get(VirtualNetworkAdminService.class);
56
57 List<VirtualNetwork> virtualNetworks = new ArrayList<>();
58
59 Set<TenantId> tenantSet = adminService.getTenantIds();
60 tenantSet.forEach(tenantId -> virtualNetworks.addAll(service.getVirtualNetworks(tenantId)));
61
62 Collections.sort(virtualNetworks, Comparators.VIRTUAL_NETWORK_COMPARATOR);
63 return virtualNetworks;
64 }
65
66 /**
67 * Prints out each virtual network.
68 *
69 * @param virtualNetwork virtual network
70 */
71 private void printVirtualNetwork(VirtualNetwork virtualNetwork) {
72 print(FMT_VIRTUAL_NETWORK, virtualNetwork.tenantId(), virtualNetwork.id());
73 }
74}
75