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