Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli.net; |
| 20 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Command; |
| 26 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 27 | import org.onlab.onos.cli.Comparators; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.Device; |
| 29 | import org.onlab.onos.net.device.DeviceService; |
| 30 | |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 31 | import java.util.Collections; |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 32 | import java.util.List; |
| 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", |
| 40 | description = "Lists all infrastructure devices") |
| 41 | public class DevicesListCommand extends AbstractShellCommand { |
| 42 | |
| 43 | private static final String FMT = |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 44 | "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] | 45 | |
| 46 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 47 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 48 | DeviceService service = get(DeviceService.class); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 49 | if (outputJson()) { |
| 50 | print("%s", json(service, getSortedDevices(service))); |
| 51 | } else { |
| 52 | for (Device device : getSortedDevices(service)) { |
| 53 | printDevice(service, device); |
| 54 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 55 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 56 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 59 | * Returns JSON node representing the specified devices. |
| 60 | * |
| 61 | * @param service device service |
| 62 | * @param devices collection of devices |
| 63 | * @return JSON node |
| 64 | */ |
| 65 | public static JsonNode json(DeviceService service, Iterable<Device> devices) { |
| 66 | ObjectMapper mapper = new ObjectMapper(); |
| 67 | ArrayNode result = mapper.createArrayNode(); |
| 68 | for (Device device : devices) { |
| 69 | result.add(json(service, mapper, device)); |
| 70 | } |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns JSON node representing the specified device. |
| 76 | * |
| 77 | * @param service device service |
| 78 | * @param mapper object mapper |
| 79 | * @param device infrastructure device |
| 80 | * @return JSON node |
| 81 | */ |
| 82 | public static ObjectNode json(DeviceService service, ObjectMapper mapper, |
| 83 | Device device) { |
| 84 | ObjectNode result = mapper.createObjectNode(); |
| 85 | if (device != null) { |
| 86 | result.put("id", device.id().toString()) |
| 87 | .put("available", service.isAvailable(device.id())) |
| 88 | .put("role", service.getRole(device.id()).toString()) |
| 89 | .put("mfr", device.manufacturer()) |
| 90 | .put("hw", device.hwVersion()) |
| 91 | .put("sw", device.swVersion()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 92 | .put("serial", device.serialNumber()) |
| 93 | .set("annotations", annotations(mapper, device.annotations())); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 94 | } |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | /** |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 99 | * Returns the list of devices sorted using the device ID URIs. |
| 100 | * |
| 101 | * @param service device service |
| 102 | * @return sorted device list |
| 103 | */ |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 104 | public static List<Device> getSortedDevices(DeviceService service) { |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 105 | List<Device> devices = newArrayList(service.getDevices()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 106 | Collections.sort(devices, Comparators.ELEMENT_COMPARATOR); |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 107 | return devices; |
| 108 | } |
| 109 | |
| 110 | /** |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 111 | * Prints information about the specified device. |
| 112 | * |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 113 | * @param service device service |
| 114 | * @param device infrastructure device |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 115 | */ |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 116 | protected void printDevice(DeviceService service, Device device) { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 117 | if (device != null) { |
| 118 | print(FMT, device.id(), service.isAvailable(device.id()), |
| 119 | service.getRole(device.id()), device.type(), |
| 120 | device.manufacturer(), device.hwVersion(), device.swVersion(), |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 121 | device.serialNumber(), annotations(device.annotations())); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 122 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 123 | } |
| 124 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 125 | } |