blob: 35d9a4cc8f84ebd30a0257657d88f4ed0e1abb0c [file] [log] [blame]
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08001/*
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 */
16package org.onosproject.cli.net;
17
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080018import static org.onosproject.net.newresource.Resource.discrete;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080019
20import java.util.Optional;
21
22import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
24import org.apache.karaf.shell.commands.Option;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.ChannelSpacing;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.GridType;
29import org.onosproject.net.OchSignal;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.intent.IntentId;
32import org.onosproject.net.newresource.ResourceAllocation;
33import org.onosproject.net.newresource.ResourceConsumer;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080034import org.onosproject.net.newresource.Resource;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080035import 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")
42public 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
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080076 Resource resource = discrete(did, portNum,
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080077 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}