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 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
tom | 32085cf | 2014-10-16 00:04:33 -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; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 24 | import org.onlab.onos.cli.Comparators; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.Device; |
| 26 | import org.onlab.onos.net.device.DeviceService; |
| 27 | |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 28 | import java.util.Collections; |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 29 | import java.util.List; |
| 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()) { |
| 47 | print("%s", json(service, getSortedDevices(service))); |
| 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 | * |
| 58 | * @param service device service |
| 59 | * @param devices collection of devices |
| 60 | * @return JSON node |
| 61 | */ |
| 62 | public static JsonNode json(DeviceService service, Iterable<Device> devices) { |
| 63 | ObjectMapper mapper = new ObjectMapper(); |
| 64 | ArrayNode result = mapper.createArrayNode(); |
| 65 | for (Device device : devices) { |
| 66 | result.add(json(service, mapper, device)); |
| 67 | } |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns JSON node representing the specified device. |
| 73 | * |
| 74 | * @param service device service |
| 75 | * @param mapper object mapper |
| 76 | * @param device infrastructure device |
| 77 | * @return JSON node |
| 78 | */ |
| 79 | public static ObjectNode json(DeviceService service, ObjectMapper mapper, |
| 80 | Device device) { |
| 81 | ObjectNode result = mapper.createObjectNode(); |
| 82 | if (device != null) { |
| 83 | result.put("id", device.id().toString()) |
| 84 | .put("available", service.isAvailable(device.id())) |
| 85 | .put("role", service.getRole(device.id()).toString()) |
| 86 | .put("mfr", device.manufacturer()) |
| 87 | .put("hw", device.hwVersion()) |
| 88 | .put("sw", device.swVersion()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 89 | .put("serial", device.serialNumber()) |
| 90 | .set("annotations", annotations(mapper, device.annotations())); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 91 | } |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | /** |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 96 | * Returns the list of devices sorted using the device ID URIs. |
| 97 | * |
| 98 | * @param service device service |
| 99 | * @return sorted device list |
| 100 | */ |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 101 | public static List<Device> getSortedDevices(DeviceService service) { |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 102 | List<Device> devices = newArrayList(service.getDevices()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 103 | Collections.sort(devices, Comparators.ELEMENT_COMPARATOR); |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 104 | return devices; |
| 105 | } |
| 106 | |
| 107 | /** |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 108 | * Prints information about the specified device. |
| 109 | * |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 110 | * @param service device service |
| 111 | * @param device infrastructure device |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 112 | */ |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 113 | protected void printDevice(DeviceService service, Device device) { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 114 | if (device != null) { |
| 115 | print(FMT, device.id(), service.isAvailable(device.id()), |
| 116 | service.getRole(device.id()), device.type(), |
| 117 | device.manufacturer(), device.hwVersion(), device.swVersion(), |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 118 | device.serialNumber(), annotations(device.annotations())); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 119 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 120 | } |
| 121 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 122 | } |