blob: f2f4c0c1df8c79dbc7180bbff517062e9a7bdee7 [file] [log] [blame]
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
18import static org.onosproject.net.DeviceId.deviceId;
19
Naoki Shiota784af312016-02-01 15:47:21 -080020import java.util.Arrays;
21import java.util.Collections;
22import java.util.HashSet;
23import java.util.Set;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080024import java.util.stream.StreamSupport;
25
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080026import com.google.common.base.Strings;
Naoki Shiota784af312016-02-01 15:47:21 -080027import com.google.common.collect.ImmutableSet;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080028import org.apache.karaf.shell.commands.Argument;
29import org.apache.karaf.shell.commands.Command;
30import org.apache.karaf.shell.commands.Option;
Naoki Shiota784af312016-02-01 15:47:21 -080031import org.onlab.packet.MplsLabel;
32import org.onlab.packet.VlanId;
Naoki Shiotad7654d42016-03-31 18:46:49 -070033import org.onlab.util.Bandwidth;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080034import org.onosproject.cli.AbstractShellCommand;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.OchSignal;
38import org.onosproject.net.Port;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.device.DeviceService;
Naoki Shiota784af312016-02-01 15:47:21 -080041import org.onosproject.net.intent.IntentId;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080042import org.onosproject.net.resource.Resources;
43import org.onosproject.net.resource.DiscreteResourceId;
44import org.onosproject.net.resource.ResourceAllocation;
45import org.onosproject.net.resource.ResourceService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080046
47/**
48 * Lists allocated resources.
49 */
50@Command(scope = "onos", name = "allocations",
51 description = "Lists allocated resources")
52public class AllocationsCommand extends AbstractShellCommand {
53
Naoki Shiota784af312016-02-01 15:47:21 -080054 @Option(name = "-t", aliases = "--type", description = "List of resource types",
55 required = false, multiValued = true)
56 String[] typeStrings = null;
57
58 Set<String> typesToPrint;
59
60 @Option(name = "-i", aliases = "--intentId", description = "Intent ID",
61 required = false, multiValued = true)
62 String[] intentStrings;
63
64 Set<String> intentsToPrint;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080065
66 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
67 required = false, multiValued = false)
68 String deviceIdStr = null;
69
70 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
71 required = false, multiValued = false)
72 String portNumberStr = null;
73
74
75
76 private DeviceService deviceService;
77 private ResourceService resourceService;
78
79 @Override
80 protected void execute() {
81 deviceService = get(DeviceService.class);
82 resourceService = get(ResourceService.class);
83
Naoki Shiota784af312016-02-01 15:47:21 -080084 if (typeStrings != null) {
85 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
86 } else {
87 typesToPrint = Collections.emptySet();
88 }
89
90 if (intentStrings != null) {
91 intentsToPrint = new HashSet<>(Arrays.asList(intentStrings));
92 } else {
93 intentsToPrint = Collections.emptySet();
94 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080095
96 if (deviceIdStr != null && portNumberStr != null) {
97 DeviceId deviceId = deviceId(deviceIdStr);
98 PortNumber portNumber = PortNumber.fromString(portNumberStr);
99
100 printAllocation(deviceId, portNumber, 0);
101 } else if (deviceIdStr != null) {
102 DeviceId deviceId = deviceId(deviceIdStr);
103
104 printAllocation(deviceId, 0);
105 } else {
106 printAllocation();
107 }
108
109 }
110
111 private void printAllocation() {
112 print("ROOT");
113 StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false)
114 .map(Device::id)
115 .forEach(did -> printAllocation(did, 1));
116 }
117
118 private void printAllocation(DeviceId did, int level) {
119 print("%s%s", Strings.repeat(" ", level), did);
120 StreamSupport.stream(deviceService.getPorts(did).spliterator(), false)
121 .map(Port::number)
122 .forEach(num -> printAllocation(did, num, level + 1));
123 }
124
125 private void printAllocation(DeviceId did, PortNumber num, int level) {
126 if (level == 0) {
127 // print DeviceId when Port was directly specified.
128 print("%s", did);
129 }
130 print("%s%s", Strings.repeat(" ", level), asVerboseString(num));
131
Naoki Shiota784af312016-02-01 15:47:21 -0800132 // FIXME: This workaround induces a lot of distributed store access.
133 // ResourceService should have an API to get all allocations under a parent resource.
134 Set<Class<?>> subResourceTypes = ImmutableSet.<Class<?>>builder()
135 .add(OchSignal.class)
136 .add(VlanId.class)
137 .add(MplsLabel.class)
Naoki Shiotad7654d42016-03-31 18:46:49 -0700138 .add(Bandwidth.class)
Naoki Shiota784af312016-02-01 15:47:21 -0800139 .build();
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800140
Naoki Shiota784af312016-02-01 15:47:21 -0800141 DiscreteResourceId resourceId = Resources.discrete(did, num).id();
142 for (Class<?> t : subResourceTypes) {
143 resourceService.getResourceAllocations(resourceId, t).stream()
144 .filter(a -> isSubjectToPrint(a))
145 .forEach(a -> print("%s%s allocated by %s", Strings.repeat(" ", level + 1),
146 a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumer())));
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800147
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800148 }
149 }
150
Naoki Shiota784af312016-02-01 15:47:21 -0800151 private boolean isSubjectToPrint(ResourceAllocation allocation) {
152 if (!intentsToPrint.isEmpty()
153 && allocation.consumer() instanceof IntentId
154 && !intentsToPrint.contains(allocation.consumer().toString())) {
155 return false;
156 }
157
158 if (!typesToPrint.isEmpty()
159 && !typesToPrint.contains(allocation.resource().simpleTypeName())) {
160 return false;
161 }
162
163 return true;
164 }
165
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800166 /**
167 * Add type name if the toString does not start with them.
168 *
169 * e.g., IntentId#toString result in "42"
170 * asVerboseString(id) will result in "IntentId:42"
171 *
172 * @param obj non-null Object to print.
173 * @return verbose String representation
174 */
175 private static String asVerboseString(Object obj) {
176 String name = obj.getClass().getSimpleName();
177 String toString = String.valueOf(obj);
178 if (toString.startsWith(name)) {
179 return toString;
180 } else {
181 return String.format("%s:%s", name, toString);
182 }
183 }
184
185}