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