blob: d18a2dbb28e492fd27ccc2e83cc9e8f1833be9a7 [file] [log] [blame]
Thomas Vachuska5c2f8132015-04-08 23:09:08 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska5c2f8132015-04-08 23:09:08 -07003 *
4 * 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
7 *
8 * 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.
15 */
16package org.onosproject.cli.net;
17
Ray Milkey39616f32015-05-14 15:43:00 -070018import java.util.Set;
19
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070020import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.driver.Driver;
24import org.onosproject.net.driver.DriverAdminService;
25
Ray Milkey39616f32015-05-14 15:43:00 -070026import com.fasterxml.jackson.databind.node.ArrayNode;
27
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070028/**
29 * Lists device drivers.
30 */
31@Command(scope = "onos", name = "drivers",
32 description = "Lists device drivers")
33public class DriversListCommand extends AbstractShellCommand {
34
Thomas Vachuska635c2d72015-05-08 14:32:13 -070035 private static final String FMT = "driver=%s, extends=%s, mfr=%s, hw=%s, sw=%s";
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070036 private static final String FMT_B = " %s via %s";
37 private static final String FMT_P = " %s=%s";
38
39 @Argument(index = 0, name = "driverName", description = "Driver name",
40 required = false, multiValued = false)
41 String driverName = null;
42
43 @Override
44 protected void execute() {
45 DriverAdminService service = get(DriverAdminService.class);
46
47 if (driverName != null) {
48 printDriver(service.getDriver(driverName));
49 } else {
Ray Milkey39616f32015-05-14 15:43:00 -070050 if (outputJson()) {
51 json(service.getDrivers());
52 } else {
53 service.getDrivers().forEach(this::printDriver);
54 }
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070055 }
56 }
57
Ray Milkey39616f32015-05-14 15:43:00 -070058 private void json(Driver driver) {
59 print("%s", jsonForEntity(driver, Driver.class));
60 }
61
62 private void json(Set<Driver> drivers) {
63 ArrayNode result = mapper().createArrayNode();
64 drivers.forEach(driver -> result.add(jsonForEntity(driver, Driver.class)));
65 print("%s", result.toString());
66 }
67
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070068 private void printDriver(Driver driver) {
Ray Milkey39616f32015-05-14 15:43:00 -070069 if (outputJson()) {
70 json(driver);
Ray Milkey8e78f462017-01-20 13:07:47 -080071 } else if (driver.parents() != null) {
Yuta HIGUCHI61bb7f42017-01-19 20:05:28 -080072 driver.parents().forEach(parent -> {
73 print(FMT, driver.name(), parent != null ? parent.name() : "none",
74 driver.manufacturer(), driver.hwVersion(), driver.swVersion());
75 driver.behaviours().forEach(b -> print(FMT_B, b.getCanonicalName(),
76 driver.implementation(b).getCanonicalName()));
77 driver.properties().forEach((k, v) -> print(FMT_P, k, v));
78 });
Frank Wang2cdf4e32017-02-06 18:03:16 +080079 } else if (driver.parents() == null) {
80 print(FMT, driver.name(), "none", driver.manufacturer(),
81 driver.hwVersion(), driver.swVersion());
82 driver.behaviours().forEach(b -> print(FMT_B, b.getCanonicalName(),
83 driver.implementation(b).getCanonicalName()));
84 driver.properties().forEach((k, v) -> print(FMT_P, k, v));
Ray Milkey39616f32015-05-14 15:43:00 -070085 }
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070086 }
87
88}