Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 18 | import java.util.Set; |
| 19 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 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.Driver; |
| 24 | import org.onosproject.net.driver.DriverAdminService; |
| 25 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 26 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 27 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Lists device drivers. |
| 30 | */ |
| 31 | @Command(scope = "onos", name = "drivers", |
| 32 | description = "Lists device drivers") |
| 33 | public class DriversListCommand extends AbstractShellCommand { |
| 34 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 35 | 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] | 36 | private static final String FMT_B = " %s via %s"; |
| 37 | private static final String FMT_P = " %s=%s"; |
| 38 | |
| 39 | @Argument(index = 0, name = "driverName", description = "Driver name", |
| 40 | required = false, multiValued = false) |
| 41 | String driverName = null; |
| 42 | |
| 43 | @Override |
| 44 | protected void execute() { |
| 45 | DriverAdminService service = get(DriverAdminService.class); |
| 46 | |
| 47 | if (driverName != null) { |
| 48 | printDriver(service.getDriver(driverName)); |
| 49 | } else { |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 50 | if (outputJson()) { |
| 51 | json(service.getDrivers()); |
| 52 | } else { |
| 53 | service.getDrivers().forEach(this::printDriver); |
| 54 | } |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 58 | private void json(Driver driver) { |
| 59 | print("%s", jsonForEntity(driver, Driver.class)); |
| 60 | } |
| 61 | |
| 62 | private void json(Set<Driver> drivers) { |
| 63 | ArrayNode result = mapper().createArrayNode(); |
| 64 | drivers.forEach(driver -> result.add(jsonForEntity(driver, Driver.class))); |
| 65 | print("%s", result.toString()); |
| 66 | } |
| 67 | |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 68 | private void printDriver(Driver driver) { |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 69 | if (outputJson()) { |
| 70 | json(driver); |
| 71 | } else { |
| 72 | Driver parent = driver.parent(); |
| 73 | print(FMT, driver.name(), parent != null ? parent.name() : "none", |
| 74 | driver.manufacturer(), driver.hwVersion(), driver.swVersion()); |
| 75 | driver.behaviours().forEach(b -> print(FMT_B, b.getCanonicalName(), |
| 76 | driver.implementation(b).getCanonicalName())); |
| 77 | driver.properties().forEach((k, v) -> print(FMT_P, k, v)); |
| 78 | } |
Thomas Vachuska | 5c2f813 | 2015-04-08 23:09:08 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } |