blob: b81e2c5dbed7695c8e1e7baa86fe3a5feb237aea [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
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 */
tom6d2a43e2014-09-08 01:50:20 -070019package org.onlab.onos.cli.net;
20
tom32085cf2014-10-16 00:04:33 -070021import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
24import com.fasterxml.jackson.databind.node.ObjectNode;
tom6d2a43e2014-09-08 01:50:20 -070025import org.apache.karaf.shell.commands.Command;
26import org.onlab.onos.cli.AbstractShellCommand;
tom91c7bd02014-09-25 22:50:44 -070027import org.onlab.onos.cli.Comparators;
tom6d2a43e2014-09-08 01:50:20 -070028import org.onlab.onos.net.Device;
29import org.onlab.onos.net.device.DeviceService;
30
tomd79f7ec2014-09-08 13:29:18 -070031import java.util.Collections;
tomd79f7ec2014-09-08 13:29:18 -070032import java.util.List;
33
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",
40 description = "Lists all infrastructure devices")
41public class DevicesListCommand extends AbstractShellCommand {
42
43 private static final String FMT =
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070044 "id=%s, available=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s%s";
tom6d2a43e2014-09-08 01:50:20 -070045
46 @Override
tom0872a172014-09-23 11:24:26 -070047 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070048 DeviceService service = get(DeviceService.class);
tom32085cf2014-10-16 00:04:33 -070049 if (outputJson()) {
50 print("%s", json(service, getSortedDevices(service)));
51 } else {
52 for (Device device : getSortedDevices(service)) {
53 printDevice(service, device);
54 }
tom6d2a43e2014-09-08 01:50:20 -070055 }
tom6d2a43e2014-09-08 01:50:20 -070056 }
tomff7eb7c2014-09-08 12:49:03 -070057
58 /**
tom32085cf2014-10-16 00:04:33 -070059 * 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 Vachuska944cb6c2014-10-22 17:05:42 -070092 .put("serial", device.serialNumber())
93 .set("annotations", annotations(mapper, device.annotations()));
tom32085cf2014-10-16 00:04:33 -070094 }
95 return result;
96 }
97
98 /**
tomd79f7ec2014-09-08 13:29:18 -070099 * Returns the list of devices sorted using the device ID URIs.
100 *
101 * @param service device service
102 * @return sorted device list
103 */
Thomas Vachuska444eda62014-10-28 13:09:42 -0700104 public static List<Device> getSortedDevices(DeviceService service) {
tomd79f7ec2014-09-08 13:29:18 -0700105 List<Device> devices = newArrayList(service.getDevices());
tom1380eee2014-09-24 09:22:02 -0700106 Collections.sort(devices, Comparators.ELEMENT_COMPARATOR);
tomd79f7ec2014-09-08 13:29:18 -0700107 return devices;
108 }
109
110 /**
tomff7eb7c2014-09-08 12:49:03 -0700111 * Prints information about the specified device.
112 *
tomc290a122014-09-08 14:27:13 -0700113 * @param service device service
114 * @param device infrastructure device
tomff7eb7c2014-09-08 12:49:03 -0700115 */
tomc290a122014-09-08 14:27:13 -0700116 protected void printDevice(DeviceService service, Device device) {
tom9eb57fb2014-09-11 19:42:38 -0700117 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 Vachuska944cb6c2014-10-22 17:05:42 -0700121 device.serialNumber(), annotations(device.annotations()));
tom9eb57fb2014-09-11 19:42:38 -0700122 }
tomff7eb7c2014-09-08 12:49:03 -0700123 }
124
tom6d2a43e2014-09-08 01:50:20 -0700125}