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