blob: d68084c7040d0752c7d78d9c79f58fdf3817ecd0 [file] [log] [blame]
Ayaka Koshibe4699b292015-04-28 14:33:10 -07001package org.onosproject.cli.net;
2
3import java.util.Set;
4import java.util.List;
5
6import org.apache.karaf.shell.commands.Argument;
7import org.apache.karaf.shell.commands.Command;
8import org.apache.karaf.shell.commands.Option;
9import org.onosproject.cli.AbstractShellCommand;
10import org.onosproject.net.intent.IntentId;
Brian O'Connor6de2e202015-05-21 14:30:41 -070011import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
12import org.onosproject.net.resource.link.LinkResourceAllocations;
13import org.onosproject.net.resource.link.LinkResourceRequest;
14import org.onosproject.net.resource.link.LinkResourceService;
Ayaka Koshibe4699b292015-04-28 14:33:10 -070015import org.onosproject.net.topology.PathService;
16import org.onosproject.net.DeviceId;
17import org.onosproject.net.Link;
18import org.onosproject.net.Path;
19
20import 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")
27public 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}