blob: 70fa2a7948e82ea014b8fa6a39a51fd99a11147d [file] [log] [blame]
Jonathan Hart32600692015-03-09 10:38:40 -07001/*
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
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070018import static com.google.common.collect.Lists.newArrayList;
19
20import java.util.Collections;
21import java.util.List;
22import java.util.SortedMap;
23import java.util.TreeMap;
24
25import org.apache.karaf.shell.commands.Argument;
Jonathan Hart32600692015-03-09 10:38:40 -070026import org.apache.karaf.shell.commands.Command;
27import org.onosproject.cli.AbstractShellCommand;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070028import org.onosproject.cli.Comparators;
29import org.onosproject.net.Device;
Jonathan Hart32600692015-03-09 10:38:40 -070030import org.onosproject.net.DeviceId;
31import org.onosproject.net.device.DeviceService;
32import org.onosproject.net.group.Group;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070033import org.onosproject.net.group.Group.GroupState;
34import org.onosproject.net.group.GroupBucket;
Jonathan Hart32600692015-03-09 10:38:40 -070035import org.onosproject.net.group.GroupService;
36
37/**
38 * Lists all groups in the system.
39 */
40@Command(scope = "onos", name = "groups",
41 description = "Lists all groups in the system")
42public class GroupsListCommand extends AbstractShellCommand {
43
44 private static final String FORMAT =
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070045 " id=%s, state=%s, bytes=%s, packets=%s, appId=%s";
46 private static final String BUCKET_FORMAT =
47 " id=%s, bucket=%s, bytes=%s, packets=%s, actions=%s";
48
49 @Argument(index = 1, name = "uri", description = "Device ID",
50 required = false, multiValued = false)
51 String uri = null;
52
53 @Argument(index = 0, name = "state", description = "Group state",
54 required = false, multiValued = false)
55 String state;
Jonathan Hart32600692015-03-09 10:38:40 -070056
57 @Override
58 protected void execute() {
59 DeviceService deviceService = get(DeviceService.class);
60 GroupService groupService = get(GroupService.class);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070061 SortedMap<Device, List<Group>> sortedGroups =
62 getSortedGroups(deviceService, groupService);
Jonathan Hart32600692015-03-09 10:38:40 -070063
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070064 sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
Jonathan Hart32600692015-03-09 10:38:40 -070065 }
66
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070067 /**
68 * Returns the list of devices sorted using the device ID URIs.
69 *
70 * @param deviceService device service
71 * @param groupService group service
72 * @return sorted device list
73 */
74 protected SortedMap<Device, List<Group>>
75 getSortedGroups(DeviceService deviceService,
76 GroupService groupService) {
77 SortedMap<Device, List<Group>> sortedGroups =
78 new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
79 List<Group> groups;
80 GroupState s = null;
81 if (state != null && !state.equals("any")) {
82 s = GroupState.valueOf(state.toUpperCase());
83 }
84 Iterable<Device> devices = (uri == null) ? deviceService.getDevices() :
85 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
86 for (Device d : devices) {
87 if (s == null) {
88 groups = newArrayList(groupService.getGroups(d.id()));
89 } else {
90 groups = newArrayList();
91 for (Group g : groupService.getGroups(d.id())) {
92 if (g.state().equals(s)) {
93 groups.add(g);
94 }
95 }
96 }
97 groups.sort(Comparators.GROUP_COMPARATOR);
98 sortedGroups.put(d, groups);
99 }
100 return sortedGroups;
101 }
102
103 private void printGroups(DeviceId deviceId, List<Group> groups) {
Jonathan Hart32600692015-03-09 10:38:40 -0700104 print("deviceId=%s", deviceId);
105 for (Group group : groups) {
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700106 print(FORMAT, group.id().id(), group.state(),
107 group.bytes(), group.packets(), group.appId().name());
108 int i = 0;
109 for (GroupBucket bucket:group.buckets().buckets()) {
110 print(BUCKET_FORMAT, group.id().id(), ++i,
111 bucket.bytes(), bucket.packets(),
112 bucket.treatment().allInstructions());
113 }
Jonathan Hart32600692015-03-09 10:38:40 -0700114 }
115 }
116}