Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.driver.Behaviour; |
| 24 | import org.onosproject.net.driver.Driver; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 25 | import org.onosproject.net.driver.DriverService; |
Andrea Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 26 | |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 27 | import java.util.List; |
| 28 | import java.util.Optional; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 29 | import java.util.Set; |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 30 | import java.util.stream.Collectors; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 31 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Lists device drivers. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "drivers", |
| 36 | description = "Lists device drivers") |
| 37 | public class DriversListCommand extends AbstractShellCommand { |
| 38 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 39 | 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] | 40 | 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() { |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 49 | DriverService service = get(DriverService.class); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 50 | |
| 51 | if (driverName != null) { |
Andrea Campanella | 45f4d3d | 2017-02-23 14:45:21 +0100 | [diff] [blame] | 52 | printDriver(service.getDriver(driverName), true); |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 53 | } else { |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 54 | if (outputJson()) { |
| 55 | json(service.getDrivers()); |
| 56 | } else { |
Andrea Campanella | 45f4d3d | 2017-02-23 14:45:21 +0100 | [diff] [blame] | 57 | service.getDrivers().forEach(d -> printDriver(d, true)); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 58 | } |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 62 | 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 Campanella | 45f4d3d | 2017-02-23 14:45:21 +0100 | [diff] [blame] | 72 | private void printDriver(Driver driver, boolean first) { |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 73 | if (outputJson()) { |
| 74 | json(driver); |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 75 | } else { |
Andrea Campanella | 45f4d3d | 2017-02-23 14:45:21 +0100 | [diff] [blame] | 76 | |
Andrea Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 77 | List<Driver> parents = Optional.ofNullable(driver.parents()) |
| 78 | .orElse(ImmutableList.of()); |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 79 | |
Andrea Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 80 | List<String> parentsNames = parents.stream() |
| 81 | .map(Driver::name).collect(Collectors.toList()); |
| 82 | |
Andrea Campanella | 45f4d3d | 2017-02-23 14:45:21 +0100 | [diff] [blame] | 83 | 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 Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 90 | driver.behaviours().forEach(b -> printBehaviour(b, driver)); |
Andrea Campanella | 45f4d3d | 2017-02-23 14:45:21 +0100 | [diff] [blame] | 91 | //recursion call to print each parent |
| 92 | parents.stream().forEach(parent -> printDriver(parent, false)); |
Frank Wang | 2cdf4e3 | 2017-02-06 18:03:16 +0800 | [diff] [blame] | 93 | driver.properties().forEach((k, v) -> print(FMT_P, k, v)); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 94 | } |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Andrea Campanella | e6e2b76 | 2017-02-22 21:55:53 +0100 | [diff] [blame] | 97 | private void printBehaviour(Class<? extends Behaviour> behaviour, Driver driver) { |
| 98 | print(FMT_B, behaviour.getCanonicalName(), |
| 99 | driver.implementation(behaviour).getCanonicalName()); |
| 100 | } |
| 101 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 102 | } |