blob: b70723e9df3b047b3914b196df147da415097a5d [file] [log] [blame]
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08001/*
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 */
16package org.onosproject.cli.net;
17
18import static org.onosproject.net.DeviceId.deviceId;
19
Naoki Shiota3f342182016-01-13 11:15:10 -080020import java.util.Set;
21import java.util.HashSet;
22import java.util.Arrays;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080023import java.util.Collection;
Naoki Shiota3f342182016-01-13 11:15:10 -080024import java.util.Collections;
25
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080026import org.apache.karaf.shell.commands.Argument;
27import org.apache.karaf.shell.commands.Command;
Naoki Shiota3f342182016-01-13 11:15:10 -080028import org.apache.karaf.shell.commands.Option;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080029import org.onosproject.cli.AbstractShellCommand;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.newresource.ResourcePath;
33import org.onosproject.net.newresource.ResourceService;
34
35import com.google.common.base.Strings;
36
37/**
38 * Lists available resources.
39 */
40@Command(scope = "onos", name = "resources",
41 description = "Lists available resources")
42public class ResourcesCommand extends AbstractShellCommand {
43
Naoki Shiota3f342182016-01-13 11:15:10 -080044 @Option(name = "-s", aliases = "--sort", description = "Sort output",
45 required = false, multiValued = false)
46 boolean sort = false;
47
48 @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed",
49 required = false, multiValued = true)
50 String[] typeStrings = null;
51
52 Set<String> typesToPrint;
53
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080054 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
55 required = false, multiValued = false)
56 String deviceIdStr = null;
57
58 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
59 required = false, multiValued = false)
60 String portNumberStr = null;
61
62
63 private ResourceService resourceService;
64
65 @Override
66 protected void execute() {
67 resourceService = get(ResourceService.class);
68
Naoki Shiota3f342182016-01-13 11:15:10 -080069 if (typeStrings != null) {
70 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
71 } else {
72 typesToPrint = Collections.emptySet();
73 }
74
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080075 if (deviceIdStr != null && portNumberStr != null) {
76 DeviceId deviceId = deviceId(deviceIdStr);
77 PortNumber portNumber = PortNumber.fromString(portNumberStr);
78
79 printResource(ResourcePath.discrete(deviceId, portNumber), 0);
80 } else if (deviceIdStr != null) {
81 DeviceId deviceId = deviceId(deviceIdStr);
82
83 printResource(ResourcePath.discrete(deviceId), 0);
84 } else {
85 printResource(ResourcePath.ROOT, 0);
86 }
87 }
88
89 private void printResource(ResourcePath resource, int level) {
Naoki Shiota3f342182016-01-13 11:15:10 -080090 Collection<ResourcePath> children = resourceService.getAvailableResources(resource);
91
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080092 if (resource.equals(ResourcePath.ROOT)) {
93 print("ROOT");
94 } else {
Naoki Shiota3f342182016-01-13 11:15:10 -080095 String resourceName = resource.last().getClass().getSimpleName();
96
97 if (children.isEmpty() && !typesToPrint.isEmpty() && !typesToPrint.contains(resourceName)) {
98 // This resource is target of filtering
99 return;
100 }
101
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800102 String toString = String.valueOf(resource.last());
Naoki Shiota3f342182016-01-13 11:15:10 -0800103 if (toString.startsWith(resourceName)) {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800104 print("%s%s", Strings.repeat(" ", level),
105 toString);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800106 } else {
Naoki Shiota3f342182016-01-13 11:15:10 -0800107 print("%s%s: %s", Strings.repeat(" ", level),
108 resourceName,
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800109 toString);
110 }
111 }
112
Naoki Shiota3f342182016-01-13 11:15:10 -0800113 if (sort) {
114 children.stream()
115 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
116 .forEach(r -> printResource(r, level + 1));
117 } else {
118 // TODO: Should consider better output for leaf nodes
119 children.forEach(r -> printResource(r, level + 1));
120 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800121 }
122}