Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.apache.karaf.shell.commands.Option; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.net.AnnotationKeys; |
| 26 | import org.onosproject.net.Device; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.driver.DriverService; |
| 29 | import org.onosproject.utils.Comparators; |
| 30 | |
| 31 | import java.util.Collections; |
| 32 | import java.util.List; |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 33 | |
| 34 | import static com.google.common.collect.Lists.newArrayList; |
| 35 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Lists all infrastructure devices. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "devices", |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 40 | description = "Lists all infrastructure devices") |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 41 | public class DevicesListCommand extends AbstractShellCommand { |
| 42 | |
| 43 | private static final String FMT = |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 44 | "id=%s, available=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s, driver=%s%s"; |
| 45 | |
| 46 | private static final String FMT_SHORT = |
| 47 | "id=%s, available=%s, role=%s, type=%s, driver=%s"; |
| 48 | |
| 49 | @Option(name = "-s", aliases = "--short", description = "Show short output only", |
| 50 | required = false, multiValued = false) |
| 51 | private boolean shortOnly = false; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 52 | |
| 53 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 54 | protected void execute() { |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 55 | DeviceService deviceService = get(DeviceService.class); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 56 | if (outputJson()) { |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 57 | print("%s", json(getSortedDevices(deviceService))); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 58 | } else { |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 59 | for (Device device : getSortedDevices(deviceService)) { |
| 60 | printDevice(deviceService, device); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 61 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 62 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 63 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 66 | * Returns JSON node representing the specified devices. |
| 67 | * |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 68 | * @param devices collection of devices |
| 69 | * @return JSON node |
| 70 | */ |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 71 | private JsonNode json(Iterable<Device> devices) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 72 | ObjectMapper mapper = new ObjectMapper(); |
| 73 | ArrayNode result = mapper.createArrayNode(); |
| 74 | for (Device device : devices) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 75 | result.add(jsonForEntity(device, Device.class)); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 76 | } |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | /** |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 81 | * Returns the list of devices sorted using the device ID URIs. |
| 82 | * |
| 83 | * @param service device service |
| 84 | * @return sorted device list |
| 85 | */ |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 86 | public static List<Device> getSortedDevices(DeviceService service) { |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 87 | List<Device> devices = newArrayList(service.getDevices()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 88 | Collections.sort(devices, Comparators.ELEMENT_COMPARATOR); |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 89 | return devices; |
| 90 | } |
| 91 | |
| 92 | /** |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 93 | * Prints information about the specified device. |
| 94 | * |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 95 | * @param deviceService device service |
| 96 | * @param device infrastructure device |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 97 | */ |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 98 | protected void printDevice(DeviceService deviceService, Device device) { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 99 | if (device != null) { |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 100 | String driver = get(DriverService.class).getDriver(device.id()).name(); |
| 101 | if (shortOnly) { |
| 102 | print(FMT_SHORT, device.id(), deviceService.isAvailable(device.id()), |
| 103 | deviceService.getRole(device.id()), device.type(), driver); |
| 104 | } else { |
| 105 | print(FMT, device.id(), deviceService.isAvailable(device.id()), |
| 106 | deviceService.getRole(device.id()), device.type(), |
| 107 | device.manufacturer(), device.hwVersion(), device.swVersion(), |
| 108 | device.serialNumber(), driver, |
| 109 | annotations(device.annotations(), ImmutableSet.of(AnnotationKeys.DRIVER))); |
| 110 | } |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 111 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 112 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 113 | } |