blob: 869868f3f51b96252309aab4f0e6499d91decd4b [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
18import static org.onosproject.net.DeviceId.deviceId;
19
20import java.util.Collection;
21import java.util.stream.StreamSupport;
22
23import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
25import org.apache.karaf.shell.commands.Option;
26import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.OchSignal;
30import org.onosproject.net.Port;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.device.DeviceService;
33import org.onosproject.net.newresource.ResourceAllocation;
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
37import com.google.common.base.Strings;
38
39/**
40 * Lists allocated resources.
41 */
42@Command(scope = "onos", name = "allocations",
43 description = "Lists allocated resources")
44public class AllocationsCommand extends AbstractShellCommand {
45
46 // TODO add other resource types
47 @Option(name = "-l", aliases = "--lambda", description = "Lambda Resource",
48 required = false, multiValued = false)
49 private boolean lambda = true;
50
51 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
52 required = false, multiValued = false)
53 String deviceIdStr = null;
54
55 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
56 required = false, multiValued = false)
57 String portNumberStr = null;
58
59
60
61 private DeviceService deviceService;
62 private ResourceService resourceService;
63
64 @Override
65 protected void execute() {
66 deviceService = get(DeviceService.class);
67 resourceService = get(ResourceService.class);
68
69
70 if (deviceIdStr != null && portNumberStr != null) {
71 DeviceId deviceId = deviceId(deviceIdStr);
72 PortNumber portNumber = PortNumber.fromString(portNumberStr);
73
74 printAllocation(deviceId, portNumber, 0);
75 } else if (deviceIdStr != null) {
76 DeviceId deviceId = deviceId(deviceIdStr);
77
78 printAllocation(deviceId, 0);
79 } else {
80 printAllocation();
81 }
82
83 }
84
85 private void printAllocation() {
86 print("ROOT");
87 StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false)
88 .map(Device::id)
89 .forEach(did -> printAllocation(did, 1));
90 }
91
92 private void printAllocation(DeviceId did, int level) {
93 print("%s%s", Strings.repeat(" ", level), did);
94 StreamSupport.stream(deviceService.getPorts(did).spliterator(), false)
95 .map(Port::number)
96 .forEach(num -> printAllocation(did, num, level + 1));
97 }
98
99 private void printAllocation(DeviceId did, PortNumber num, int level) {
100 if (level == 0) {
101 // print DeviceId when Port was directly specified.
102 print("%s", did);
103 }
104 print("%s%s", Strings.repeat(" ", level), asVerboseString(num));
105
106 // TODO: Current design cannot deal with sub-resources
107 // (e.g., TX/RX under Port)
108
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800109 Resource resource = Resource.discrete(did, num);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800110 if (lambda) {
111 //print("Lambda resources:");
112 Collection<ResourceAllocation> allocations
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800113 = resourceService.getResourceAllocations(resource, OchSignal.class);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800114
115 for (ResourceAllocation a : allocations) {
116 print("%s%s allocated by %s", Strings.repeat(" ", level + 1),
117 a.resource().last(), asVerboseString(a.consumer()));
118 }
119 }
120 }
121
122 /**
123 * Add type name if the toString does not start with them.
124 *
125 * e.g., IntentId#toString result in "42"
126 * asVerboseString(id) will result in "IntentId:42"
127 *
128 * @param obj non-null Object to print.
129 * @return verbose String representation
130 */
131 private static String asVerboseString(Object obj) {
132 String name = obj.getClass().getSimpleName();
133 String toString = String.valueOf(obj);
134 if (toString.startsWith(name)) {
135 return toString;
136 } else {
137 return String.format("%s:%s", name, toString);
138 }
139 }
140
141}