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