Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
| 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 Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
| 17 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.Path; |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 26 | import org.onosproject.net.intent.IntentId; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 27 | import org.onosproject.net.resource.link.DefaultLinkResourceRequest; |
| 28 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 29 | import org.onosproject.net.resource.link.LinkResourceRequest; |
| 30 | import org.onosproject.net.resource.link.LinkResourceService; |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 31 | import org.onosproject.net.topology.PathService; |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 32 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 33 | import java.util.List; |
| 34 | import java.util.Set; |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Commands to test out LinkResourceManager directly. |
HIGUCHI Yuta | 14869be | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 38 | * |
| 39 | * @deprecated in Emu release |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 40 | */ |
HIGUCHI Yuta | 14869be | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 41 | @Deprecated |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 42 | @Command(scope = "onos", name = "resource-request", |
HIGUCHI Yuta | 14869be | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 43 | description = "request or remove resources" |
| 44 | + "[Using deprecated LinkResourceService]") |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 45 | public class LinkResourceTestCommand extends AbstractShellCommand { |
| 46 | |
| 47 | // default is bandwidth. |
| 48 | @Option(name = "-m", aliases = "--mpls", description = "MPLS resource", |
| 49 | required = false, multiValued = false) |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 50 | private boolean isMpls = false; |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 51 | |
| 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 Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 102 | if (isMpls) { |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 103 | 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 | } |