blob: f372f9c87ae1c90608007a863d25aa6c6bf73d25 [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.
HIGUCHI Yuta14869be2015-12-08 13:22:33 -080038 *
39 * @deprecated in Emu release
Ayaka Koshibe4699b292015-04-28 14:33:10 -070040 */
HIGUCHI Yuta14869be2015-12-08 13:22:33 -080041@Deprecated
Ayaka Koshibe4699b292015-04-28 14:33:10 -070042@Command(scope = "onos", name = "resource-request",
HIGUCHI Yuta14869be2015-12-08 13:22:33 -080043 description = "request or remove resources"
44 + "[Using deprecated LinkResourceService]")
Ayaka Koshibe4699b292015-04-28 14:33:10 -070045public class LinkResourceTestCommand extends AbstractShellCommand {
46
47 // default is bandwidth.
48 @Option(name = "-m", aliases = "--mpls", description = "MPLS resource",
49 required = false, multiValued = false)
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080050 private boolean isMpls = false;
Ayaka Koshibe4699b292015-04-28 14:33:10 -070051
52 @Option(name = "-o", aliases = "--optical", description = "Optical resource",
53 required = false, multiValued = false)
54 private boolean isOptical = false;
55
56 @Option(name = "-d", aliases = "--delete", description = "Delete resource by intent ID",
57 required = false, multiValued = false)
58 private boolean remove = false;
59
60 @Argument(index = 0, name = "srcString", description = "Link source",
61 required = true, multiValued = false)
62 String srcString = null;
63
64 @Argument(index = 1, name = "dstString", description = "Link destination",
65 required = true, multiValued = false)
66 String dstString = null;
67
68 @Argument(index = 2, name = "id", description = "Identifier",
69 required = true, multiValued = false)
70 int id;
71
72 private LinkResourceService resService;
73 private PathService pathService;
74
75 private static final int BANDWIDTH = 1_000_000;
76
77 @Override
78 protected void execute() {
79 resService = get(LinkResourceService.class);
80 pathService = get(PathService.class);
81
82 DeviceId src = DeviceId.deviceId(getDeviceId(srcString));
83 DeviceId dst = DeviceId.deviceId(getDeviceId(dstString));
84 IntentId intId = IntentId.valueOf(id);
85
86 Set<Path> paths = pathService.getPaths(src, dst);
87
88 if (paths == null || paths.isEmpty()) {
89 print("No path between %s and %s", srcString, dstString);
90 return;
91 }
92
93 if (remove) {
94 LinkResourceAllocations lra = resService.getAllocations(intId);
95 resService.releaseResources(lra);
96 return;
97 }
98
99 for (Path p : paths) {
100 List<Link> links = p.links();
101 LinkResourceRequest.Builder request = null;
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800102 if (isMpls) {
Ayaka Koshibe4699b292015-04-28 14:33:10 -0700103 List<Link> nlinks = Lists.newArrayList();
104 try {
105 nlinks.addAll(links.subList(1, links.size() - 2));
106 request = DefaultLinkResourceRequest.builder(intId, nlinks)
107 .addMplsRequest();
108 } catch (IndexOutOfBoundsException e) {
109 log.warn("could not allocate MPLS path", e);
110 continue;
111 }
112 } else if (isOptical) {
113 request = DefaultLinkResourceRequest.builder(intId, links)
114 .addLambdaRequest();
115 } else {
116 request = DefaultLinkResourceRequest.builder(intId, links)
117 .addBandwidthRequest(BANDWIDTH);
118 }
119
120 if (request != null) {
121 LinkResourceRequest lrr = request.build();
122 LinkResourceAllocations lra = resService.requestResources(lrr);
123 if (lra != null) {
124 break;
125 }
126 print("Allocated:\n%s", lra);
127 } else {
128 log.info("nothing to request");
129 }
130 }
131 }
132
133 public String getDeviceId(String deviceString) {
134 int slash = deviceString.indexOf('/');
135 if (slash <= 0) {
136 return "";
137 }
138 return deviceString.substring(0, slash);
139 }
140
141}