blob: 611f51a6973af7234176529912a23e54b5d23d24 [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
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
Naoki Shiota784af312016-02-01 15:47:21 -080026import com.google.common.collect.ImmutableSet;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080027import org.apache.karaf.shell.commands.Argument;
28import org.apache.karaf.shell.commands.Command;
29import org.apache.karaf.shell.commands.Option;
Naoki Shiota784af312016-02-01 15:47:21 -080030import org.onlab.packet.MplsLabel;
31import org.onlab.packet.VlanId;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080032import org.onosproject.cli.AbstractShellCommand;
33import org.onosproject.net.Device;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.OchSignal;
36import org.onosproject.net.Port;
37import org.onosproject.net.PortNumber;
38import org.onosproject.net.device.DeviceService;
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080039import org.onosproject.net.newresource.DiscreteResourceId;
Naoki Shiota784af312016-02-01 15:47:21 -080040import org.onosproject.net.intent.IntentId;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080041import org.onosproject.net.newresource.ResourceAllocation;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080042import org.onosproject.net.newresource.ResourceService;
43
44import com.google.common.base.Strings;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080045import org.onosproject.net.newresource.Resources;
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)
138 .build();
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800139
Naoki Shiota784af312016-02-01 15:47:21 -0800140 DiscreteResourceId resourceId = Resources.discrete(did, num).id();
141 for (Class<?> t : subResourceTypes) {
142 resourceService.getResourceAllocations(resourceId, t).stream()
143 .filter(a -> isSubjectToPrint(a))
144 .forEach(a -> print("%s%s allocated by %s", Strings.repeat(" ", level + 1),
145 a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumer())));
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800146
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800147 }
148 }
149
Naoki Shiota784af312016-02-01 15:47:21 -0800150 private boolean isSubjectToPrint(ResourceAllocation allocation) {
151 if (!intentsToPrint.isEmpty()
152 && allocation.consumer() instanceof IntentId
153 && !intentsToPrint.contains(allocation.consumer().toString())) {
154 return false;
155 }
156
157 if (!typesToPrint.isEmpty()
158 && !typesToPrint.contains(allocation.resource().simpleTypeName())) {
159 return false;
160 }
161
162 return true;
163 }
164
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800165 /**
166 * Add type name if the toString does not start with them.
167 *
168 * e.g., IntentId#toString result in "42"
169 * asVerboseString(id) will result in "IntentId:42"
170 *
171 * @param obj non-null Object to print.
172 * @return verbose String representation
173 */
174 private static String asVerboseString(Object obj) {
175 String name = obj.getClass().getSimpleName();
176 String toString = String.valueOf(obj);
177 if (toString.startsWith(name)) {
178 return toString;
179 } else {
180 return String.format("%s:%s", name, toString);
181 }
182 }
183
184}