blob: a1a76925ab681eba2035da91d4218d9c06161cc2 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
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 */
Ayaka Koshibe4699b292015-04-28 14:33:10 -070016package org.onosproject.cli.net;
17
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080018import com.google.common.collect.Lists;
Ayaka Koshibe4699b292015-04-28 14:33:10 -070019import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.apache.karaf.shell.commands.Option;
22import org.onosproject.cli.AbstractShellCommand;
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080023import org.onosproject.net.DeviceId;
24import org.onosproject.net.Link;
25import org.onosproject.net.Path;
Ayaka Koshibe4699b292015-04-28 14:33:10 -070026import org.onosproject.net.intent.IntentId;
Brian O'Connor6de2e202015-05-21 14:30:41 -070027import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
28import org.onosproject.net.resource.link.LinkResourceAllocations;
29import org.onosproject.net.resource.link.LinkResourceRequest;
30import org.onosproject.net.resource.link.LinkResourceService;
Ayaka Koshibe4699b292015-04-28 14:33:10 -070031import org.onosproject.net.topology.PathService;
Ayaka Koshibe4699b292015-04-28 14:33:10 -070032
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080033import java.util.List;
34import java.util.Set;
Ayaka Koshibe4699b292015-04-28 14:33:10 -070035
36/**
37 * Commands to test out LinkResourceManager directly.
38 */
39@Command(scope = "onos", name = "resource-request",
40 description = "request or remove resources")
41public class LinkResourceTestCommand extends AbstractShellCommand {
42
43 // default is bandwidth.
44 @Option(name = "-m", aliases = "--mpls", description = "MPLS resource",
45 required = false, multiValued = false)
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080046 private boolean isMpls = false;
Ayaka Koshibe4699b292015-04-28 14:33:10 -070047
48 @Option(name = "-o", aliases = "--optical", description = "Optical resource",
49 required = false, multiValued = false)
50 private boolean isOptical = false;
51
52 @Option(name = "-d", aliases = "--delete", description = "Delete resource by intent ID",
53 required = false, multiValued = false)
54 private boolean remove = false;
55
56 @Argument(index = 0, name = "srcString", description = "Link source",
57 required = true, multiValued = false)
58 String srcString = null;
59
60 @Argument(index = 1, name = "dstString", description = "Link destination",
61 required = true, multiValued = false)
62 String dstString = null;
63
64 @Argument(index = 2, name = "id", description = "Identifier",
65 required = true, multiValued = false)
66 int id;
67
68 private LinkResourceService resService;
69 private PathService pathService;
70
71 private static final int BANDWIDTH = 1_000_000;
72
73 @Override
74 protected void execute() {
75 resService = get(LinkResourceService.class);
76 pathService = get(PathService.class);
77
78 DeviceId src = DeviceId.deviceId(getDeviceId(srcString));
79 DeviceId dst = DeviceId.deviceId(getDeviceId(dstString));
80 IntentId intId = IntentId.valueOf(id);
81
82 Set<Path> paths = pathService.getPaths(src, dst);
83
84 if (paths == null || paths.isEmpty()) {
85 print("No path between %s and %s", srcString, dstString);
86 return;
87 }
88
89 if (remove) {
90 LinkResourceAllocations lra = resService.getAllocations(intId);
91 resService.releaseResources(lra);
92 return;
93 }
94
95 for (Path p : paths) {
96 List<Link> links = p.links();
97 LinkResourceRequest.Builder request = null;
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080098 if (isMpls) {
Ayaka Koshibe4699b292015-04-28 14:33:10 -070099 List<Link> nlinks = Lists.newArrayList();
100 try {
101 nlinks.addAll(links.subList(1, links.size() - 2));
102 request = DefaultLinkResourceRequest.builder(intId, nlinks)
103 .addMplsRequest();
104 } catch (IndexOutOfBoundsException e) {
105 log.warn("could not allocate MPLS path", e);
106 continue;
107 }
108 } else if (isOptical) {
109 request = DefaultLinkResourceRequest.builder(intId, links)
110 .addLambdaRequest();
111 } else {
112 request = DefaultLinkResourceRequest.builder(intId, links)
113 .addBandwidthRequest(BANDWIDTH);
114 }
115
116 if (request != null) {
117 LinkResourceRequest lrr = request.build();
118 LinkResourceAllocations lra = resService.requestResources(lrr);
119 if (lra != null) {
120 break;
121 }
122 print("Allocated:\n%s", lra);
123 } else {
124 log.info("nothing to request");
125 }
126 }
127 }
128
129 public String getDeviceId(String deviceString) {
130 int slash = deviceString.indexOf('/');
131 if (slash <= 0) {
132 return "";
133 }
134 return deviceString.substring(0, slash);
135 }
136
137}