Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
| 16 | package org.onlab.onos.cli.net; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onlab.onos.cli.AbstractShellCommand; |
| 21 | import org.onlab.onos.net.ConnectPoint; |
| 22 | import org.onlab.onos.net.DeviceId; |
| 23 | import org.onlab.onos.net.Link; |
| 24 | import org.onlab.onos.net.PortNumber; |
| 25 | import org.onlab.onos.net.link.LinkService; |
| 26 | import org.onlab.onos.net.resource.LinkResourceAllocations; |
| 27 | import org.onlab.onos.net.resource.LinkResourceService; |
| 28 | |
| 29 | import static org.onlab.onos.cli.net.AddPointToPointIntentCommand.getDeviceId; |
| 30 | import static org.onlab.onos.cli.net.AddPointToPointIntentCommand.getPortNumber; |
| 31 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 32 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 33 | |
| 34 | /** |
| 35 | * Lists allocations by link. |
| 36 | */ |
| 37 | @Command(scope = "onos", name = "resource-allocations", |
| 38 | description = "Lists allocations by link") |
| 39 | public class ResourceAllocationsCommand extends AbstractShellCommand { |
| 40 | |
| 41 | private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s%s"; |
| 42 | private static final String COMPACT = "%s/%s-%s/%s"; |
| 43 | |
| 44 | @Argument(index = 0, name = "srcString", description = "Link source", |
| 45 | required = false, multiValued = false) |
| 46 | String srcString = null; |
| 47 | @Argument(index = 1, name = "dstString", description = "Link destination", |
| 48 | required = false, multiValued = false) |
| 49 | String dstString = null; |
| 50 | |
| 51 | @Override |
| 52 | protected void execute() { |
| 53 | LinkResourceService resourceService = get(LinkResourceService.class); |
| 54 | LinkService linkService = get(LinkService.class); |
| 55 | |
| 56 | Iterable<LinkResourceAllocations> itr = null; |
| 57 | try { |
| 58 | DeviceId ingressDeviceId = deviceId(getDeviceId(srcString)); |
| 59 | PortNumber ingressPortNumber = portNumber(getPortNumber(srcString)); |
| 60 | ConnectPoint src = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 61 | |
| 62 | DeviceId egressDeviceId = deviceId(getDeviceId(dstString)); |
| 63 | PortNumber egressPortNumber = portNumber(getPortNumber(dstString)); |
| 64 | ConnectPoint dst = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 65 | |
| 66 | Link link = linkService.getLink(src, dst); |
| 67 | |
| 68 | itr = resourceService.getAllocations(link); |
| 69 | |
| 70 | for (LinkResourceAllocations allocation : itr) { |
| 71 | print("%s", allocation.getResourceAllocation(link)); |
| 72 | } |
| 73 | |
| 74 | } catch (Exception e) { |
| 75 | print("----- Displaying all resource allocations -----", e.getMessage()); |
| 76 | itr = resourceService.getAllocations(); |
| 77 | for (LinkResourceAllocations allocation : itr) { |
| 78 | print("%s", allocation); |
| 79 | } |
| 80 | |
| 81 | } |
| 82 | } |
| 83 | } |