blob: 520a33acb02c8296848e34c49f63f87f53dfa1e0 [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
jiangrui8143ad42015-08-12 15:26:20 +080023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.DeviceId;
25import org.onosproject.vtnrsc.TenantNetworkId;
26import org.onosproject.vtnrsc.VirtualPort;
27import org.onosproject.vtnrsc.VirtualPortId;
28import org.onosproject.vtnrsc.virtualport.VirtualPortService;
29
30/**
31 * Supports for querying virtualPorts.
32 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070033@Service
jiangrui8143ad42015-08-12 15:26:20 +080034@Command(scope = "onos", name = "virtualports", description = "Supports for querying virtualPorts.")
35public class VirtualPortQueryCommand extends AbstractShellCommand {
36
37 @Option(name = "-v", aliases = "--vPortId", description = "virtualPort ID.", required = false,
38 multiValued = false)
39 String vPortId;
40
41 @Option(name = "-n", aliases = "--networkId", description = "network ID.", required = false,
42 multiValued = false)
43 String networkId;
44
45 @Option(name = "-d", aliases = "--deviceId", description = "device ID.", required = false,
46 multiValued = false)
47 String deviceId;
48
49 @Option(name = "-t", aliases = "--tenantId", description = "tenant ID.", required = false,
50 multiValued = false)
51 String tenantId;
52
53 private static final String FMT = "virtualPortId=%s, networkId=%s, name=%s,"
54 + " tenantId=%s, deviceId=%s, adminStateUp=%s, state=%s,"
55 + " macAddress=%s, deviceOwner=%s, fixedIp=%s, bindingHostId=%s,"
56 + " bindingvnicType=%s, bindingvifType=%s, bindingvnicDetails=%s,"
57 + " allowedAddress=%s, securityGroups=%s";
58
59 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070060 protected void doExecute() {
jiangrui8143ad42015-08-12 15:26:20 +080061 VirtualPortService service = get(VirtualPortService.class);
62 if (vPortId != null && networkId == null && deviceId == null && tenantId == null) {
63 VirtualPort port = service.getPort(VirtualPortId.portId(vPortId));
64 printPort(port);
65 } else if (vPortId == null && networkId != null && deviceId == null && tenantId == null) {
66 Collection<VirtualPort> ports = service.getPorts(TenantNetworkId.networkId(networkId));
67 printPorts(ports);
68 } else if (vPortId == null && networkId == null && deviceId != null && tenantId == null) {
69 Collection<VirtualPort> ports = service.getPorts(DeviceId.deviceId(deviceId));
70 printPorts(ports);
71 } else if (vPortId == null && networkId == null && deviceId == null && tenantId != null) {
72 Collection<VirtualPort> ports = service.getPorts(DeviceId.deviceId(tenantId));
73 printPorts(ports);
74 } else if (vPortId == null && networkId == null && deviceId == null && tenantId == null) {
75 Collection<VirtualPort> ports = service.getPorts();
76 printPorts(ports);
77 } else {
78 print("cannot input more than one parameter");
79 }
80
81 }
82
83 private void printPorts(Collection<VirtualPort> ports) {
84 for (VirtualPort port : ports) {
85 printPort(port);
86 }
87 }
88
89 private void printPort(VirtualPort port) {
90 print(FMT, port.portId(), port.networkId(), port.name(), port.tenantId(), port.deviceId(),
91 port.adminStateUp(), port.state(), port.macAddress(), port.deviceOwner(), port
92 .fixedIps(), port.bindingHostId(), port.bindingVnicType(),
93 port.bindingVifType(), port.bindingVifDetails(), port.allowedAddressPairs(),
94 port.securityGroups());
95 }
96}