blob: 8083c96d82fc929016196f0da5c1b2e6066d016e [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
Charles Chanf38aca72016-02-18 13:40:18 -080048 public static final String ANY = "any";
49
Jonathan Hart32600692015-03-09 10:38:40 -070050 private static final String FORMAT =
Charles Chanccd59a42015-11-10 17:52:59 -080051 " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070052 private static final String BUCKET_FORMAT =
Charles Chanccd59a42015-11-10 17:52:59 -080053 " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070054
55 @Argument(index = 1, name = "uri", description = "Device ID",
56 required = false, multiValued = false)
57 String uri = null;
58
59 @Argument(index = 0, name = "state", description = "Group state",
60 required = false, multiValued = false)
61 String state;
Jonathan Hart32600692015-03-09 10:38:40 -070062
Ray Milkey39616f32015-05-14 15:43:00 -070063 private JsonNode json(Map<Device, List<Group>> sortedGroups) {
64 ArrayNode result = mapper().createArrayNode();
65
66 sortedGroups.forEach((device, groups) ->
67 groups.forEach(group ->
68 result.add(jsonForEntity(group, Group.class))));
69
70 return result;
71 }
72
Jonathan Hart32600692015-03-09 10:38:40 -070073 @Override
74 protected void execute() {
75 DeviceService deviceService = get(DeviceService.class);
76 GroupService groupService = get(GroupService.class);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070077 SortedMap<Device, List<Group>> sortedGroups =
78 getSortedGroups(deviceService, groupService);
Jonathan Hart32600692015-03-09 10:38:40 -070079
Ray Milkey39616f32015-05-14 15:43:00 -070080 if (outputJson()) {
81 print("%s", json(sortedGroups));
82 } else {
83 sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
84 }
Jonathan Hart32600692015-03-09 10:38:40 -070085 }
86
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070087 /**
88 * Returns the list of devices sorted using the device ID URIs.
89 *
90 * @param deviceService device service
91 * @param groupService group service
92 * @return sorted device list
93 */
94 protected SortedMap<Device, List<Group>>
95 getSortedGroups(DeviceService deviceService,
96 GroupService groupService) {
97 SortedMap<Device, List<Group>> sortedGroups =
98 new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
99 List<Group> groups;
100 GroupState s = null;
101 if (state != null && !state.equals("any")) {
102 s = GroupState.valueOf(state.toUpperCase());
103 }
104 Iterable<Device> devices = (uri == null) ? deviceService.getDevices() :
105 Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
106 for (Device d : devices) {
107 if (s == null) {
108 groups = newArrayList(groupService.getGroups(d.id()));
109 } else {
110 groups = newArrayList();
111 for (Group g : groupService.getGroups(d.id())) {
112 if (g.state().equals(s)) {
113 groups.add(g);
114 }
115 }
116 }
117 groups.sort(Comparators.GROUP_COMPARATOR);
118 sortedGroups.put(d, groups);
119 }
120 return sortedGroups;
121 }
122
123 private void printGroups(DeviceId deviceId, List<Group> groups) {
Saurav Das4ce45962015-11-24 23:21:05 -0800124 print("deviceId=%s, groupCount=%s", deviceId, groups.size());
Jonathan Hart32600692015-03-09 10:38:40 -0700125 for (Group group : groups) {
Saurav Das4f980082015-11-05 13:39:15 -0800126 print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(),
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700127 group.bytes(), group.packets(), group.appId().name());
128 int i = 0;
129 for (GroupBucket bucket:group.buckets().buckets()) {
Saurav Das4f980082015-11-05 13:39:15 -0800130 print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700131 bucket.bytes(), bucket.packets(),
132 bucket.treatment().allInstructions());
133 }
Jonathan Hart32600692015-03-09 10:38:40 -0700134 }
135 }
136}