blob: 2b25744b3edde164d00e2c578eb438b142ed01f2 [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 java.util.Collections;
19import java.util.List;
Ray Milkey39616f32015-05-14 15:43:00 -070020import java.util.Map;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070021import java.util.SortedMap;
22import java.util.TreeMap;
23
24import org.apache.karaf.shell.commands.Argument;
Jonathan Hart32600692015-03-09 10:38:40 -070025import org.apache.karaf.shell.commands.Command;
26import org.onosproject.cli.AbstractShellCommand;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070027import org.onosproject.cli.Comparators;
28import org.onosproject.net.Device;
Jonathan Hart32600692015-03-09 10:38:40 -070029import org.onosproject.net.DeviceId;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.net.group.Group;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070032import org.onosproject.net.group.Group.GroupState;
33import org.onosproject.net.group.GroupBucket;
Jonathan Hart32600692015-03-09 10:38:40 -070034import org.onosproject.net.group.GroupService;
35
Ray Milkey39616f32015-05-14 15:43:00 -070036import com.fasterxml.jackson.databind.JsonNode;
37import com.fasterxml.jackson.databind.node.ArrayNode;
38
39import static com.google.common.collect.Lists.newArrayList;
40
Jonathan Hart32600692015-03-09 10:38:40 -070041/**
42 * Lists all groups in the system.
43 */
44@Command(scope = "onos", name = "groups",
45 description = "Lists all groups in the system")
46public class GroupsListCommand extends AbstractShellCommand {
47
48 private static final String FORMAT =
Charles Chanccd59a42015-11-10 17:52:59 -080049 " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070050 private static final String BUCKET_FORMAT =
Charles Chanccd59a42015-11-10 17:52:59 -080051 " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070052
53 @Argument(index = 1, name = "uri", description = "Device ID",
54 required = false, multiValued = false)
55 String uri = null;
56
57 @Argument(index = 0, name = "state", description = "Group state",
58 required = false, multiValued = false)
59 String state;
Jonathan Hart32600692015-03-09 10:38:40 -070060
Ray Milkey39616f32015-05-14 15:43:00 -070061 private JsonNode json(Map<Device, List<Group>> sortedGroups) {
62 ArrayNode result = mapper().createArrayNode();
63
64 sortedGroups.forEach((device, groups) ->
65 groups.forEach(group ->
66 result.add(jsonForEntity(group, Group.class))));
67
68 return result;
69 }
70
Jonathan Hart32600692015-03-09 10:38:40 -070071 @Override
72 protected void execute() {
73 DeviceService deviceService = get(DeviceService.class);
74 GroupService groupService = get(GroupService.class);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070075 SortedMap<Device, List<Group>> sortedGroups =
76 getSortedGroups(deviceService, groupService);
Jonathan Hart32600692015-03-09 10:38:40 -070077
Ray Milkey39616f32015-05-14 15:43:00 -070078 if (outputJson()) {
79 print("%s", json(sortedGroups));
80 } else {
81 sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
82 }
Jonathan Hart32600692015-03-09 10:38:40 -070083 }
84
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070085 /**
86 * Returns the list of devices sorted using the device ID URIs.
87 *
88 * @param deviceService device service
89 * @param groupService group service
90 * @return sorted device list
91 */
92 protected SortedMap<Device, List<Group>>
93 getSortedGroups(DeviceService deviceService,
94 GroupService groupService) {
95 SortedMap<Device, List<Group>> sortedGroups =
96 new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
97 List<Group> groups;
98 GroupState s = null;
99 if (state != null && !state.equals("any")) {
100 s = GroupState.valueOf(state.toUpperCase());
101 }
102 Iterable<Device> devices = (uri == null) ? deviceService.getDevices() :
103 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
104 for (Device d : devices) {
105 if (s == null) {
106 groups = newArrayList(groupService.getGroups(d.id()));
107 } else {
108 groups = newArrayList();
109 for (Group g : groupService.getGroups(d.id())) {
110 if (g.state().equals(s)) {
111 groups.add(g);
112 }
113 }
114 }
115 groups.sort(Comparators.GROUP_COMPARATOR);
116 sortedGroups.put(d, groups);
117 }
118 return sortedGroups;
119 }
120
121 private void printGroups(DeviceId deviceId, List<Group> groups) {
Saurav Das4ce45962015-11-24 23:21:05 -0800122 print("deviceId=%s, groupCount=%s", deviceId, groups.size());
Jonathan Hart32600692015-03-09 10:38:40 -0700123 for (Group group : groups) {
Saurav Das4f980082015-11-05 13:39:15 -0800124 print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(),
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700125 group.bytes(), group.packets(), group.appId().name());
126 int i = 0;
127 for (GroupBucket bucket:group.buckets().buckets()) {
Saurav Das4f980082015-11-05 13:39:15 -0800128 print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700129 bucket.bytes(), bucket.packets(),
130 bucket.treatment().allInstructions());
131 }
Jonathan Hart32600692015-03-09 10:38:40 -0700132 }
133 }
134}