blob: bd61d03f070c4ae6606454811ab241ab01282ddb [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;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
24import org.apache.karaf.shell.api.action.Option;
Jonathan Hart32600692015-03-09 10:38:40 -070025import org.onosproject.cli.AbstractShellCommand;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070026import org.onosproject.net.Device;
Jonathan Hart32600692015-03-09 10:38:40 -070027import org.onosproject.net.DeviceId;
28import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.group.Group;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070030import org.onosproject.net.group.Group.GroupState;
31import org.onosproject.net.group.GroupBucket;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053032import org.onosproject.net.group.GroupDescription;
Jonathan Hart32600692015-03-09 10:38:40 -070033import org.onosproject.net.group.GroupService;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053034import org.onosproject.utils.Comparators;
Jonathan Hart32600692015-03-09 10:38:40 -070035
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053036import java.util.Collections;
37import java.util.List;
38import java.util.Map;
Charles Chand916af92018-04-26 15:45:04 -040039import java.util.Optional;
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053040import java.util.SortedMap;
41import java.util.TreeMap;
42import java.util.stream.Collectors;
Charles Chand916af92018-04-26 15:45:04 -040043import java.util.stream.Stream;
Ray Milkey39616f32015-05-14 15:43:00 -070044
Jonathan Hart32600692015-03-09 10:38:40 -070045/**
46 * Lists all groups in the system.
47 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048@Service
Jonathan Hart32600692015-03-09 10:38:40 -070049@Command(scope = "onos", name = "groups",
50 description = "Lists all groups in the system")
51public class GroupsListCommand extends AbstractShellCommand {
52
Charles Chanf38aca72016-02-18 13:40:18 -080053 public static final String ANY = "any";
54
Jonathan Hart32600692015-03-09 10:38:40 -070055 private static final String FORMAT =
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053056 " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s, referenceCount=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070057 private static final String BUCKET_FORMAT =
Andreas Pantelopoulos250ad712017-10-02 17:44:59 -040058 " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s";
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070059
60 @Argument(index = 1, name = "uri", description = "Device ID",
61 required = false, multiValued = false)
62 String uri = null;
63
64 @Argument(index = 0, name = "state", description = "Group state",
65 required = false, multiValued = false)
66 String state;
Jonathan Hart32600692015-03-09 10:38:40 -070067
Charles Chanf9b94ab2016-02-23 19:31:41 -080068 @Option(name = "-c", aliases = "--count",
69 description = "Print group count only",
70 required = false, multiValued = false)
71 private boolean countOnly = false;
72
Charles Chand916af92018-04-26 15:45:04 -040073 @Option(name = "-r", aliases = "--referenced",
74 description = "Print referenced groups only",
75 required = false, multiValued = false)
76 private boolean referencedOnly = false;
77
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +053078 @Option(name = "-t", aliases = "--type",
79 description = "Print groups with specified type",
80 required = false, multiValued = false)
81 private String type = null;
82
83
Ray Milkey39616f32015-05-14 15:43:00 -070084 private JsonNode json(Map<Device, List<Group>> sortedGroups) {
85 ArrayNode result = mapper().createArrayNode();
86
87 sortedGroups.forEach((device, groups) ->
88 groups.forEach(group ->
89 result.add(jsonForEntity(group, Group.class))));
90
91 return result;
92 }
93
Jonathan Hart32600692015-03-09 10:38:40 -070094 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070095 protected void doExecute() {
Jonathan Hart32600692015-03-09 10:38:40 -070096 DeviceService deviceService = get(DeviceService.class);
97 GroupService groupService = get(GroupService.class);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070098 SortedMap<Device, List<Group>> sortedGroups =
99 getSortedGroups(deviceService, groupService);
Ray Milkey39616f32015-05-14 15:43:00 -0700100 if (outputJson()) {
101 print("%s", json(sortedGroups));
102 } else {
103 sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
104 }
Jonathan Hart32600692015-03-09 10:38:40 -0700105 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700106 /**
107 * Returns the list of devices sorted using the device ID URIs.
108 *
109 * @param deviceService device service
110 * @param groupService group service
111 * @return sorted device list
112 */
Charles Chand916af92018-04-26 15:45:04 -0400113 protected SortedMap<Device, List<Group>> getSortedGroups(DeviceService deviceService, GroupService groupService) {
114 final GroupState groupsState = (this.state != null && !"any".equals(this.state)) ?
115 GroupState.valueOf(this.state.toUpperCase()) :
116 null;
117 final Iterable<Device> devices = Optional.ofNullable(uri)
118 .map(DeviceId::deviceId)
119 .map(deviceService::getDevice)
120 .map(dev -> (Iterable<Device>) Collections.singletonList(dev))
121 .orElse(deviceService.getDevices());
122
123 SortedMap<Device, List<Group>> sortedGroups = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700124 for (Device d : devices) {
Charles Chand916af92018-04-26 15:45:04 -0400125 Stream<Group> groupStream = Lists.newArrayList(groupService.getGroups(d.id())).stream();
126 if (groupsState != null) {
127 groupStream = groupStream.filter(g -> g.state().equals(groupsState));
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700128 }
Charles Chand916af92018-04-26 15:45:04 -0400129 if (referencedOnly) {
130 groupStream = groupStream.filter(g -> g.referenceCount() != 0);
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530131 }
Charles Chand916af92018-04-26 15:45:04 -0400132 if (type != null && !"any".equals(type)) {
133 groupStream = groupStream.filter(g ->
134 g.type().equals(GroupDescription.Type.valueOf(type.toUpperCase())));
135 }
136 sortedGroups.put(d, groupStream.sorted(Comparators.GROUP_COMPARATOR).collect(Collectors.toList()));
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530137 }
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700138 return sortedGroups;
139 }
140
141 private void printGroups(DeviceId deviceId, List<Group> groups) {
Saurav Das4ce45962015-11-24 23:21:05 -0800142 print("deviceId=%s, groupCount=%s", deviceId, groups.size());
Charles Chanf9b94ab2016-02-23 19:31:41 -0800143
144 if (countOnly) {
145 return;
146 }
147
Jonathan Hart32600692015-03-09 10:38:40 -0700148 for (Group group : groups) {
Saurav Das4f980082015-11-05 13:39:15 -0800149 print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(),
Sivachidambaram Subramanian9882abb2017-06-19 14:35:25 +0530150 group.bytes(), group.packets(), group.appId().name(), group.referenceCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700151 int i = 0;
152 for (GroupBucket bucket:group.buckets().buckets()) {
Saurav Das4f980082015-11-05 13:39:15 -0800153 print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700154 bucket.bytes(), bucket.packets(),
155 bucket.treatment().allInstructions());
156 }
Jonathan Hart32600692015-03-09 10:38:40 -0700157 }
158 }
159}