blob: c2078be76c8b17c8e56a88c9d151b9b123b0d3c7 [file] [log] [blame]
Andreas Papazois1ed54cf2016-05-04 16:22:40 +03001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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;
18
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onosproject.net.Device;
22import org.onosproject.net.behaviour.InterfaceConfig;
23import org.onosproject.net.device.DeviceInterfaceDescription;
24import org.onosproject.net.device.DeviceService;
25import org.onosproject.net.driver.DriverHandler;
26import org.onosproject.net.driver.DriverService;
27
28import java.util.List;
29
30import static org.onosproject.net.DeviceId.deviceId;
31
32/**
33 * Lists all interfaces or interfaces of a device.
34 */
35@Command(scope = "onos", name = "device-interfaces",
36 description = "Lists all interfaces or interfaces of a device.")
37public class DeviceInterfacesListCommand extends DevicesListCommand {
38 private static final String FORMAT = "%s";
39 private static final String MODE_FORMAT = " mode=";
40 private static final String ACCESS_MODE = "access";
41 private static final String TRUNK_MODE = "trunk";
42 private static final String VLAN_FORMAT = " vlan=";
43 private static final String LIMIT_FORMAT = " rate-limit=";
44 private static final String ERROR_RESULT = "Cannot retrieve interfaces for device";
45 private static final String NO_INTERFACES = "No interfaces found";
46 private static final String PERCENT = "%%";
47
48 @Argument(index = 0, name = "uri", description = "Device ID",
49 required = false, multiValued = false)
50 private String uri = null;
51
52 @Override
53 protected void execute() {
54 DeviceService deviceService = get(DeviceService.class);
55 DriverService driverService = get(DriverService.class);
56
57 if (uri == null) {
58 // No specific device, so all devices will be examined.
59 for (Device device : getSortedDevices(deviceService)) {
60 printDevice(deviceService, driverService, device);
61 }
62 } else {
63 Device device = deviceService.getDevice(deviceId(uri));
64 printDevice(deviceService, driverService, device);
65 }
66 }
67
68 private void printDevice(DeviceService deviceService,
69 DriverService driverService,
70 Device device) {
71 super.printDevice(deviceService, device);
72 if (!device.is(InterfaceConfig.class)) {
73 // The relevant behavior is not supported by the device.
74 print(ERROR_RESULT);
75 return;
76 }
77 DriverHandler h = driverService.createHandler(device.id());
78 InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
79
80 List<DeviceInterfaceDescription> interfaces =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030081 interfaceConfig.getInterfaces();
Andreas Papazois1ed54cf2016-05-04 16:22:40 +030082 if (interfaces == null) {
83 print(ERROR_RESULT);
84 } else if (interfaces.isEmpty()) {
85 print(NO_INTERFACES);
86 } else {
87 interfaces.forEach(this::printInterface);
88 }
89 }
90
91 private void printInterface(DeviceInterfaceDescription intf) {
92 StringBuilder formatStringBuilder = new StringBuilder(FORMAT);
93
94 if (intf.mode().equals(DeviceInterfaceDescription.Mode.ACCESS)) {
95 formatStringBuilder.append(MODE_FORMAT)
96 .append(ACCESS_MODE)
97 .append(VLAN_FORMAT);
98 formatStringBuilder.append(intf.vlans().get(0).toString());
99 } else if (intf.mode().equals(DeviceInterfaceDescription.Mode.TRUNK)) {
100 formatStringBuilder.append(MODE_FORMAT)
101 .append(TRUNK_MODE)
102 .append(VLAN_FORMAT);
103 for (int i = 0; i < intf.vlans().size(); i++) {
104 formatStringBuilder.append(intf.vlans().get(i));
105 if (i != intf.vlans().size() - 1) {
106 formatStringBuilder.append(",");
107 }
108 }
109 }
110
111 if (intf.isRateLimited()) {
112 formatStringBuilder.append(LIMIT_FORMAT);
113 formatStringBuilder.append(intf.rateLimit());
114 formatStringBuilder.append(PERCENT);
115 }
116
117 print(formatStringBuilder.toString(), intf.name());
118 }
119}