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 | |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 18 | import java.util.List; |
| 19 | import java.util.Optional; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 20 | import java.util.Set; |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 21 | import java.util.stream.Collectors; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 22 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Argument; |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.net.driver.Driver; |
| 27 | import org.onosproject.net.driver.DriverAdminService; |
| 28 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 29 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableList; |
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() { |
| 49 | DriverAdminService service = get(DriverAdminService.class); |
| 50 | |
| 51 | if (driverName != null) { |
| 52 | printDriver(service.getDriver(driverName)); |
| 53 | } else { |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 54 | if (outputJson()) { |
| 55 | json(service.getDrivers()); |
| 56 | } else { |
| 57 | service.getDrivers().forEach(this::printDriver); |
| 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 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 72 | private void printDriver(Driver driver) { |
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 { |
| 76 | List<String> parents = Optional.ofNullable(driver.parents()) |
| 77 | .orElse(ImmutableList.of()) |
| 78 | .stream() |
| 79 | .map(Driver::name) |
| 80 | .collect(Collectors.toList()); |
| 81 | |
| 82 | print(FMT, driver.name(), parents, |
| 83 | driver.manufacturer(), driver.hwVersion(), driver.swVersion()); |
Frank Wang | 2cdf4e3 | 2017-02-06 18:03:16 +0800 | [diff] [blame] | 84 | driver.behaviours().forEach(b -> print(FMT_B, b.getCanonicalName(), |
Yuta HIGUCHI | 0083a51 | 2017-02-21 12:14:38 -0800 | [diff] [blame] | 85 | driver.implementation(b).getCanonicalName())); |
Frank Wang | 2cdf4e3 | 2017-02-06 18:03:16 +0800 | [diff] [blame] | 86 | driver.properties().forEach((k, v) -> print(FMT_P, k, v)); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 87 | } |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | } |