blob: 48c2e1faefe9b266e525ba99e2d32d05e66d342d [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;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070021import org.apache.karaf.shell.commands.Argument;
Jonathan Hart32600692015-03-09 10:38:40 -070022import org.apache.karaf.shell.commands.Command;
Charles Chanf9b94ab2016-02-23 19:31:41 -080023import org.apache.karaf.shell.commands.Option;
Jonathan Hart32600692015-03-09 10:38:40 -070024import org.onosproject.cli.AbstractShellCommand;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070025import org.onosproject.net.Device;
Jonathan Hart32600692015-03-09 10:38:40 -070026import org.onosproject.net.DeviceId;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.group.Group;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070029import org.onosproject.net.group.Group.GroupState;
30import org.onosproject.net.group.GroupBucket;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053031import org.onosproject.net.group.GroupDescription;
Jonathan Hart32600692015-03-09 10:38:40 -070032import org.onosproject.net.group.GroupService;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053033import org.onosproject.utils.Comparators;
Jonathan Hart32600692015-03-09 10:38:40 -070034
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053035import java.util.Collections;
36import java.util.List;
37import java.util.Map;
Charles Chand916af92018-04-26 15:45:04 -040038import java.util.Optional;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053039import java.util.SortedMap;
40import java.util.TreeMap;
41import java.util.stream.Collectors;
Charles Chand916af92018-04-26 15:45:04 -040042import java.util.stream.Stream;
Ray Milkey39616f32015-05-14 15:43:00 -070043
Jonathan Hart32600692015-03-09 10:38:40 -070044/**
45 * Lists all groups in the system.
46 */
47@Command(scope = "onos", name = "groups",
48 description = "Lists all groups in the system")
49public class GroupsListCommand extends AbstractShellCommand {
50
Charles Chanf38aca72016-02-18 13:40:18 -080051 public static final String ANY = "any";
52
Jonathan Hart32600692015-03-09 10:38:40 -070053 private static final String FORMAT =
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053054 " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s, referenceCount=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070055 private static final String BUCKET_FORMAT =
Andreas Pantelopoulos250ad712017-10-02 17:44:59 -040056 " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070057
58 @Argument(index = 1, name = "uri", description = "Device ID",
59 required = false, multiValued = false)
60 String uri = null;
61
62 @Argument(index = 0, name = "state", description = "Group state",
63 required = false, multiValued = false)
64 String state;
Jonathan Hart32600692015-03-09 10:38:40 -070065
Charles Chanf9b94ab2016-02-23 19:31:41 -080066 @Option(name = "-c", aliases = "--count",
67 description = "Print group count only",
68 required = false, multiValued = false)
69 private boolean countOnly = false;
70
Charles Chand916af92018-04-26 15:45:04 -040071 @Option(name = "-r", aliases = "--referenced",
72 description = "Print referenced groups only",
73 required = false, multiValued = false)
74 private boolean referencedOnly = false;
75
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053076 @Option(name = "-t", aliases = "--type",
77 description = "Print groups with specified type",
78 required = false, multiValued = false)
79 private String type = null;
80
81
Ray Milkey39616f32015-05-14 15:43:00 -070082 private JsonNode json(Map<Device, List<Group>> sortedGroups) {
83 ArrayNode result = mapper().createArrayNode();
84
85 sortedGroups.forEach((device, groups) ->
86 groups.forEach(group ->
87 result.add(jsonForEntity(group, Group.class))));
88
89 return result;
90 }
91
Jonathan Hart32600692015-03-09 10:38:40 -070092 @Override
93 protected void execute() {
94 DeviceService deviceService = get(DeviceService.class);
95 GroupService groupService = get(GroupService.class);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070096 SortedMap<Device, List<Group>> sortedGroups =
97 getSortedGroups(deviceService, groupService);
Ray Milkey39616f32015-05-14 15:43:00 -070098 if (outputJson()) {
99 print("%s", json(sortedGroups));
100 } else {
101 sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
102 }
Jonathan Hart32600692015-03-09 10:38:40 -0700103 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700104 /**
105 * Returns the list of devices sorted using the device ID URIs.
106 *
107 * @param deviceService device service
108 * @param groupService group service
109 * @return sorted device list
110 */
Charles Chand916af92018-04-26 15:45:04 -0400111 protected SortedMap<Device, List<Group>> getSortedGroups(DeviceService deviceService, GroupService groupService) {
112 final GroupState groupsState = (this.state != null && !"any".equals(this.state)) ?
113 GroupState.valueOf(this.state.toUpperCase()) :
114 null;
115 final Iterable<Device> devices = Optional.ofNullable(uri)
116 .map(DeviceId::deviceId)
117 .map(deviceService::getDevice)
118 .map(dev -> (Iterable<Device>) Collections.singletonList(dev))
119 .orElse(deviceService.getDevices());
120
121 SortedMap<Device, List<Group>> sortedGroups = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700122 for (Device d : devices) {
Charles Chand916af92018-04-26 15:45:04 -0400123 Stream<Group> groupStream = Lists.newArrayList(groupService.getGroups(d.id())).stream();
124 if (groupsState != null) {
125 groupStream = groupStream.filter(g -> g.state().equals(groupsState));
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700126 }
Charles Chand916af92018-04-26 15:45:04 -0400127 if (referencedOnly) {
128 groupStream = groupStream.filter(g -> g.referenceCount() != 0);
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530129 }
Charles Chand916af92018-04-26 15:45:04 -0400130 if (type != null && !"any".equals(type)) {
131 groupStream = groupStream.filter(g ->
132 g.type().equals(GroupDescription.Type.valueOf(type.toUpperCase())));
133 }
134 sortedGroups.put(d, groupStream.sorted(Comparators.GROUP_COMPARATOR).collect(Collectors.toList()));
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530135 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700136 return sortedGroups;
137 }
138
139 private void printGroups(DeviceId deviceId, List<Group> groups) {
Saurav Das4ce45962015-11-24 23:21:05 -0800140 print("deviceId=%s, groupCount=%s", deviceId, groups.size());
Charles Chanf9b94ab2016-02-23 19:31:41 -0800141
142 if (countOnly) {
143 return;
144 }
145
Jonathan Hart32600692015-03-09 10:38:40 -0700146 for (Group group : groups) {
Saurav Das4f980082015-11-05 13:39:15 -0800147 print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(),
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530148 group.bytes(), group.packets(), group.appId().name(), group.referenceCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700149 int i = 0;
150 for (GroupBucket bucket:group.buckets().buckets()) {
Saurav Das4f980082015-11-05 13:39:15 -0800151 print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700152 bucket.bytes(), bucket.packets(),
153 bucket.treatment().allInstructions());
154 }
Jonathan Hart32600692015-03-09 10:38:40 -0700155 }
156 }
157}