blob: 08824290467e4603241f69e3a33c9d00a675736e [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;
Rimon Ashkenazyfbac3782016-05-02 14:01:41 +030040import org.onosproject.net.TributarySlot;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080041import org.onosproject.net.device.DeviceService;
Naoki Shiota784af312016-02-01 15:47:21 -080042import org.onosproject.net.intent.IntentId;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070043import org.onosproject.net.resource.ResourceConsumerId;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080044import org.onosproject.net.resource.Resources;
45import org.onosproject.net.resource.DiscreteResourceId;
46import org.onosproject.net.resource.ResourceAllocation;
47import org.onosproject.net.resource.ResourceService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080048
49/**
50 * Lists allocated resources.
51 */
52@Command(scope = "onos", name = "allocations",
53 description = "Lists allocated resources")
54public class AllocationsCommand extends AbstractShellCommand {
55
Naoki Shiota784af312016-02-01 15:47:21 -080056 @Option(name = "-t", aliases = "--type", description = "List of resource types",
57 required = false, multiValued = true)
58 String[] typeStrings = null;
59
60 Set<String> typesToPrint;
61
62 @Option(name = "-i", aliases = "--intentId", description = "Intent ID",
63 required = false, multiValued = true)
64 String[] intentStrings;
65
66 Set<String> intentsToPrint;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080067
68 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
69 required = false, multiValued = false)
70 String deviceIdStr = null;
71
72 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
73 required = false, multiValued = false)
74 String portNumberStr = null;
75
76
77
78 private DeviceService deviceService;
79 private ResourceService resourceService;
80
81 @Override
82 protected void execute() {
83 deviceService = get(DeviceService.class);
84 resourceService = get(ResourceService.class);
85
Naoki Shiota784af312016-02-01 15:47:21 -080086 if (typeStrings != null) {
87 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
88 } else {
89 typesToPrint = Collections.emptySet();
90 }
91
92 if (intentStrings != null) {
93 intentsToPrint = new HashSet<>(Arrays.asList(intentStrings));
94 } else {
95 intentsToPrint = Collections.emptySet();
96 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080097
98 if (deviceIdStr != null && portNumberStr != null) {
99 DeviceId deviceId = deviceId(deviceIdStr);
100 PortNumber portNumber = PortNumber.fromString(portNumberStr);
101
102 printAllocation(deviceId, portNumber, 0);
103 } else if (deviceIdStr != null) {
104 DeviceId deviceId = deviceId(deviceIdStr);
105
106 printAllocation(deviceId, 0);
107 } else {
108 printAllocation();
109 }
110
111 }
112
113 private void printAllocation() {
114 print("ROOT");
115 StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false)
116 .map(Device::id)
117 .forEach(did -> printAllocation(did, 1));
118 }
119
120 private void printAllocation(DeviceId did, int level) {
121 print("%s%s", Strings.repeat(" ", level), did);
122 StreamSupport.stream(deviceService.getPorts(did).spliterator(), false)
123 .map(Port::number)
124 .forEach(num -> printAllocation(did, num, level + 1));
125 }
126
127 private void printAllocation(DeviceId did, PortNumber num, int level) {
128 if (level == 0) {
129 // print DeviceId when Port was directly specified.
130 print("%s", did);
131 }
132 print("%s%s", Strings.repeat(" ", level), asVerboseString(num));
133
Naoki Shiota784af312016-02-01 15:47:21 -0800134 // FIXME: This workaround induces a lot of distributed store access.
135 // ResourceService should have an API to get all allocations under a parent resource.
136 Set<Class<?>> subResourceTypes = ImmutableSet.<Class<?>>builder()
137 .add(OchSignal.class)
138 .add(VlanId.class)
139 .add(MplsLabel.class)
Naoki Shiotad7654d42016-03-31 18:46:49 -0700140 .add(Bandwidth.class)
Rimon Ashkenazyfbac3782016-05-02 14:01:41 +0300141 .add(TributarySlot.class)
Naoki Shiota784af312016-02-01 15:47:21 -0800142 .build();
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800143
Naoki Shiota784af312016-02-01 15:47:21 -0800144 DiscreteResourceId resourceId = Resources.discrete(did, num).id();
145 for (Class<?> t : subResourceTypes) {
146 resourceService.getResourceAllocations(resourceId, t).stream()
147 .filter(a -> isSubjectToPrint(a))
148 .forEach(a -> print("%s%s allocated by %s", Strings.repeat(" ", level + 1),
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700149 a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumerId())));
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800150
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800151 }
152 }
153
Naoki Shiota784af312016-02-01 15:47:21 -0800154 private boolean isSubjectToPrint(ResourceAllocation allocation) {
155 if (!intentsToPrint.isEmpty()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700156 && allocation.consumerId().isClassOf(IntentId.class)
157 && !intentsToPrint.contains(allocation.consumerId().toString())) {
Naoki Shiota784af312016-02-01 15:47:21 -0800158 return false;
159 }
160
161 if (!typesToPrint.isEmpty()
162 && !typesToPrint.contains(allocation.resource().simpleTypeName())) {
163 return false;
164 }
165
166 return true;
167 }
168
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800169 /**
170 * Add type name if the toString does not start with them.
171 *
172 * e.g., IntentId#toString result in "42"
173 * asVerboseString(id) will result in "IntentId:42"
174 *
175 * @param obj non-null Object to print.
176 * @return verbose String representation
177 */
178 private static String asVerboseString(Object obj) {
179 String name = obj.getClass().getSimpleName();
180 String toString = String.valueOf(obj);
181 if (toString.startsWith(name)) {
182 return toString;
183 } else {
184 return String.format("%s:%s", name, toString);
185 }
186 }
187
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700188 private static String asVerboseString(ResourceConsumerId consumerId) {
189 return String.format("%s:%s", consumerId.consumerClass(), consumerId.value());
190 }
191
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800192}