HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import static org.onosproject.net.newresource.ResourcePath.discrete; |
| 19 | |
| 20 | import java.util.Optional; |
| 21 | |
| 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
| 24 | import org.apache.karaf.shell.commands.Option; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.net.ChannelSpacing; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.GridType; |
| 29 | import org.onosproject.net.OchSignal; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.intent.IntentId; |
| 32 | import org.onosproject.net.newresource.ResourceAllocation; |
| 33 | import org.onosproject.net.newresource.ResourceConsumer; |
| 34 | import org.onosproject.net.newresource.ResourcePath; |
| 35 | import org.onosproject.net.newresource.ResourceService; |
| 36 | |
| 37 | /** |
| 38 | * Test tool to allocate resources. |
| 39 | */ |
| 40 | @Command(scope = "onos", name = "test-allocate-resources", |
| 41 | description = "Test tool to allocate resources") |
| 42 | public class TestAllocateResource extends AbstractShellCommand { |
| 43 | |
| 44 | // TODO add support for other resource types |
| 45 | |
| 46 | // FIXME provide a proper way to specify a lambda and lambda ranges |
| 47 | @Option(name = "-l", aliases = "--lambda", |
| 48 | description = "Lambda Resource to allocate", |
| 49 | required = false, multiValued = false) |
| 50 | private String lambda = "1"; |
| 51 | |
| 52 | @Option(name = "-i", aliases = "--intentId", |
| 53 | description = "IntentId to use for allocation", |
| 54 | required = false, multiValued = false) |
| 55 | private int nIntendId = 42; |
| 56 | |
| 57 | |
| 58 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", |
| 59 | required = true, multiValued = false) |
| 60 | String deviceIdStr = null; |
| 61 | |
| 62 | @Argument(index = 1, name = "portNumberString", description = "PortNumber", |
| 63 | required = true, multiValued = false) |
| 64 | String portNumberStr = null; |
| 65 | |
| 66 | private ResourceService resourceService; |
| 67 | |
| 68 | @Override |
| 69 | protected void execute() { |
| 70 | resourceService = get(ResourceService.class); |
| 71 | DeviceId did = DeviceId.deviceId(deviceIdStr); |
| 72 | PortNumber portNum = PortNumber.fromString(portNumberStr); |
| 73 | |
| 74 | ResourceConsumer consumer = IntentId.valueOf(nIntendId); |
| 75 | |
| 76 | ResourcePath resource = discrete(did, portNum, |
| 77 | createLambda(Integer.parseInt(lambda))); |
| 78 | |
| 79 | Optional<ResourceAllocation> allocate = resourceService.allocate(consumer, resource); |
| 80 | if (allocate.isPresent()) { |
| 81 | print("Allocated: %s", allocate.get()); |
| 82 | } else { |
| 83 | print("Failed to allocate %s for %s", resource, consumer); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private OchSignal createLambda(int i) { |
| 88 | return new OchSignal(GridType.FLEX, |
| 89 | ChannelSpacing.CHL_12P5GHZ, |
| 90 | i, |
| 91 | 1); |
| 92 | } |
| 93 | |
| 94 | } |