blob: f981411da097e67a66f3f3f3bf33a3c4a7b57e34 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
tom6d2a43e2014-09-08 01:50:20 -070016package org.onlab.onos.cli.net;
17
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
tom6d2a43e2014-09-08 01:50:20 -070022import org.apache.karaf.shell.commands.Command;
23import org.onlab.onos.cli.AbstractShellCommand;
tom91c7bd02014-09-25 22:50:44 -070024import org.onlab.onos.cli.Comparators;
tom6d2a43e2014-09-08 01:50:20 -070025import org.onlab.onos.net.Device;
26import org.onlab.onos.net.device.DeviceService;
27
tomd79f7ec2014-09-08 13:29:18 -070028import java.util.Collections;
tomd79f7ec2014-09-08 13:29:18 -070029import java.util.List;
30
31import static com.google.common.collect.Lists.newArrayList;
32
tom6d2a43e2014-09-08 01:50:20 -070033/**
34 * Lists all infrastructure devices.
35 */
36@Command(scope = "onos", name = "devices",
37 description = "Lists all infrastructure devices")
38public class DevicesListCommand extends AbstractShellCommand {
39
40 private static final String FMT =
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070041 "id=%s, available=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s%s";
tom6d2a43e2014-09-08 01:50:20 -070042
43 @Override
tom0872a172014-09-23 11:24:26 -070044 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070045 DeviceService service = get(DeviceService.class);
tom32085cf2014-10-16 00:04:33 -070046 if (outputJson()) {
47 print("%s", json(service, getSortedDevices(service)));
48 } else {
49 for (Device device : getSortedDevices(service)) {
50 printDevice(service, device);
51 }
tom6d2a43e2014-09-08 01:50:20 -070052 }
tom6d2a43e2014-09-08 01:50:20 -070053 }
tomff7eb7c2014-09-08 12:49:03 -070054
55 /**
tom32085cf2014-10-16 00:04:33 -070056 * 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()))
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -080085 .put("type", device.type().toString())
tom32085cf2014-10-16 00:04:33 -070086 .put("role", service.getRole(device.id()).toString())
87 .put("mfr", device.manufacturer())
88 .put("hw", device.hwVersion())
89 .put("sw", device.swVersion())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070090 .put("serial", device.serialNumber())
91 .set("annotations", annotations(mapper, device.annotations()));
tom32085cf2014-10-16 00:04:33 -070092 }
93 return result;
94 }
95
96 /**
tomd79f7ec2014-09-08 13:29:18 -070097 * Returns the list of devices sorted using the device ID URIs.
98 *
99 * @param service device service
100 * @return sorted device list
101 */
Thomas Vachuska444eda62014-10-28 13:09:42 -0700102 public static List<Device> getSortedDevices(DeviceService service) {
tomd79f7ec2014-09-08 13:29:18 -0700103 List<Device> devices = newArrayList(service.getDevices());
tom1380eee2014-09-24 09:22:02 -0700104 Collections.sort(devices, Comparators.ELEMENT_COMPARATOR);
tomd79f7ec2014-09-08 13:29:18 -0700105 return devices;
106 }
107
108 /**
tomff7eb7c2014-09-08 12:49:03 -0700109 * Prints information about the specified device.
110 *
tomc290a122014-09-08 14:27:13 -0700111 * @param service device service
112 * @param device infrastructure device
tomff7eb7c2014-09-08 12:49:03 -0700113 */
tomc290a122014-09-08 14:27:13 -0700114 protected void printDevice(DeviceService service, Device device) {
tom9eb57fb2014-09-11 19:42:38 -0700115 if (device != null) {
116 print(FMT, device.id(), service.isAvailable(device.id()),
117 service.getRole(device.id()), device.type(),
118 device.manufacturer(), device.hwVersion(), device.swVersion(),
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700119 device.serialNumber(), annotations(device.annotations()));
tom9eb57fb2014-09-11 19:42:38 -0700120 }
tomff7eb7c2014-09-08 12:49:03 -0700121 }
122
tom6d2a43e2014-09-08 01:50:20 -0700123}