blob: 62103f9c49ac73f78dff0c4b177bc7369056b2e9 [file] [log] [blame]
Seyeon Jeong1e249102020-03-10 17:41:14 -07001/*
2 * Copyright 2015-present Open Networking Foundation
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 */
16package org.onosproject.cli.net;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Completion;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.driver.DriverService;
26
27import java.util.Map;
28
29@Service
30@Command(scope = "onos", name = "device-drivers",
31 description = "list all devices and their driver names or a driver name of a device")
32public class DeviceDriversCommand extends AbstractShellCommand {
33
34 @Argument(index = 0, name = "uri", description = "Device ID",
35 required = false, multiValued = false)
36 @Completion(DeviceIdCompleter.class)
37 String uri = null;
38
39 @Override
40 protected void doExecute() {
41 DriverService service = get(DriverService.class);
42
43 if (uri == null) {
44 Map<DeviceId, String> deviceDriverNameMap = service.getDeviceDrivers();
45 if (outputJson()) {
46 json(deviceDriverNameMap);
47 } else {
48 deviceDriverNameMap.forEach((k, v) -> print("%s : %s", k.toString(), v));
49 }
50 } else {
51 DeviceId deviceId = DeviceId.deviceId(uri);
52 String driverName = service.getDriver(deviceId).name();
53 if (outputJson()) {
54 json(deviceId, driverName);
55 } else {
56 print("%s : %s", deviceId.toString(), driverName);
57 }
58 }
59 }
60
61 private void json(Map<DeviceId, String> map) {
62 ObjectNode result = mapper().createObjectNode();
63 map.forEach((k, v) -> {
64 result.put(k.toString(), v);
65 });
66 print("%s", result.toString());
67 }
68
69 private void json(DeviceId deviceId, String driverName) {
70 ObjectNode result = mapper().createObjectNode();
71 result.put(deviceId.toString(), driverName);
72 print("%s", result.toString());
73 }
74
75}