blob: 4a58061647ab9aecefcdfba57522e878a195f874 [file] [log] [blame]
Jonathan Hart32600692015-03-09 10:38:40 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070020import org.apache.karaf.shell.commands.Argument;
Jonathan Hart32600692015-03-09 10:38:40 -070021import org.apache.karaf.shell.commands.Command;
Charles Chanf9b94ab2016-02-23 19:31:41 -080022import org.apache.karaf.shell.commands.Option;
Jonathan Hart32600692015-03-09 10:38:40 -070023import org.onosproject.cli.AbstractShellCommand;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070024import org.onosproject.net.Device;
Jonathan Hart32600692015-03-09 10:38:40 -070025import org.onosproject.net.DeviceId;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.group.Group;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070028import org.onosproject.net.group.Group.GroupState;
29import org.onosproject.net.group.GroupBucket;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053030import org.onosproject.net.group.GroupDescription;
Jonathan Hart32600692015-03-09 10:38:40 -070031import org.onosproject.net.group.GroupService;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053032import org.onosproject.utils.Comparators;
Jonathan Hart32600692015-03-09 10:38:40 -070033
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053034import java.util.Collections;
35import java.util.List;
36import java.util.Map;
37import java.util.SortedMap;
38import java.util.TreeMap;
39import java.util.stream.Collectors;
Ray Milkey39616f32015-05-14 15:43:00 -070040
41import static com.google.common.collect.Lists.newArrayList;
42
Jonathan Hart32600692015-03-09 10:38:40 -070043/**
44 * Lists all groups in the system.
45 */
46@Command(scope = "onos", name = "groups",
47 description = "Lists all groups in the system")
48public class GroupsListCommand extends AbstractShellCommand {
49
Charles Chanf38aca72016-02-18 13:40:18 -080050 public static final String ANY = "any";
51
Jonathan Hart32600692015-03-09 10:38:40 -070052 private static final String FORMAT =
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053053 " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s, referenceCount=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070054 private static final String BUCKET_FORMAT =
Andreas Pantelopoulos250ad712017-10-02 17:44:59 -040055 " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070056
57 @Argument(index = 1, name = "uri", description = "Device ID",
58 required = false, multiValued = false)
59 String uri = null;
60
61 @Argument(index = 0, name = "state", description = "Group state",
62 required = false, multiValued = false)
63 String state;
Jonathan Hart32600692015-03-09 10:38:40 -070064
Charles Chanf9b94ab2016-02-23 19:31:41 -080065 @Option(name = "-c", aliases = "--count",
66 description = "Print group count only",
67 required = false, multiValued = false)
68 private boolean countOnly = false;
69
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053070 @Option(name = "-t", aliases = "--type",
71 description = "Print groups with specified type",
72 required = false, multiValued = false)
73 private String type = null;
74
75
Ray Milkey39616f32015-05-14 15:43:00 -070076 private JsonNode json(Map<Device, List<Group>> sortedGroups) {
77 ArrayNode result = mapper().createArrayNode();
78
79 sortedGroups.forEach((device, groups) ->
80 groups.forEach(group ->
81 result.add(jsonForEntity(group, Group.class))));
82
83 return result;
84 }
85
Jonathan Hart32600692015-03-09 10:38:40 -070086 @Override
87 protected void execute() {
88 DeviceService deviceService = get(DeviceService.class);
89 GroupService groupService = get(GroupService.class);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070090 SortedMap<Device, List<Group>> sortedGroups =
91 getSortedGroups(deviceService, groupService);
Ray Milkey39616f32015-05-14 15:43:00 -070092 if (outputJson()) {
93 print("%s", json(sortedGroups));
94 } else {
95 sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
96 }
Jonathan Hart32600692015-03-09 10:38:40 -070097 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070098 /**
99 * Returns the list of devices sorted using the device ID URIs.
100 *
101 * @param deviceService device service
102 * @param groupService group service
103 * @return sorted device list
104 */
105 protected SortedMap<Device, List<Group>>
106 getSortedGroups(DeviceService deviceService,
107 GroupService groupService) {
108 SortedMap<Device, List<Group>> sortedGroups =
109 new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
110 List<Group> groups;
111 GroupState s = null;
Saurav Das36b37932017-04-07 17:24:38 -0700112 if (state != null && !"any".equals(state)) {
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700113 s = GroupState.valueOf(state.toUpperCase());
114 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700115 Iterable<Device> devices = deviceService.getDevices();
116 if (uri != null) {
117 Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
118 if (dev != null) {
119 devices = Collections.singletonList(dev);
120 }
121 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700122 for (Device d : devices) {
123 if (s == null) {
124 groups = newArrayList(groupService.getGroups(d.id()));
125 } else {
126 groups = newArrayList();
127 for (Group g : groupService.getGroups(d.id())) {
128 if (g.state().equals(s)) {
129 groups.add(g);
130 }
131 }
132 }
133 groups.sort(Comparators.GROUP_COMPARATOR);
134 sortedGroups.put(d, groups);
135 }
Andreas Pantelopoulos250ad712017-10-02 17:44:59 -0400136 if (type != null && !"any".equals(type)) {
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530137 for (Device device : sortedGroups.keySet()) {
138 sortedGroups.put(device, sortedGroups.get(device).stream()
139 .filter(group -> GroupDescription.Type.valueOf(type.toUpperCase()).equals(group.type()))
140 .collect(Collectors.toList()));
141 }
142 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700143 return sortedGroups;
144 }
145
146 private void printGroups(DeviceId deviceId, List<Group> groups) {
Saurav Das4ce45962015-11-24 23:21:05 -0800147 print("deviceId=%s, groupCount=%s", deviceId, groups.size());
Charles Chanf9b94ab2016-02-23 19:31:41 -0800148
149 if (countOnly) {
150 return;
151 }
152
Jonathan Hart32600692015-03-09 10:38:40 -0700153 for (Group group : groups) {
Saurav Das4f980082015-11-05 13:39:15 -0800154 print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(),
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530155 group.bytes(), group.packets(), group.appId().name(), group.referenceCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700156 int i = 0;
157 for (GroupBucket bucket:group.buckets().buckets()) {
Saurav Das4f980082015-11-05 13:39:15 -0800158 print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700159 bucket.bytes(), bucket.packets(),
160 bucket.treatment().allInstructions());
161 }
Jonathan Hart32600692015-03-09 10:38:40 -0700162 }
163 }
164}