blob: 58b09036cec895e65c2962a674268a74d0becc02 [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;
Charles Chand916af92018-04-26 15:45:04 -040020import com.google.common.collect.Lists;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.Argument;
22import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070023import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070024import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.apache.karaf.shell.api.action.Option;
Jonathan Hart32600692015-03-09 10:38:40 -070026import org.onosproject.cli.AbstractShellCommand;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070027import org.onosproject.net.Device;
Jonathan Hart32600692015-03-09 10:38:40 -070028import org.onosproject.net.DeviceId;
29import org.onosproject.net.device.DeviceService;
30import org.onosproject.net.group.Group;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070031import org.onosproject.net.group.Group.GroupState;
32import org.onosproject.net.group.GroupBucket;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053033import org.onosproject.net.group.GroupDescription;
Jonathan Hart32600692015-03-09 10:38:40 -070034import org.onosproject.net.group.GroupService;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053035import org.onosproject.utils.Comparators;
Jonathan Hart32600692015-03-09 10:38:40 -070036
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053037import java.util.Collections;
38import java.util.List;
39import java.util.Map;
Charles Chand916af92018-04-26 15:45:04 -040040import java.util.Optional;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053041import java.util.SortedMap;
42import java.util.TreeMap;
43import java.util.stream.Collectors;
Charles Chand916af92018-04-26 15:45:04 -040044import java.util.stream.Stream;
Ray Milkey39616f32015-05-14 15:43:00 -070045
Jonathan Hart32600692015-03-09 10:38:40 -070046/**
47 * Lists all groups in the system.
48 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049@Service
Jonathan Hart32600692015-03-09 10:38:40 -070050@Command(scope = "onos", name = "groups",
51 description = "Lists all groups in the system")
52public class GroupsListCommand extends AbstractShellCommand {
53
Charles Chanf38aca72016-02-18 13:40:18 -080054 public static final String ANY = "any";
55
Jonathan Hart32600692015-03-09 10:38:40 -070056 private static final String FORMAT =
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053057 " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s, referenceCount=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070058 private static final String BUCKET_FORMAT =
Thiago Santos5c34cfe2018-09-20 10:41:36 -070059 " id=0x%s, bucket=%s, bytes=%s, packets=%s, weight=%s, actions=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070060
61 @Argument(index = 1, name = "uri", description = "Device ID",
62 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070063 @Completion(DeviceIdCompleter.class)
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070064 String uri = null;
65
66 @Argument(index = 0, name = "state", description = "Group state",
67 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070068 @Completion(GroupStatusCompleter.class)
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070069 String state;
Jonathan Hart32600692015-03-09 10:38:40 -070070
Charles Chanf9b94ab2016-02-23 19:31:41 -080071 @Option(name = "-c", aliases = "--count",
72 description = "Print group count only",
73 required = false, multiValued = false)
74 private boolean countOnly = false;
75
Charles Chand916af92018-04-26 15:45:04 -040076 @Option(name = "-r", aliases = "--referenced",
77 description = "Print referenced groups only",
78 required = false, multiValued = false)
79 private boolean referencedOnly = false;
80
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053081 @Option(name = "-t", aliases = "--type",
82 description = "Print groups with specified type",
83 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070084 @Completion(GroupTypeCompleter.class)
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053085 private String type = null;
86
William Davies6b2be2f2020-01-24 15:52:17 -080087 @Option(name = "-u", aliases = "--unreferenced",
88 description = "Print unreferenced groups only",
89 required = false, multiValued = false)
90 private boolean unreferencedOnly = false;
91
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053092
Ray Milkey39616f32015-05-14 15:43:00 -070093 private JsonNode json(Map<Device, List<Group>> sortedGroups) {
94 ArrayNode result = mapper().createArrayNode();
95
96 sortedGroups.forEach((device, groups) ->
97 groups.forEach(group ->
98 result.add(jsonForEntity(group, Group.class))));
99
100 return result;
101 }
102
Jonathan Hart32600692015-03-09 10:38:40 -0700103 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700104 protected void doExecute() {
Jonathan Hart32600692015-03-09 10:38:40 -0700105 DeviceService deviceService = get(DeviceService.class);
106 GroupService groupService = get(GroupService.class);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700107 SortedMap<Device, List<Group>> sortedGroups =
108 getSortedGroups(deviceService, groupService);
William Davies6b2be2f2020-01-24 15:52:17 -0800109
110 if (referencedOnly && unreferencedOnly) {
111 print("Options -r and -u cannot be used at the same time");
112 return;
113 }
114
Ray Milkey39616f32015-05-14 15:43:00 -0700115 if (outputJson()) {
116 print("%s", json(sortedGroups));
117 } else {
118 sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
119 }
Jonathan Hart32600692015-03-09 10:38:40 -0700120 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700121 /**
122 * Returns the list of devices sorted using the device ID URIs.
123 *
124 * @param deviceService device service
125 * @param groupService group service
126 * @return sorted device list
127 */
Charles Chand916af92018-04-26 15:45:04 -0400128 protected SortedMap<Device, List<Group>> getSortedGroups(DeviceService deviceService, GroupService groupService) {
129 final GroupState groupsState = (this.state != null && !"any".equals(this.state)) ?
130 GroupState.valueOf(this.state.toUpperCase()) :
131 null;
132 final Iterable<Device> devices = Optional.ofNullable(uri)
133 .map(DeviceId::deviceId)
134 .map(deviceService::getDevice)
135 .map(dev -> (Iterable<Device>) Collections.singletonList(dev))
136 .orElse(deviceService.getDevices());
137
138 SortedMap<Device, List<Group>> sortedGroups = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700139 for (Device d : devices) {
Charles Chand916af92018-04-26 15:45:04 -0400140 Stream<Group> groupStream = Lists.newArrayList(groupService.getGroups(d.id())).stream();
141 if (groupsState != null) {
142 groupStream = groupStream.filter(g -> g.state().equals(groupsState));
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700143 }
Charles Chand916af92018-04-26 15:45:04 -0400144 if (referencedOnly) {
145 groupStream = groupStream.filter(g -> g.referenceCount() != 0);
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530146 }
Charles Chand916af92018-04-26 15:45:04 -0400147 if (type != null && !"any".equals(type)) {
148 groupStream = groupStream.filter(g ->
149 g.type().equals(GroupDescription.Type.valueOf(type.toUpperCase())));
150 }
William Davies6b2be2f2020-01-24 15:52:17 -0800151 if (unreferencedOnly) {
152 groupStream = groupStream.filter(g -> g.referenceCount() == 0);
153 }
Charles Chand916af92018-04-26 15:45:04 -0400154 sortedGroups.put(d, groupStream.sorted(Comparators.GROUP_COMPARATOR).collect(Collectors.toList()));
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530155 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700156 return sortedGroups;
157 }
158
159 private void printGroups(DeviceId deviceId, List<Group> groups) {
Saurav Das4ce45962015-11-24 23:21:05 -0800160 print("deviceId=%s, groupCount=%s", deviceId, groups.size());
Charles Chanf9b94ab2016-02-23 19:31:41 -0800161
162 if (countOnly) {
163 return;
164 }
165
Jonathan Hart32600692015-03-09 10:38:40 -0700166 for (Group group : groups) {
Saurav Das4f980082015-11-05 13:39:15 -0800167 print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(),
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530168 group.bytes(), group.packets(), group.appId().name(), group.referenceCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700169 int i = 0;
170 for (GroupBucket bucket:group.buckets().buckets()) {
Saurav Das4f980082015-11-05 13:39:15 -0800171 print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i,
Thiago Santos5c34cfe2018-09-20 10:41:36 -0700172 bucket.bytes(), bucket.packets(), bucket.weight(),
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700173 bucket.treatment().allInstructions());
174 }
Jonathan Hart32600692015-03-09 10:38:40 -0700175 }
176 }
177}