blob: d704509babc22bb2f0ee44af8633ceff387f8cb2 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070022import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
24import org.apache.karaf.shell.api.action.Option;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080025import 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;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080032import org.onosproject.net.resource.ResourceAllocation;
33import org.onosproject.net.resource.ResourceConsumer;
34import org.onosproject.net.resource.Resource;
35import org.onosproject.net.resource.ResourceService;
36import org.onosproject.net.resource.Resources;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080037
38/**
39 * Test tool to allocate resources.
40 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Service
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080042@Command(scope = "onos", name = "test-allocate-resources",
43 description = "Test tool to allocate resources")
44public 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 HIGUCHI7fc72462017-03-29 19:07:03 -070060 @Argument(index = 0, name = "deviceId", description = "Device ID",
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080061 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070062 @Completion(DeviceIdCompleter.class)
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080063 String deviceIdStr = null;
64
Yuta HIGUCHI7fc72462017-03-29 19:07:03 -070065 @Argument(index = 1, name = "portNumber", description = "PortNumber",
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080066 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070067 @Completion(PortNumberCompleter.class)
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080068 String portNumberStr = null;
69
70 private ResourceService resourceService;
71
72 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073 protected void doExecute() {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080074 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 SHIMIZU460b9722016-01-28 10:48:26 -080080 Resource resource = Resources.discrete(did, portNum,
81 createLambda(Integer.parseInt(lambda))).resource();
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080082
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 HIGUCHI7fc72462017-03-29 19:07:03 -070093 ChannelSpacing.CHL_6P25GHZ,
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080094 i,
95 1);
96 }
97
98}