blob: 6ffda807649431e9e1f48a90752dc29df9f62ac5 [file] [log] [blame]
jiangrui8143ad42015-08-12 15:26:20 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
jiangrui8143ad42015-08-12 15:26:20 +08003 *
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 */
16package org.onosproject.vtnrsc.cli.virtualport;
17
18import java.util.Collection;
19
20import org.apache.karaf.shell.commands.Command;
21import org.apache.karaf.shell.commands.Option;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.DeviceId;
24import org.onosproject.vtnrsc.TenantNetworkId;
25import org.onosproject.vtnrsc.VirtualPort;
26import org.onosproject.vtnrsc.VirtualPortId;
27import org.onosproject.vtnrsc.virtualport.VirtualPortService;
28
29/**
30 * Supports for querying virtualPorts.
31 */
32@Command(scope = "onos", name = "virtualports", description = "Supports for querying virtualPorts.")
33public class VirtualPortQueryCommand extends AbstractShellCommand {
34
35 @Option(name = "-v", aliases = "--vPortId", description = "virtualPort ID.", required = false,
36 multiValued = false)
37 String vPortId;
38
39 @Option(name = "-n", aliases = "--networkId", description = "network ID.", required = false,
40 multiValued = false)
41 String networkId;
42
43 @Option(name = "-d", aliases = "--deviceId", description = "device ID.", required = false,
44 multiValued = false)
45 String deviceId;
46
47 @Option(name = "-t", aliases = "--tenantId", description = "tenant ID.", required = false,
48 multiValued = false)
49 String tenantId;
50
51 private static final String FMT = "virtualPortId=%s, networkId=%s, name=%s,"
52 + " tenantId=%s, deviceId=%s, adminStateUp=%s, state=%s,"
53 + " macAddress=%s, deviceOwner=%s, fixedIp=%s, bindingHostId=%s,"
54 + " bindingvnicType=%s, bindingvifType=%s, bindingvnicDetails=%s,"
55 + " allowedAddress=%s, securityGroups=%s";
56
57 @Override
58 protected void execute() {
59 VirtualPortService service = get(VirtualPortService.class);
60 if (vPortId != null && networkId == null && deviceId == null && tenantId == null) {
61 VirtualPort port = service.getPort(VirtualPortId.portId(vPortId));
62 printPort(port);
63 } else if (vPortId == null && networkId != null && deviceId == null && tenantId == null) {
64 Collection<VirtualPort> ports = service.getPorts(TenantNetworkId.networkId(networkId));
65 printPorts(ports);
66 } else if (vPortId == null && networkId == null && deviceId != null && tenantId == null) {
67 Collection<VirtualPort> ports = service.getPorts(DeviceId.deviceId(deviceId));
68 printPorts(ports);
69 } else if (vPortId == null && networkId == null && deviceId == null && tenantId != null) {
70 Collection<VirtualPort> ports = service.getPorts(DeviceId.deviceId(tenantId));
71 printPorts(ports);
72 } else if (vPortId == null && networkId == null && deviceId == null && tenantId == null) {
73 Collection<VirtualPort> ports = service.getPorts();
74 printPorts(ports);
75 } else {
76 print("cannot input more than one parameter");
77 }
78
79 }
80
81 private void printPorts(Collection<VirtualPort> ports) {
82 for (VirtualPort port : ports) {
83 printPort(port);
84 }
85 }
86
87 private void printPort(VirtualPort port) {
88 print(FMT, port.portId(), port.networkId(), port.name(), port.tenantId(), port.deviceId(),
89 port.adminStateUp(), port.state(), port.macAddress(), port.deviceOwner(), port
90 .fixedIps(), port.bindingHostId(), port.bindingVnicType(),
91 port.bindingVifType(), port.bindingVifDetails(), port.allowedAddressPairs(),
92 port.securityGroups());
93 }
94}