blob: 4631920067c0f9021fcdfee781910e7549944796 [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.apache.karaf.shell.api.action.Option;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.ChannelSpacing;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.GridType;
28import org.onosproject.net.OchSignal;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.intent.IntentId;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080031import org.onosproject.net.resource.ResourceAllocation;
32import org.onosproject.net.resource.ResourceConsumer;
33import org.onosproject.net.resource.Resource;
34import org.onosproject.net.resource.ResourceService;
35import org.onosproject.net.resource.Resources;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080036
37/**
38 * Test tool to allocate resources.
39 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080041@Command(scope = "onos", name = "test-allocate-resources",
42 description = "Test tool to allocate resources")
43public 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 HIGUCHI7fc72462017-03-29 19:07:03 -070059 @Argument(index = 0, name = "deviceId", description = "Device ID",
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080060 required = true, multiValued = false)
61 String deviceIdStr = null;
62
Yuta HIGUCHI7fc72462017-03-29 19:07:03 -070063 @Argument(index = 1, name = "portNumber", description = "PortNumber",
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080064 required = true, multiValued = false)
65 String portNumberStr = null;
66
67 private ResourceService resourceService;
68
69 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070 protected void doExecute() {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080071 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 SHIMIZU460b9722016-01-28 10:48:26 -080077 Resource resource = Resources.discrete(did, portNum,
78 createLambda(Integer.parseInt(lambda))).resource();
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080079
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 HIGUCHI7fc72462017-03-29 19:07:03 -070090 ChannelSpacing.CHL_6P25GHZ,
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080091 i,
92 1);
93 }
94
95}