blob: 79fb65aae4a2c2b675e5e571156d6286b49618a0 [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;
20import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
Yuta HIGUCHI4a6852e2017-10-27 17:15:39 -070022import org.apache.karaf.shell.commands.Option;
Andrea Campanellae6e2b762017-02-22 21:55:53 +010023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.driver.Behaviour;
25import org.onosproject.net.driver.Driver;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070026import org.onosproject.net.driver.DriverService;
Andrea Campanellae6e2b762017-02-22 21:55:53 +010027
Yuta HIGUCHI4a6852e2017-10-27 17:15:39 -070028import java.util.Comparator;
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080029import java.util.List;
30import java.util.Optional;
Ray Milkey39616f32015-05-14 15:43:00 -070031import java.util.Set;
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080032import java.util.stream.Collectors;
Ray Milkey39616f32015-05-14 15:43:00 -070033
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070034/**
35 * Lists device drivers.
36 */
37@Command(scope = "onos", name = "drivers",
38 description = "Lists device drivers")
39public class DriversListCommand extends AbstractShellCommand {
40
Thomas Vachuska635c2d72015-05-08 14:32:13 -070041 private static final String FMT = "driver=%s, extends=%s, mfr=%s, hw=%s, sw=%s";
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070042 private static final String FMT_B = " %s via %s";
43 private static final String FMT_P = " %s=%s";
44
45 @Argument(index = 0, name = "driverName", description = "Driver name",
46 required = false, multiValued = false)
47 String driverName = null;
48
Yuta HIGUCHI4a6852e2017-10-27 17:15:39 -070049 @Option(name = "-s", aliases = "--sort", description = "Sort output by driver name",
50 required = false, multiValued = false)
51 private boolean sort = false;
52
53 @Option(name = "-n", aliases = "--name", description = "Show driver name only",
54 required = false, multiValued = false)
55 private boolean nameOnly = false;
56
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070057 @Override
58 protected void execute() {
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070059 DriverService service = get(DriverService.class);
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070060
61 if (driverName != null) {
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010062 printDriver(service.getDriver(driverName), true);
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070063 } else {
Ray Milkey39616f32015-05-14 15:43:00 -070064 if (outputJson()) {
65 json(service.getDrivers());
66 } else {
Yuta HIGUCHI4a6852e2017-10-27 17:15:39 -070067 service.getDrivers()
68 .stream()
69 .sorted(Comparator.comparing(Driver::name))
70 .forEach(d -> printDriver(d, true));
Ray Milkey39616f32015-05-14 15:43:00 -070071 }
Thomas Vachuska5c2f8132015-04-08 23:09:08 -070072 }
73 }
74
Ray Milkey39616f32015-05-14 15:43:00 -070075 private void json(Driver driver) {
76 print("%s", jsonForEntity(driver, Driver.class));
77 }
78
79 private void json(Set<Driver> drivers) {
80 ArrayNode result = mapper().createArrayNode();
81 drivers.forEach(driver -> result.add(jsonForEntity(driver, Driver.class)));
82 print("%s", result.toString());
83 }
84
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010085 private void printDriver(Driver driver, boolean first) {
Ray Milkey39616f32015-05-14 15:43:00 -070086 if (outputJson()) {
87 json(driver);
Yuta HIGUCHI4a6852e2017-10-27 17:15:39 -070088 } else if (nameOnly) {
89 print("%s", driver.name());
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080090 } else {
Andrea Campanellae6e2b762017-02-22 21:55:53 +010091 List<Driver> parents = Optional.ofNullable(driver.parents())
92 .orElse(ImmutableList.of());
Yuta HIGUCHI0083a512017-02-21 12:14:38 -080093
Andrea Campanellae6e2b762017-02-22 21:55:53 +010094 List<String> parentsNames = parents.stream()
95 .map(Driver::name).collect(Collectors.toList());
96
Andrea Campanella45f4d3d2017-02-23 14:45:21 +010097 if (first) {
98 print(FMT, driver.name(), parentsNames,
99 driver.manufacturer(), driver.hwVersion(), driver.swVersion());
100 } else {
101 print(" Inherited from %s", driver.name());
102 }
103
Andrea Campanellae6e2b762017-02-22 21:55:53 +0100104 driver.behaviours().forEach(b -> printBehaviour(b, driver));
Charles Chana59f9b762017-07-30 18:09:44 -0700105 driver.properties().forEach((k, v) -> print(FMT_P, k, v));
106
Andrea Campanella45f4d3d2017-02-23 14:45:21 +0100107 //recursion call to print each parent
108 parents.stream().forEach(parent -> printDriver(parent, false));
Ray Milkey39616f32015-05-14 15:43:00 -0700109 }
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700110 }
111
Andrea Campanellae6e2b762017-02-22 21:55:53 +0100112 private void printBehaviour(Class<? extends Behaviour> behaviour, Driver driver) {
113 print(FMT_B, behaviour.getCanonicalName(),
114 driver.implementation(behaviour).getCanonicalName());
115 }
116
Thomas Vachuska5c2f8132015-04-08 23:09:08 -0700117}