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 | |
| 18 | import java.util.Set; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.apache.karaf.shell.commands.Argument; |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.apache.karaf.shell.commands.Option; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.net.intent.IntentId; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 26 | import org.onosproject.net.resource.link.DefaultLinkResourceRequest; |
| 27 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 28 | import org.onosproject.net.resource.link.LinkResourceRequest; |
| 29 | import org.onosproject.net.resource.link.LinkResourceService; |
Ayaka Koshibe | 4699b29 | 2015-04-28 14:33:10 -0700 | [diff] [blame] | 30 | import org.onosproject.net.topology.PathService; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.Link; |
| 33 | import org.onosproject.net.Path; |
| 34 | |
| 35 | import 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") |
| 42 | public 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 | } |