blob: a61705871c00a8280e175f77f7c5eb734029d32f [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Andreas Papazois1ed54cf2016-05-04 16:22:40 +030023import org.onosproject.net.Device;
24import org.onosproject.net.behaviour.InterfaceConfig;
25import org.onosproject.net.device.DeviceInterfaceDescription;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.driver.DriverHandler;
28import org.onosproject.net.driver.DriverService;
29
30import java.util.List;
31
32import static org.onosproject.net.DeviceId.deviceId;
33
34/**
35 * Lists all interfaces or interfaces of a device.
36 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Service
Andreas Papazois1ed54cf2016-05-04 16:22:40 +030038@Command(scope = "onos", name = "device-interfaces",
39 description = "Lists all interfaces or interfaces of a device.")
40public class DeviceInterfacesListCommand extends DevicesListCommand {
41 private static final String FORMAT = "%s";
42 private static final String MODE_FORMAT = " mode=";
43 private static final String ACCESS_MODE = "access";
44 private static final String TRUNK_MODE = "trunk";
45 private static final String VLAN_FORMAT = " vlan=";
46 private static final String LIMIT_FORMAT = " rate-limit=";
47 private static final String ERROR_RESULT = "Cannot retrieve interfaces for device";
48 private static final String NO_INTERFACES = "No interfaces found";
49 private static final String PERCENT = "%%";
50
51 @Argument(index = 0, name = "uri", description = "Device ID",
52 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070053 @Completion(DeviceIdCompleter.class)
Andreas Papazois1ed54cf2016-05-04 16:22:40 +030054 private String uri = null;
55
56 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057 protected void doExecute() {
Andreas Papazois1ed54cf2016-05-04 16:22:40 +030058 DeviceService deviceService = get(DeviceService.class);
59 DriverService driverService = get(DriverService.class);
60
61 if (uri == null) {
62 // No specific device, so all devices will be examined.
63 for (Device device : getSortedDevices(deviceService)) {
64 printDevice(deviceService, driverService, device);
65 }
66 } else {
67 Device device = deviceService.getDevice(deviceId(uri));
68 printDevice(deviceService, driverService, device);
69 }
70 }
71
72 private void printDevice(DeviceService deviceService,
73 DriverService driverService,
74 Device device) {
75 super.printDevice(deviceService, device);
76 if (!device.is(InterfaceConfig.class)) {
77 // The relevant behavior is not supported by the device.
78 print(ERROR_RESULT);
79 return;
80 }
81 DriverHandler h = driverService.createHandler(device.id());
82 InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
83
84 List<DeviceInterfaceDescription> interfaces =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030085 interfaceConfig.getInterfaces();
Andreas Papazois1ed54cf2016-05-04 16:22:40 +030086 if (interfaces == null) {
87 print(ERROR_RESULT);
88 } else if (interfaces.isEmpty()) {
89 print(NO_INTERFACES);
90 } else {
91 interfaces.forEach(this::printInterface);
92 }
93 }
94
95 private void printInterface(DeviceInterfaceDescription intf) {
96 StringBuilder formatStringBuilder = new StringBuilder(FORMAT);
97
98 if (intf.mode().equals(DeviceInterfaceDescription.Mode.ACCESS)) {
99 formatStringBuilder.append(MODE_FORMAT)
100 .append(ACCESS_MODE)
101 .append(VLAN_FORMAT);
102 formatStringBuilder.append(intf.vlans().get(0).toString());
103 } else if (intf.mode().equals(DeviceInterfaceDescription.Mode.TRUNK)) {
104 formatStringBuilder.append(MODE_FORMAT)
105 .append(TRUNK_MODE)
106 .append(VLAN_FORMAT);
107 for (int i = 0; i < intf.vlans().size(); i++) {
108 formatStringBuilder.append(intf.vlans().get(i));
109 if (i != intf.vlans().size() - 1) {
110 formatStringBuilder.append(",");
111 }
112 }
113 }
114
115 if (intf.isRateLimited()) {
116 formatStringBuilder.append(LIMIT_FORMAT);
117 formatStringBuilder.append(intf.rateLimit());
118 formatStringBuilder.append(PERCENT);
119 }
120
121 print(formatStringBuilder.toString(), intf.name());
122 }
123}