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