blob: e40bcad6e33243329446a3104ada4efa8b326c82 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
tom6d2a43e2014-09-08 01:50:20 -070017
Ray Milkey3078fc02015-05-06 16:14:14 -070018import java.util.Collections;
19import java.util.List;
20
tom6d2a43e2014-09-08 01:50:20 -070021import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.cli.Comparators;
24import org.onosproject.net.Device;
25import org.onosproject.net.device.DeviceService;
tom6d2a43e2014-09-08 01:50:20 -070026
Ray Milkey3078fc02015-05-06 16:14:14 -070027import com.fasterxml.jackson.databind.JsonNode;
28import com.fasterxml.jackson.databind.ObjectMapper;
29import com.fasterxml.jackson.databind.node.ArrayNode;
tomd79f7ec2014-09-08 13:29:18 -070030
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()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070047 print("%s", json(getSortedDevices(service)));
tom32085cf2014-10-16 00:04:33 -070048 } 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 *
tom32085cf2014-10-16 00:04:33 -070058 * @param devices collection of devices
59 * @return JSON node
60 */
Ray Milkey3078fc02015-05-06 16:14:14 -070061 private JsonNode json(Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070062 ObjectMapper mapper = new ObjectMapper();
63 ArrayNode result = mapper.createArrayNode();
64 for (Device device : devices) {
Ray Milkey3078fc02015-05-06 16:14:14 -070065 result.add(jsonForEntity(device, Device.class));
tom32085cf2014-10-16 00:04:33 -070066 }
67 return result;
68 }
69
70 /**
tomd79f7ec2014-09-08 13:29:18 -070071 * Returns the list of devices sorted using the device ID URIs.
72 *
73 * @param service device service
74 * @return sorted device list
75 */
Thomas Vachuska444eda62014-10-28 13:09:42 -070076 public static List<Device> getSortedDevices(DeviceService service) {
tomd79f7ec2014-09-08 13:29:18 -070077 List<Device> devices = newArrayList(service.getDevices());
tom1380eee2014-09-24 09:22:02 -070078 Collections.sort(devices, Comparators.ELEMENT_COMPARATOR);
tomd79f7ec2014-09-08 13:29:18 -070079 return devices;
80 }
81
82 /**
tomff7eb7c2014-09-08 12:49:03 -070083 * Prints information about the specified device.
84 *
tomc290a122014-09-08 14:27:13 -070085 * @param service device service
86 * @param device infrastructure device
tomff7eb7c2014-09-08 12:49:03 -070087 */
tomc290a122014-09-08 14:27:13 -070088 protected void printDevice(DeviceService service, Device device) {
tom9eb57fb2014-09-11 19:42:38 -070089 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 Vachuska944cb6c2014-10-22 17:05:42 -070093 device.serialNumber(), annotations(device.annotations()));
tom9eb57fb2014-09-11 19:42:38 -070094 }
tomff7eb7c2014-09-08 12:49:03 -070095 }
96
tom6d2a43e2014-09-08 01:50:20 -070097}