blob: 172687a548eacfcd46e6c8825a4ca207b5f698eb [file] [log] [blame]
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08003 *
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
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080018import java.util.Optional;
19
20import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
22import org.apache.karaf.shell.commands.Option;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.ChannelSpacing;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.GridType;
27import org.onosproject.net.OchSignal;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.intent.IntentId;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080030import org.onosproject.net.resource.ResourceAllocation;
31import org.onosproject.net.resource.ResourceConsumer;
32import org.onosproject.net.resource.Resource;
33import org.onosproject.net.resource.ResourceService;
34import org.onosproject.net.resource.Resources;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080035
36/**
37 * Test tool to allocate resources.
38 */
39@Command(scope = "onos", name = "test-allocate-resources",
40 description = "Test tool to allocate resources")
41public 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
Yuta HIGUCHI7fc72462017-03-29 19:07:03 -070057 @Argument(index = 0, name = "deviceId", description = "Device ID",
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080058 required = true, multiValued = false)
59 String deviceIdStr = null;
60
Yuta HIGUCHI7fc72462017-03-29 19:07:03 -070061 @Argument(index = 1, name = "portNumber", description = "PortNumber",
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080062 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 SHIMIZU460b9722016-01-28 10:48:26 -080075 Resource resource = Resources.discrete(did, portNum,
76 createLambda(Integer.parseInt(lambda))).resource();
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080077
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,
Yuta HIGUCHI7fc72462017-03-29 19:07:03 -070088 ChannelSpacing.CHL_6P25GHZ,
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080089 i,
90 1);
91 }
92
93}