Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.cli.net; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.behaviour.InterfaceConfig; |
| 25 | import org.onosproject.net.device.DeviceInterfaceDescription; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.driver.DriverHandler; |
| 28 | import org.onosproject.net.driver.DriverService; |
| 29 | |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static org.onosproject.net.DeviceId.deviceId; |
| 33 | |
| 34 | /** |
| 35 | * Lists all interfaces or interfaces of a device. |
| 36 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | @Service |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 38 | @Command(scope = "onos", name = "device-interfaces", |
| 39 | description = "Lists all interfaces or interfaces of a device.") |
| 40 | public 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 Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 53 | @Completion(DeviceIdCompleter.class) |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 54 | private String uri = null; |
| 55 | |
| 56 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 57 | protected void doExecute() { |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 58 | 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 Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 85 | interfaceConfig.getInterfaces(); |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 86 | 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 | } |