Naoki Shiota

Intent ID filter and resource type filter for CLI "allocation" command.

Change-Id: Ifa232e3dd783c415a487df81c57ef196a207e8fc
...@@ -17,12 +17,18 @@ package org.onosproject.cli.net; ...@@ -17,12 +17,18 @@ package org.onosproject.cli.net;
17 17
18 import static org.onosproject.net.DeviceId.deviceId; 18 import static org.onosproject.net.DeviceId.deviceId;
19 19
20 -import java.util.Collection; 20 +import java.util.Arrays;
21 +import java.util.Collections;
22 +import java.util.HashSet;
23 +import java.util.Set;
21 import java.util.stream.StreamSupport; 24 import java.util.stream.StreamSupport;
22 25
26 +import com.google.common.collect.ImmutableSet;
23 import org.apache.karaf.shell.commands.Argument; 27 import org.apache.karaf.shell.commands.Argument;
24 import org.apache.karaf.shell.commands.Command; 28 import org.apache.karaf.shell.commands.Command;
25 import org.apache.karaf.shell.commands.Option; 29 import org.apache.karaf.shell.commands.Option;
30 +import org.onlab.packet.MplsLabel;
31 +import org.onlab.packet.VlanId;
26 import org.onosproject.cli.AbstractShellCommand; 32 import org.onosproject.cli.AbstractShellCommand;
27 import org.onosproject.net.Device; 33 import org.onosproject.net.Device;
28 import org.onosproject.net.DeviceId; 34 import org.onosproject.net.DeviceId;
...@@ -31,6 +37,7 @@ import org.onosproject.net.Port; ...@@ -31,6 +37,7 @@ import org.onosproject.net.Port;
31 import org.onosproject.net.PortNumber; 37 import org.onosproject.net.PortNumber;
32 import org.onosproject.net.device.DeviceService; 38 import org.onosproject.net.device.DeviceService;
33 import org.onosproject.net.newresource.DiscreteResourceId; 39 import org.onosproject.net.newresource.DiscreteResourceId;
40 +import org.onosproject.net.intent.IntentId;
34 import org.onosproject.net.newresource.ResourceAllocation; 41 import org.onosproject.net.newresource.ResourceAllocation;
35 import org.onosproject.net.newresource.ResourceService; 42 import org.onosproject.net.newresource.ResourceService;
36 43
...@@ -44,10 +51,17 @@ import org.onosproject.net.newresource.Resources; ...@@ -44,10 +51,17 @@ import org.onosproject.net.newresource.Resources;
44 description = "Lists allocated resources") 51 description = "Lists allocated resources")
45 public class AllocationsCommand extends AbstractShellCommand { 52 public class AllocationsCommand extends AbstractShellCommand {
46 53
47 - // TODO add other resource types 54 + @Option(name = "-t", aliases = "--type", description = "List of resource types",
48 - @Option(name = "-l", aliases = "--lambda", description = "Lambda Resource", 55 + required = false, multiValued = true)
49 - required = false, multiValued = false) 56 + String[] typeStrings = null;
50 - private boolean lambda = true; 57 +
58 + Set<String> typesToPrint;
59 +
60 + @Option(name = "-i", aliases = "--intentId", description = "Intent ID",
61 + required = false, multiValued = true)
62 + String[] intentStrings;
63 +
64 + Set<String> intentsToPrint;
51 65
52 @Argument(index = 0, name = "deviceIdString", description = "Device ID", 66 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
53 required = false, multiValued = false) 67 required = false, multiValued = false)
...@@ -67,6 +81,17 @@ public class AllocationsCommand extends AbstractShellCommand { ...@@ -67,6 +81,17 @@ public class AllocationsCommand extends AbstractShellCommand {
67 deviceService = get(DeviceService.class); 81 deviceService = get(DeviceService.class);
68 resourceService = get(ResourceService.class); 82 resourceService = get(ResourceService.class);
69 83
84 + if (typeStrings != null) {
85 + typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
86 + } else {
87 + typesToPrint = Collections.emptySet();
88 + }
89 +
90 + if (intentStrings != null) {
91 + intentsToPrint = new HashSet<>(Arrays.asList(intentStrings));
92 + } else {
93 + intentsToPrint = Collections.emptySet();
94 + }
70 95
71 if (deviceIdStr != null && portNumberStr != null) { 96 if (deviceIdStr != null && portNumberStr != null) {
72 DeviceId deviceId = deviceId(deviceIdStr); 97 DeviceId deviceId = deviceId(deviceIdStr);
...@@ -104,20 +129,37 @@ public class AllocationsCommand extends AbstractShellCommand { ...@@ -104,20 +129,37 @@ public class AllocationsCommand extends AbstractShellCommand {
104 } 129 }
105 print("%s%s", Strings.repeat(" ", level), asVerboseString(num)); 130 print("%s%s", Strings.repeat(" ", level), asVerboseString(num));
106 131
107 - // TODO: Current design cannot deal with sub-resources 132 + // FIXME: This workaround induces a lot of distributed store access.
108 - // (e.g., TX/RX under Port) 133 + // ResourceService should have an API to get all allocations under a parent resource.
134 + Set<Class<?>> subResourceTypes = ImmutableSet.<Class<?>>builder()
135 + .add(OchSignal.class)
136 + .add(VlanId.class)
137 + .add(MplsLabel.class)
138 + .build();
139 +
140 + DiscreteResourceId resourceId = Resources.discrete(did, num).id();
141 + for (Class<?> t : subResourceTypes) {
142 + resourceService.getResourceAllocations(resourceId, t).stream()
143 + .filter(a -> isSubjectToPrint(a))
144 + .forEach(a -> print("%s%s allocated by %s", Strings.repeat(" ", level + 1),
145 + a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumer())));
146 +
147 + }
148 + }
109 149
110 - DiscreteResourceId resource = Resources.discrete(did, num).id(); 150 + private boolean isSubjectToPrint(ResourceAllocation allocation) {
111 - if (lambda) { 151 + if (!intentsToPrint.isEmpty()
112 - //print("Lambda resources:"); 152 + && allocation.consumer() instanceof IntentId
113 - Collection<ResourceAllocation> allocations 153 + && !intentsToPrint.contains(allocation.consumer().toString())) {
114 - = resourceService.getResourceAllocations(resource, OchSignal.class); 154 + return false;
155 + }
115 156
116 - for (ResourceAllocation a : allocations) { 157 + if (!typesToPrint.isEmpty()
117 - print("%s%s allocated by %s", Strings.repeat(" ", level + 1), 158 + && !typesToPrint.contains(allocation.resource().simpleTypeName())) {
118 - a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumer())); 159 + return false;
119 - }
120 } 160 }
161 +
162 + return true;
121 } 163 }
122 164
123 /** 165 /**
......