blob: fd6f230e734ba34b87235cc91f33990db2d7c2ad [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
Andrea Campanellae6e2b762017-02-22 21:55:53 +010018import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.google.common.collect.ImmutableList;
20import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.driver.Behaviour;
24import org.onosproject.net.driver.Driver;
25import org.onosproject.net.driver.DriverAdminService;
26
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080027import java.util.List;
28import java.util.Optional;
Ray Milkey39616f32015-05-14 15:43:00 -070029import java.util.Set;
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080030import java.util.stream.Collectors;
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) {
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010052 printDriver(service.getDriver(driverName), true);
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070053 } else {
Ray Milkey39616f32015-05-14 15:43:00 -070054 if (outputJson()) {
55 json(service.getDrivers());
56 } else {
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010057 service.getDrivers().forEach(d -> printDriver(d, true));
Ray Milkey39616f32015-05-14 15:43:00 -070058 }
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
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010072 private void printDriver(Driver driver, boolean first) {
Ray Milkey39616f32015-05-14 15:43:00 -070073 if (outputJson()) {
74 json(driver);
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080075 } else {
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010076
Andrea Campanellae6e2b762017-02-22 21:55:53 +010077 List<Driver> parents = Optional.ofNullable(driver.parents())
78 .orElse(ImmutableList.of());
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080079
Andrea Campanellae6e2b762017-02-22 21:55:53 +010080 List<String> parentsNames = parents.stream()
81 .map(Driver::name).collect(Collectors.toList());
82
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010083 if (first) {
84 print(FMT, driver.name(), parentsNames,
85 driver.manufacturer(), driver.hwVersion(), driver.swVersion());
86 } else {
87 print(" Inherited from %s", driver.name());
88 }
89
Andrea Campanellae6e2b762017-02-22 21:55:53 +010090 driver.behaviours().forEach(b -> printBehaviour(b, driver));
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010091 //recursion call to print each parent
92 parents.stream().forEach(parent -> printDriver(parent, false));
Frank Wang2cdf4e32017-02-06 18:03:16 +080093 driver.properties().forEach((k, v) -> print(FMT_P, k, v));
Ray Milkey39616f32015-05-14 15:43:00 -070094 }
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070095 }
96
Andrea Campanellae6e2b762017-02-22 21:55:53 +010097 private void printBehaviour(Class<? extends Behaviour> behaviour, Driver driver) {
98 print(FMT_B, behaviour.getCanonicalName(),
99 driver.implementation(behaviour).getCanonicalName());
100 }
101
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700102}