blob: d4dd14efb15c0e2687fac78b19f2d7cad51efc1a [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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 com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
Andrea Campanella13032532016-03-31 10:50:50 -070021import com.google.common.collect.ImmutableSet;
22import org.apache.karaf.shell.commands.Command;
23import org.apache.karaf.shell.commands.Option;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.AnnotationKeys;
26import org.onosproject.net.Device;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.driver.DriverService;
29import org.onosproject.utils.Comparators;
30
31import java.util.Collections;
32import java.util.List;
tomd79f7ec2014-09-08 13:29:18 -070033
34import static com.google.common.collect.Lists.newArrayList;
35
tom6d2a43e2014-09-08 01:50:20 -070036/**
37 * Lists all infrastructure devices.
38 */
39@Command(scope = "onos", name = "devices",
Andrea Campanella13032532016-03-31 10:50:50 -070040 description = "Lists all infrastructure devices")
tom6d2a43e2014-09-08 01:50:20 -070041public class DevicesListCommand extends AbstractShellCommand {
42
43 private static final String FMT =
Saurav Dasd5ec9e92017-01-17 10:40:18 -080044 "id=%s, available=%s, local-status=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s, driver=%s%s";
Andrea Campanella13032532016-03-31 10:50:50 -070045
46 private static final String FMT_SHORT =
47 "id=%s, available=%s, role=%s, type=%s, driver=%s";
48
49 @Option(name = "-s", aliases = "--short", description = "Show short output only",
50 required = false, multiValued = false)
51 private boolean shortOnly = false;
tom6d2a43e2014-09-08 01:50:20 -070052
53 @Override
tom0872a172014-09-23 11:24:26 -070054 protected void execute() {
Andrea Campanella13032532016-03-31 10:50:50 -070055 DeviceService deviceService = get(DeviceService.class);
tom32085cf2014-10-16 00:04:33 -070056 if (outputJson()) {
Andrea Campanella13032532016-03-31 10:50:50 -070057 print("%s", json(getSortedDevices(deviceService)));
tom32085cf2014-10-16 00:04:33 -070058 } else {
Andrea Campanella13032532016-03-31 10:50:50 -070059 for (Device device : getSortedDevices(deviceService)) {
60 printDevice(deviceService, device);
tom32085cf2014-10-16 00:04:33 -070061 }
tom6d2a43e2014-09-08 01:50:20 -070062 }
tom6d2a43e2014-09-08 01:50:20 -070063 }
tomff7eb7c2014-09-08 12:49:03 -070064
65 /**
tom32085cf2014-10-16 00:04:33 -070066 * Returns JSON node representing the specified devices.
67 *
tom32085cf2014-10-16 00:04:33 -070068 * @param devices collection of devices
69 * @return JSON node
70 */
Ray Milkey3078fc02015-05-06 16:14:14 -070071 private JsonNode json(Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070072 ObjectMapper mapper = new ObjectMapper();
73 ArrayNode result = mapper.createArrayNode();
74 for (Device device : devices) {
Ray Milkey3078fc02015-05-06 16:14:14 -070075 result.add(jsonForEntity(device, Device.class));
tom32085cf2014-10-16 00:04:33 -070076 }
77 return result;
78 }
79
80 /**
tomd79f7ec2014-09-08 13:29:18 -070081 * Returns the list of devices sorted using the device ID URIs.
82 *
83 * @param service device service
84 * @return sorted device list
85 */
Thomas Vachuska444eda62014-10-28 13:09:42 -070086 public static List<Device> getSortedDevices(DeviceService service) {
tomd79f7ec2014-09-08 13:29:18 -070087 List<Device> devices = newArrayList(service.getDevices());
tom1380eee2014-09-24 09:22:02 -070088 Collections.sort(devices, Comparators.ELEMENT_COMPARATOR);
tomd79f7ec2014-09-08 13:29:18 -070089 return devices;
90 }
91
92 /**
tomff7eb7c2014-09-08 12:49:03 -070093 * Prints information about the specified device.
94 *
Andrea Campanella13032532016-03-31 10:50:50 -070095 * @param deviceService device service
96 * @param device infrastructure device
tomff7eb7c2014-09-08 12:49:03 -070097 */
Andrea Campanella13032532016-03-31 10:50:50 -070098 protected void printDevice(DeviceService deviceService, Device device) {
tom9eb57fb2014-09-11 19:42:38 -070099 if (device != null) {
Andrea Campanella13032532016-03-31 10:50:50 -0700100 String driver = get(DriverService.class).getDriver(device.id()).name();
101 if (shortOnly) {
102 print(FMT_SHORT, device.id(), deviceService.isAvailable(device.id()),
103 deviceService.getRole(device.id()), device.type(), driver);
104 } else {
105 print(FMT, device.id(), deviceService.isAvailable(device.id()),
Saurav Dasd5ec9e92017-01-17 10:40:18 -0800106 deviceService.localStatus(device.id()),
Andrea Campanella13032532016-03-31 10:50:50 -0700107 deviceService.getRole(device.id()), device.type(),
108 device.manufacturer(), device.hwVersion(), device.swVersion(),
109 device.serialNumber(), driver,
110 annotations(device.annotations(), ImmutableSet.of(AnnotationKeys.DRIVER)));
111 }
tom9eb57fb2014-09-11 19:42:38 -0700112 }
tomff7eb7c2014-09-08 12:49:03 -0700113 }
tom6d2a43e2014-09-08 01:50:20 -0700114}