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