blob: cf90604446f89a168bcdeb6bd98c0cae43fa9b87 [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
24import org.apache.karaf.shell.api.action.Option;
Andrea Campanella13032532016-03-31 10:50:50 -070025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.AnnotationKeys;
27import org.onosproject.net.Device;
28import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.driver.DriverService;
30import org.onosproject.utils.Comparators;
31
32import java.util.Collections;
33import java.util.List;
tomd79f7ec2014-09-08 13:29:18 -070034
35import static com.google.common.collect.Lists.newArrayList;
36
tom6d2a43e2014-09-08 01:50:20 -070037/**
38 * Lists all infrastructure devices.
39 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
tom6d2a43e2014-09-08 01:50:20 -070041@Command(scope = "onos", name = "devices",
Andrea Campanella13032532016-03-31 10:50:50 -070042 description = "Lists all infrastructure devices")
tom6d2a43e2014-09-08 01:50:20 -070043public class DevicesListCommand extends AbstractShellCommand {
44
45 private static final String FMT =
Andrea Campanellaab7d9ab2018-05-02 15:13:54 +020046 "id=%s, available=%s, local-status=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, " +
47 "serial=%s, chassis=%s, driver=%s%s";
Andrea Campanella13032532016-03-31 10:50:50 -070048
49 private static final String FMT_SHORT =
50 "id=%s, available=%s, role=%s, type=%s, driver=%s";
51
52 @Option(name = "-s", aliases = "--short", description = "Show short output only",
53 required = false, multiValued = false)
54 private boolean shortOnly = false;
tom6d2a43e2014-09-08 01:50:20 -070055
56 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057 protected void doExecute() {
Andrea Campanella13032532016-03-31 10:50:50 -070058 DeviceService deviceService = get(DeviceService.class);
tom32085cf2014-10-16 00:04:33 -070059 if (outputJson()) {
Andrea Campanella13032532016-03-31 10:50:50 -070060 print("%s", json(getSortedDevices(deviceService)));
tom32085cf2014-10-16 00:04:33 -070061 } else {
Andrea Campanella13032532016-03-31 10:50:50 -070062 for (Device device : getSortedDevices(deviceService)) {
63 printDevice(deviceService, device);
tom32085cf2014-10-16 00:04:33 -070064 }
tom6d2a43e2014-09-08 01:50:20 -070065 }
tom6d2a43e2014-09-08 01:50:20 -070066 }
tomff7eb7c2014-09-08 12:49:03 -070067
68 /**
tom32085cf2014-10-16 00:04:33 -070069 * Returns JSON node representing the specified devices.
70 *
tom32085cf2014-10-16 00:04:33 -070071 * @param devices collection of devices
72 * @return JSON node
73 */
Ray Milkey3078fc02015-05-06 16:14:14 -070074 private JsonNode json(Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070075 ObjectMapper mapper = new ObjectMapper();
76 ArrayNode result = mapper.createArrayNode();
77 for (Device device : devices) {
Ray Milkey3078fc02015-05-06 16:14:14 -070078 result.add(jsonForEntity(device, Device.class));
tom32085cf2014-10-16 00:04:33 -070079 }
80 return result;
81 }
82
83 /**
tomd79f7ec2014-09-08 13:29:18 -070084 * Returns the list of devices sorted using the device ID URIs.
85 *
86 * @param service device service
87 * @return sorted device list
88 */
Thomas Vachuska444eda62014-10-28 13:09:42 -070089 public static List<Device> getSortedDevices(DeviceService service) {
tomd79f7ec2014-09-08 13:29:18 -070090 List<Device> devices = newArrayList(service.getDevices());
tom1380eee2014-09-24 09:22:02 -070091 Collections.sort(devices, Comparators.ELEMENT_COMPARATOR);
tomd79f7ec2014-09-08 13:29:18 -070092 return devices;
93 }
94
95 /**
tomff7eb7c2014-09-08 12:49:03 -070096 * Prints information about the specified device.
97 *
Andrea Campanella13032532016-03-31 10:50:50 -070098 * @param deviceService device service
99 * @param device infrastructure device
tomff7eb7c2014-09-08 12:49:03 -0700100 */
Andrea Campanella13032532016-03-31 10:50:50 -0700101 protected void printDevice(DeviceService deviceService, Device device) {
tom9eb57fb2014-09-11 19:42:38 -0700102 if (device != null) {
Andrea Campanella13032532016-03-31 10:50:50 -0700103 String driver = get(DriverService.class).getDriver(device.id()).name();
104 if (shortOnly) {
105 print(FMT_SHORT, device.id(), deviceService.isAvailable(device.id()),
106 deviceService.getRole(device.id()), device.type(), driver);
107 } else {
108 print(FMT, device.id(), deviceService.isAvailable(device.id()),
Saurav Dasd5ec9e92017-01-17 10:40:18 -0800109 deviceService.localStatus(device.id()),
Andrea Campanella13032532016-03-31 10:50:50 -0700110 deviceService.getRole(device.id()), device.type(),
111 device.manufacturer(), device.hwVersion(), device.swVersion(),
Andrea Campanellaab7d9ab2018-05-02 15:13:54 +0200112 device.serialNumber(), device.chassisId(), driver,
Andrea Campanella13032532016-03-31 10:50:50 -0700113 annotations(device.annotations(), ImmutableSet.of(AnnotationKeys.DRIVER)));
114 }
tom9eb57fb2014-09-11 19:42:38 -0700115 }
tomff7eb7c2014-09-08 12:49:03 -0700116 }
tom6d2a43e2014-09-08 01:50:20 -0700117}