blob: 4349922b343628595f769c8c39d49dac319c1c26 [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
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080018import java.util.List;
19import java.util.Optional;
Ray Milkey39616f32015-05-14 15:43:00 -070020import java.util.Set;
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080021import java.util.stream.Collectors;
Ray Milkey39616f32015-05-14 15:43:00 -070022
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070023import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.driver.Driver;
27import org.onosproject.net.driver.DriverAdminService;
28
Ray Milkey39616f32015-05-14 15:43:00 -070029import com.fasterxml.jackson.databind.node.ArrayNode;
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080030import com.google.common.collect.ImmutableList;
Ray Milkey39616f32015-05-14 15:43:00 -070031
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070032/**
33 * Lists device drivers.
34 */
35@Command(scope = "onos", name = "drivers",
36 description = "Lists device drivers")
37public class DriversListCommand extends AbstractShellCommand {
38
Thomas Vachuska635c2d72015-05-08 14:32:13 -070039 private static final String FMT = "driver=%s, extends=%s, mfr=%s, hw=%s, sw=%s";
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070040 private static final String FMT_B = " %s via %s";
41 private static final String FMT_P = " %s=%s";
42
43 @Argument(index = 0, name = "driverName", description = "Driver name",
44 required = false, multiValued = false)
45 String driverName = null;
46
47 @Override
48 protected void execute() {
49 DriverAdminService service = get(DriverAdminService.class);
50
51 if (driverName != null) {
52 printDriver(service.getDriver(driverName));
53 } else {
Ray Milkey39616f32015-05-14 15:43:00 -070054 if (outputJson()) {
55 json(service.getDrivers());
56 } else {
57 service.getDrivers().forEach(this::printDriver);
58 }
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070059 }
60 }
61
Ray Milkey39616f32015-05-14 15:43:00 -070062 private void json(Driver driver) {
63 print("%s", jsonForEntity(driver, Driver.class));
64 }
65
66 private void json(Set<Driver> drivers) {
67 ArrayNode result = mapper().createArrayNode();
68 drivers.forEach(driver -> result.add(jsonForEntity(driver, Driver.class)));
69 print("%s", result.toString());
70 }
71
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070072 private void printDriver(Driver driver) {
Ray Milkey39616f32015-05-14 15:43:00 -070073 if (outputJson()) {
74 json(driver);
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080075 } else {
76 List<String> parents = Optional.ofNullable(driver.parents())
77 .orElse(ImmutableList.of())
78 .stream()
79 .map(Driver::name)
80 .collect(Collectors.toList());
81
82 print(FMT, driver.name(), parents,
83 driver.manufacturer(), driver.hwVersion(), driver.swVersion());
Frank Wang2cdf4e32017-02-06 18:03:16 +080084 driver.behaviours().forEach(b -> print(FMT_B, b.getCanonicalName(),
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080085 driver.implementation(b).getCanonicalName()));
Frank Wang2cdf4e32017-02-06 18:03:16 +080086 driver.properties().forEach((k, v) -> print(FMT_P, k, v));
Ray Milkey39616f32015-05-14 15:43:00 -070087 }
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070088 }
89
90}