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