blob: f162e771bf4a9dd02cea83d5795b45ca4bcffd01 [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 =
Andreas Pantelopoulos250ad712017-10-02 17:44:59 -040059 " id=0x%s, bucket=%s, bytes=%s, packets=%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
87
Ray Milkey39616f32015-05-14 15:43:00 -070088 private JsonNode json(Map<Device, List<Group>> sortedGroups) {
89 ArrayNode result = mapper().createArrayNode();
90
91 sortedGroups.forEach((device, groups) ->
92 groups.forEach(group ->
93 result.add(jsonForEntity(group, Group.class))));
94
95 return result;
96 }
97
Jonathan Hart32600692015-03-09 10:38:40 -070098 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070099 protected void doExecute() {
Jonathan Hart32600692015-03-09 10:38:40 -0700100 DeviceService deviceService = get(DeviceService.class);
101 GroupService groupService = get(GroupService.class);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700102 SortedMap<Device, List<Group>> sortedGroups =
103 getSortedGroups(deviceService, groupService);
Ray Milkey39616f32015-05-14 15:43:00 -0700104 if (outputJson()) {
105 print("%s", json(sortedGroups));
106 } else {
107 sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
108 }
Jonathan Hart32600692015-03-09 10:38:40 -0700109 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700110 /**
111 * Returns the list of devices sorted using the device ID URIs.
112 *
113 * @param deviceService device service
114 * @param groupService group service
115 * @return sorted device list
116 */
Charles Chand916af92018-04-26 15:45:04 -0400117 protected SortedMap<Device, List<Group>> getSortedGroups(DeviceService deviceService, GroupService groupService) {
118 final GroupState groupsState = (this.state != null && !"any".equals(this.state)) ?
119 GroupState.valueOf(this.state.toUpperCase()) :
120 null;
121 final Iterable<Device> devices = Optional.ofNullable(uri)
122 .map(DeviceId::deviceId)
123 .map(deviceService::getDevice)
124 .map(dev -> (Iterable<Device>) Collections.singletonList(dev))
125 .orElse(deviceService.getDevices());
126
127 SortedMap<Device, List<Group>> sortedGroups = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700128 for (Device d : devices) {
Charles Chand916af92018-04-26 15:45:04 -0400129 Stream<Group> groupStream = Lists.newArrayList(groupService.getGroups(d.id())).stream();
130 if (groupsState != null) {
131 groupStream = groupStream.filter(g -> g.state().equals(groupsState));
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700132 }
Charles Chand916af92018-04-26 15:45:04 -0400133 if (referencedOnly) {
134 groupStream = groupStream.filter(g -> g.referenceCount() != 0);
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530135 }
Charles Chand916af92018-04-26 15:45:04 -0400136 if (type != null && !"any".equals(type)) {
137 groupStream = groupStream.filter(g ->
138 g.type().equals(GroupDescription.Type.valueOf(type.toUpperCase())));
139 }
140 sortedGroups.put(d, groupStream.sorted(Comparators.GROUP_COMPARATOR).collect(Collectors.toList()));
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530141 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700142 return sortedGroups;
143 }
144
145 private void printGroups(DeviceId deviceId, List<Group> groups) {
Saurav Das4ce45962015-11-24 23:21:05 -0800146 print("deviceId=%s, groupCount=%s", deviceId, groups.size());
Charles Chanf9b94ab2016-02-23 19:31:41 -0800147
148 if (countOnly) {
149 return;
150 }
151
Jonathan Hart32600692015-03-09 10:38:40 -0700152 for (Group group : groups) {
Saurav Das4f980082015-11-05 13:39:15 -0800153 print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(),
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530154 group.bytes(), group.packets(), group.appId().name(), group.referenceCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700155 int i = 0;
156 for (GroupBucket bucket:group.buckets().buckets()) {
Saurav Das4f980082015-11-05 13:39:15 -0800157 print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700158 bucket.bytes(), bucket.packets(),
159 bucket.treatment().allInstructions());
160 }
Jonathan Hart32600692015-03-09 10:38:40 -0700161 }
162 }
163}