Ayaka Koshibe
Committed by Gerrit Code Review

Command for basic testing of LinkResourceService

Change-Id: I65b3a3050255b2134cd4ffc3cad6e0c7d7df5515
1 +package org.onosproject.cli.net;
2 +
3 +import java.util.Set;
4 +import java.util.List;
5 +
6 +import org.apache.karaf.shell.commands.Argument;
7 +import org.apache.karaf.shell.commands.Command;
8 +import org.apache.karaf.shell.commands.Option;
9 +import org.onosproject.cli.AbstractShellCommand;
10 +import org.onosproject.net.intent.IntentId;
11 +import org.onosproject.net.resource.DefaultLinkResourceRequest;
12 +import org.onosproject.net.resource.LinkResourceAllocations;
13 +import org.onosproject.net.resource.LinkResourceRequest;
14 +import org.onosproject.net.resource.LinkResourceService;
15 +import org.onosproject.net.topology.PathService;
16 +import org.onosproject.net.DeviceId;
17 +import org.onosproject.net.Link;
18 +import org.onosproject.net.Path;
19 +
20 +import com.google.common.collect.Lists;
21 +
22 +/**
23 + * Commands to test out LinkResourceManager directly.
24 + */
25 +@Command(scope = "onos", name = "resource-request",
26 + description = "request or remove resources")
27 +public class LinkResourceTestCommand extends AbstractShellCommand {
28 +
29 + // default is bandwidth.
30 + @Option(name = "-m", aliases = "--mpls", description = "MPLS resource",
31 + required = false, multiValued = false)
32 + private boolean isMPLS = false;
33 +
34 + @Option(name = "-o", aliases = "--optical", description = "Optical resource",
35 + required = false, multiValued = false)
36 + private boolean isOptical = false;
37 +
38 + @Option(name = "-d", aliases = "--delete", description = "Delete resource by intent ID",
39 + required = false, multiValued = false)
40 + private boolean remove = false;
41 +
42 + @Argument(index = 0, name = "srcString", description = "Link source",
43 + required = true, multiValued = false)
44 + String srcString = null;
45 +
46 + @Argument(index = 1, name = "dstString", description = "Link destination",
47 + required = true, multiValued = false)
48 + String dstString = null;
49 +
50 + @Argument(index = 2, name = "id", description = "Identifier",
51 + required = true, multiValued = false)
52 + int id;
53 +
54 + private LinkResourceService resService;
55 + private PathService pathService;
56 +
57 + private static final int BANDWIDTH = 1_000_000;
58 +
59 + @Override
60 + protected void execute() {
61 + resService = get(LinkResourceService.class);
62 + pathService = get(PathService.class);
63 +
64 + DeviceId src = DeviceId.deviceId(getDeviceId(srcString));
65 + DeviceId dst = DeviceId.deviceId(getDeviceId(dstString));
66 + IntentId intId = IntentId.valueOf(id);
67 +
68 + Set<Path> paths = pathService.getPaths(src, dst);
69 +
70 + if (paths == null || paths.isEmpty()) {
71 + print("No path between %s and %s", srcString, dstString);
72 + return;
73 + }
74 +
75 + if (remove) {
76 + LinkResourceAllocations lra = resService.getAllocations(intId);
77 + resService.releaseResources(lra);
78 + return;
79 + }
80 +
81 + for (Path p : paths) {
82 + List<Link> links = p.links();
83 + LinkResourceRequest.Builder request = null;
84 + if (isMPLS) {
85 + List<Link> nlinks = Lists.newArrayList();
86 + try {
87 + nlinks.addAll(links.subList(1, links.size() - 2));
88 + request = DefaultLinkResourceRequest.builder(intId, nlinks)
89 + .addMplsRequest();
90 + } catch (IndexOutOfBoundsException e) {
91 + log.warn("could not allocate MPLS path", e);
92 + continue;
93 + }
94 + } else if (isOptical) {
95 + request = DefaultLinkResourceRequest.builder(intId, links)
96 + .addLambdaRequest();
97 + } else {
98 + request = DefaultLinkResourceRequest.builder(intId, links)
99 + .addBandwidthRequest(BANDWIDTH);
100 + }
101 +
102 + if (request != null) {
103 + LinkResourceRequest lrr = request.build();
104 + LinkResourceAllocations lra = resService.requestResources(lrr);
105 + if (lra != null) {
106 + break;
107 + }
108 + print("Allocated:\n%s", lra);
109 + } else {
110 + log.info("nothing to request");
111 + }
112 + }
113 + }
114 +
115 + public String getDeviceId(String deviceString) {
116 + int slash = deviceString.indexOf('/');
117 + if (slash <= 0) {
118 + return "";
119 + }
120 + return deviceString.substring(0, slash);
121 + }
122 +
123 +}
...@@ -255,6 +255,14 @@ ...@@ -255,6 +255,14 @@
255 </completers> 255 </completers>
256 </command> 256 </command>
257 <command> 257 <command>
258 + <action class="org.onosproject.cli.net.LinkResourceTestCommand"/>
259 + <completers>
260 + <ref component-id="connectPointCompleter"/>
261 + <ref component-id="connectPointCompleter"/>
262 + <null/>
263 + </completers>
264 + </command>
265 + <command>
258 <action class="org.onosproject.cli.net.ClustersListCommand"/> 266 <action class="org.onosproject.cli.net.ClustersListCommand"/>
259 </command> 267 </command>
260 <command> 268 <command>
......