blob: be50a39ef7230357b34fd1d6cd10eb3d95f73a4e [file] [log] [blame]
Jonathan Hart32600692015-03-09 10:38:40 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hart32600692015-03-09 10:38:40 -07003 *
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;
Charles Chanf9b94ab2016-02-23 19:31:41 -080026import org.apache.karaf.shell.commands.Option;
Jonathan Hart32600692015-03-09 10:38:40 -070027import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080028import org.onosproject.utils.Comparators;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070029import 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
Ray Milkey39616f32015-05-14 15:43:00 -070037import com.fasterxml.jackson.databind.JsonNode;
38import com.fasterxml.jackson.databind.node.ArrayNode;
39
40import static com.google.common.collect.Lists.newArrayList;
41
Jonathan Hart32600692015-03-09 10:38:40 -070042/**
43 * Lists all groups in the system.
44 */
45@Command(scope = "onos", name = "groups",
46 description = "Lists all groups in the system")
47public class GroupsListCommand extends AbstractShellCommand {
48
Charles Chanf38aca72016-02-18 13:40:18 -080049 public static final String ANY = "any";
50
Jonathan Hart32600692015-03-09 10:38:40 -070051 private static final String FORMAT =
Charles Chanccd59a42015-11-10 17:52:59 -080052 " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070053 private static final String BUCKET_FORMAT =
Charles Chanccd59a42015-11-10 17:52:59 -080054 " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070055
56 @Argument(index = 1, name = "uri", description = "Device ID",
57 required = false, multiValued = false)
58 String uri = null;
59
60 @Argument(index = 0, name = "state", description = "Group state",
61 required = false, multiValued = false)
62 String state;
Jonathan Hart32600692015-03-09 10:38:40 -070063
Charles Chanf9b94ab2016-02-23 19:31:41 -080064 @Option(name = "-c", aliases = "--count",
65 description = "Print group count only",
66 required = false, multiValued = false)
67 private boolean countOnly = false;
68
Ray Milkey39616f32015-05-14 15:43:00 -070069 private JsonNode json(Map<Device, List<Group>> sortedGroups) {
70 ArrayNode result = mapper().createArrayNode();
71
72 sortedGroups.forEach((device, groups) ->
73 groups.forEach(group ->
74 result.add(jsonForEntity(group, Group.class))));
75
76 return result;
77 }
78
Jonathan Hart32600692015-03-09 10:38:40 -070079 @Override
80 protected void execute() {
81 DeviceService deviceService = get(DeviceService.class);
82 GroupService groupService = get(GroupService.class);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070083 SortedMap<Device, List<Group>> sortedGroups =
84 getSortedGroups(deviceService, groupService);
Jonathan Hart32600692015-03-09 10:38:40 -070085
Ray Milkey39616f32015-05-14 15:43:00 -070086 if (outputJson()) {
87 print("%s", json(sortedGroups));
88 } else {
89 sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
90 }
Jonathan Hart32600692015-03-09 10:38:40 -070091 }
92
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070093 /**
94 * Returns the list of devices sorted using the device ID URIs.
95 *
96 * @param deviceService device service
97 * @param groupService group service
98 * @return sorted device list
99 */
100 protected SortedMap<Device, List<Group>>
101 getSortedGroups(DeviceService deviceService,
102 GroupService groupService) {
103 SortedMap<Device, List<Group>> sortedGroups =
104 new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
105 List<Group> groups;
106 GroupState s = null;
Saurav Das36b37932017-04-07 17:24:38 -0700107 if (state != null && !"any".equals(state)) {
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700108 s = GroupState.valueOf(state.toUpperCase());
109 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700110 Iterable<Device> devices = deviceService.getDevices();
111 if (uri != null) {
112 Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
113 if (dev != null) {
114 devices = Collections.singletonList(dev);
115 }
116 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700117 for (Device d : devices) {
118 if (s == null) {
119 groups = newArrayList(groupService.getGroups(d.id()));
120 } else {
121 groups = newArrayList();
122 for (Group g : groupService.getGroups(d.id())) {
123 if (g.state().equals(s)) {
124 groups.add(g);
125 }
126 }
127 }
128 groups.sort(Comparators.GROUP_COMPARATOR);
129 sortedGroups.put(d, groups);
130 }
131 return sortedGroups;
132 }
133
134 private void printGroups(DeviceId deviceId, List<Group> groups) {
Saurav Das4ce45962015-11-24 23:21:05 -0800135 print("deviceId=%s, groupCount=%s", deviceId, groups.size());
Charles Chanf9b94ab2016-02-23 19:31:41 -0800136
137 if (countOnly) {
138 return;
139 }
140
Jonathan Hart32600692015-03-09 10:38:40 -0700141 for (Group group : groups) {
Saurav Das4f980082015-11-05 13:39:15 -0800142 print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(),
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700143 group.bytes(), group.packets(), group.appId().name());
144 int i = 0;
145 for (GroupBucket bucket:group.buckets().buckets()) {
Saurav Das4f980082015-11-05 13:39:15 -0800146 print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700147 bucket.bytes(), bucket.packets(),
148 bucket.treatment().allInstructions());
149 }
Jonathan Hart32600692015-03-09 10:38:40 -0700150 }
151 }
152}