Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
Andrea Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.apache.karaf.shell.commands.Argument; |
| 21 | import org.apache.karaf.shell.commands.Command; |
Yuta HIGUCHI | 4a6852e | 2017-10-27 17:15:39 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Option; |
Andrea Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.net.driver.Behaviour; |
| 25 | import org.onosproject.net.driver.Driver; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 26 | import org.onosproject.net.driver.DriverService; |
Andrea Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 27 | |
Yuta HIGUCHI | 4a6852e | 2017-10-27 17:15:39 -0700 | [diff] [blame] | 28 | import java.util.Comparator; |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 29 | import java.util.List; |
| 30 | import java.util.Optional; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 31 | import java.util.Set; |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 32 | import java.util.stream.Collectors; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 33 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Lists device drivers. |
| 36 | */ |
| 37 | @Command(scope = "onos", name = "drivers", |
| 38 | description = "Lists device drivers") |
| 39 | public class DriversListCommand extends AbstractShellCommand { |
| 40 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 41 | private static final String FMT = "driver=%s, extends=%s, mfr=%s, hw=%s, sw=%s"; |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 42 | 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 HIGUCHI | 4a6852e | 2017-10-27 17:15:39 -0700 | [diff] [blame] | 49 | @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 Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 57 | @Override |
| 58 | protected void execute() { |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 59 | DriverService service = get(DriverService.class); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 60 | |
| 61 | if (driverName != null) { |
Andrea Campanella | 45f4d3d | 2017-02-23 14:45:21 +0100 | [diff] [blame] | 62 | printDriver(service.getDriver(driverName), true); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 63 | } else { |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 64 | if (outputJson()) { |
| 65 | json(service.getDrivers()); |
| 66 | } else { |
Yuta HIGUCHI | 4a6852e | 2017-10-27 17:15:39 -0700 | [diff] [blame] | 67 | service.getDrivers() |
| 68 | .stream() |
| 69 | .sorted(Comparator.comparing(Driver::name)) |
| 70 | .forEach(d -> printDriver(d, true)); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 71 | } |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 75 | 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 Campanella | 45f4d3d | 2017-02-23 14:45:21 +0100 | [diff] [blame] | 85 | private void printDriver(Driver driver, boolean first) { |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 86 | if (outputJson()) { |
| 87 | json(driver); |
Yuta HIGUCHI | 4a6852e | 2017-10-27 17:15:39 -0700 | [diff] [blame] | 88 | } else if (nameOnly) { |
| 89 | print("%s", driver.name()); |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 90 | } else { |
Andrea Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 91 | List<Driver> parents = Optional.ofNullable(driver.parents()) |
| 92 | .orElse(ImmutableList.of()); |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 93 | |
Andrea Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 94 | List<String> parentsNames = parents.stream() |
| 95 | .map(Driver::name).collect(Collectors.toList()); |
| 96 | |
Andrea Campanella | 45f4d3d | 2017-02-23 14:45:21 +0100 | [diff] [blame] | 97 | 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 Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 104 | driver.behaviours().forEach(b -> printBehaviour(b, driver)); |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 105 | driver.properties().forEach((k, v) -> print(FMT_P, k, v)); |
| 106 | |
Andrea Campanella | 45f4d3d | 2017-02-23 14:45:21 +0100 | [diff] [blame] | 107 | //recursion call to print each parent |
| 108 | parents.stream().forEach(parent -> printDriver(parent, false)); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 109 | } |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Andrea Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 112 | private void printBehaviour(Class<? extends Behaviour> behaviour, Driver driver) { |
| 113 | print(FMT_B, behaviour.getCanonicalName(), |
| 114 | driver.implementation(behaviour).getCanonicalName()); |
| 115 | } |
| 116 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 117 | } |