blob: 6a973ea072d1fdab5a71351b460474ea0bf2175d [file] [log] [blame]
Thomas Vachuska5c2f8132015-04-08 23:09:08 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.apache.karaf.shell.api.action.Option;
Andrea Campanellae6e2b762017-02-22 21:55:53 +010024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.driver.Behaviour;
26import org.onosproject.net.driver.Driver;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070027import org.onosproject.net.driver.DriverService;
Andrea Campanellae6e2b762017-02-22 21:55:53 +010028
Yuta HIGUCHI4a6852e2017-10-27 17:15:39 -070029import java.util.Comparator;
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080030import java.util.List;
31import java.util.Optional;
Ray Milkey39616f32015-05-14 15:43:00 -070032import java.util.Set;
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080033import java.util.stream.Collectors;
Ray Milkey39616f32015-05-14 15:43:00 -070034
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070035/**
36 * Lists device drivers.
37 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070038@Service
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070039@Command(scope = "onos", name = "drivers",
40 description = "Lists device drivers")
41public class DriversListCommand extends AbstractShellCommand {
42
Thomas Vachuska635c2d72015-05-08 14:32:13 -070043 private static final String FMT = "driver=%s, extends=%s, mfr=%s, hw=%s, sw=%s";
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070044 private static final String FMT_B = " %s via %s";
45 private static final String FMT_P = " %s=%s";
46
47 @Argument(index = 0, name = "driverName", description = "Driver name",
48 required = false, multiValued = false)
49 String driverName = null;
50
Yuta HIGUCHI4a6852e2017-10-27 17:15:39 -070051 @Option(name = "-s", aliases = "--sort", description = "Sort output by driver name",
52 required = false, multiValued = false)
53 private boolean sort = false;
54
55 @Option(name = "-n", aliases = "--name", description = "Show driver name only",
56 required = false, multiValued = false)
57 private boolean nameOnly = false;
58
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070059 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060 protected void doExecute() {
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070061 DriverService service = get(DriverService.class);
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070062
63 if (driverName != null) {
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010064 printDriver(service.getDriver(driverName), true);
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070065 } else {
Ray Milkey39616f32015-05-14 15:43:00 -070066 if (outputJson()) {
67 json(service.getDrivers());
68 } else {
Yuta HIGUCHI4a6852e2017-10-27 17:15:39 -070069 service.getDrivers()
70 .stream()
71 .sorted(Comparator.comparing(Driver::name))
72 .forEach(d -> printDriver(d, true));
Ray Milkey39616f32015-05-14 15:43:00 -070073 }
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070074 }
75 }
76
Ray Milkey39616f32015-05-14 15:43:00 -070077 private void json(Driver driver) {
78 print("%s", jsonForEntity(driver, Driver.class));
79 }
80
81 private void json(Set<Driver> drivers) {
82 ArrayNode result = mapper().createArrayNode();
83 drivers.forEach(driver -> result.add(jsonForEntity(driver, Driver.class)));
84 print("%s", result.toString());
85 }
86
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010087 private void printDriver(Driver driver, boolean first) {
Ray Milkey39616f32015-05-14 15:43:00 -070088 if (outputJson()) {
89 json(driver);
Yuta HIGUCHI4a6852e2017-10-27 17:15:39 -070090 } else if (nameOnly) {
91 print("%s", driver.name());
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080092 } else {
Andrea Campanellae6e2b762017-02-22 21:55:53 +010093 List<Driver> parents = Optional.ofNullable(driver.parents())
94 .orElse(ImmutableList.of());
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080095
Andrea Campanellae6e2b762017-02-22 21:55:53 +010096 List<String> parentsNames = parents.stream()
97 .map(Driver::name).collect(Collectors.toList());
98
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010099 if (first) {
100 print(FMT, driver.name(), parentsNames,
101 driver.manufacturer(), driver.hwVersion(), driver.swVersion());
102 } else {
103 print(" Inherited from %s", driver.name());
104 }
105
Andrea Campanellae6e2b762017-02-22 21:55:53 +0100106 driver.behaviours().forEach(b -> printBehaviour(b, driver));
Charles Chana59f9b762017-07-30 18:09:44 -0700107 driver.properties().forEach((k, v) -> print(FMT_P, k, v));
108
Andrea Campanella45f4d3d2017-02-23 14:45:21 +0100109 //recursion call to print each parent
110 parents.stream().forEach(parent -> printDriver(parent, false));
Ray Milkey39616f32015-05-14 15:43:00 -0700111 }
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700112 }
113
Andrea Campanellae6e2b762017-02-22 21:55:53 +0100114 private void printBehaviour(Class<? extends Behaviour> behaviour, Driver driver) {
115 print(FMT_B, behaviour.getCanonicalName(),
116 driver.implementation(behaviour).getCanonicalName());
117 }
118
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700119}