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