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