blob: 4a1ea76b27b381ac16f6aa2aef0101f6f6faa9c5 [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.VirtualNetworkService;
25import org.onosproject.incubator.net.virtual.VirtualPort;
26import org.onosproject.net.DeviceId;
27import org.onosproject.utils.Comparators;
28
29import java.util.ArrayList;
30import java.util.Collections;
31import java.util.List;
32
33/**
34 * Lists all virtual ports for the network ID.
35 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036@Service
Brian Stanke5df14472016-03-11 19:34:38 -050037@Command(scope = "onos", name = "vnet-ports",
38 description = "Lists all virtual ports in a virtual network.")
39public class VirtualPortListCommand extends AbstractShellCommand {
40
41 private static final String FMT_VIRTUAL_PORT =
Claudine Chiu579969d2017-10-06 14:32:18 -040042 "virtual portNumber=%s, physical deviceId=%s, portNumber=%s, isEnabled=%s";
Brian Stanke5df14472016-03-11 19:34:38 -050043
44 @Argument(index = 0, name = "networkId", description = "Network ID",
45 required = true, multiValued = false)
46 Long networkId = null;
47
48 @Argument(index = 1, name = "deviceId", description = "Virtual Device ID",
49 required = true, multiValued = false)
50 String deviceId = null;
51
52 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070053 protected void doExecute() {
Brian Stanke5df14472016-03-11 19:34:38 -050054
55 getSortedVirtualPorts().forEach(this::printVirtualPort);
56 }
57
58 /**
59 * Returns the list of virtual ports sorted using the network identifier.
60 *
61 * @return sorted virtual port list
62 */
63 private List<VirtualPort> getSortedVirtualPorts() {
64 VirtualNetworkService service = get(VirtualNetworkService.class);
65
66 List<VirtualPort> virtualPorts = new ArrayList<>();
Yoonseon Han6c603892016-09-01 11:52:21 -070067 virtualPorts.addAll(service.getVirtualPorts(NetworkId.networkId(networkId),
68 DeviceId.deviceId(deviceId)));
Brian Stanke5df14472016-03-11 19:34:38 -050069 Collections.sort(virtualPorts, Comparators.VIRTUAL_PORT_COMPARATOR);
70 return virtualPorts;
71 }
72
73 /**
74 * Prints out each virtual port.
75 *
76 * @param virtualPort virtual port
77 */
78 private void printVirtualPort(VirtualPort virtualPort) {
Yoonseon Han6c603892016-09-01 11:52:21 -070079 if (virtualPort.realizedBy() == null) {
Claudine Chiu579969d2017-10-06 14:32:18 -040080 print(FMT_VIRTUAL_PORT, virtualPort.number(), "None", "None", virtualPort.isEnabled());
Yoonseon Han6c603892016-09-01 11:52:21 -070081 } else {
82 print(FMT_VIRTUAL_PORT, virtualPort.number(),
83 virtualPort.realizedBy().deviceId(),
Claudine Chiu579969d2017-10-06 14:32:18 -040084 virtualPort.realizedBy().port(),
85 virtualPort.isEnabled());
Yoonseon Han6c603892016-09-01 11:52:21 -070086 }
Brian Stanke5df14472016-03-11 19:34:38 -050087 }
88}