Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Argument; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Command; |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Option; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 24 | import org.onosproject.net.Device; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.group.Group; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 28 | import org.onosproject.net.group.Group.GroupState; |
| 29 | import org.onosproject.net.group.GroupBucket; |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 30 | import org.onosproject.net.group.GroupDescription; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 31 | import org.onosproject.net.group.GroupService; |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 32 | import org.onosproject.utils.Comparators; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 33 | |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 34 | import java.util.Collections; |
| 35 | import java.util.List; |
| 36 | import java.util.Map; |
| 37 | import java.util.SortedMap; |
| 38 | import java.util.TreeMap; |
| 39 | import java.util.stream.Collectors; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 40 | |
| 41 | import static com.google.common.collect.Lists.newArrayList; |
| 42 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Lists all groups in the system. |
| 45 | */ |
| 46 | @Command(scope = "onos", name = "groups", |
| 47 | description = "Lists all groups in the system") |
| 48 | public class GroupsListCommand extends AbstractShellCommand { |
| 49 | |
Charles Chan | f38aca7 | 2016-02-18 13:40:18 -0800 | [diff] [blame] | 50 | public static final String ANY = "any"; |
| 51 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 52 | private static final String FORMAT = |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 53 | " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s, referenceCount=%s"; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 54 | private static final String BUCKET_FORMAT = |
Andreas Pantelopoulos | 250ad71 | 2017-10-02 17:44:59 -0400 | [diff] [blame] | 55 | " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s"; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 56 | |
| 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 Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 64 | |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 65 | @Option(name = "-c", aliases = "--count", |
| 66 | description = "Print group count only", |
| 67 | required = false, multiValued = false) |
| 68 | private boolean countOnly = false; |
| 69 | |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 70 | @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 Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 76 | 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 Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 86 | @Override |
| 87 | protected void execute() { |
| 88 | DeviceService deviceService = get(DeviceService.class); |
| 89 | GroupService groupService = get(GroupService.class); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 90 | SortedMap<Device, List<Group>> sortedGroups = |
| 91 | getSortedGroups(deviceService, groupService); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 92 | if (outputJson()) { |
| 93 | print("%s", json(sortedGroups)); |
| 94 | } else { |
| 95 | sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups)); |
| 96 | } |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 97 | } |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 98 | /** |
| 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 Das | 36b3793 | 2017-04-07 17:24:38 -0700 | [diff] [blame] | 112 | if (state != null && !"any".equals(state)) { |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 113 | s = GroupState.valueOf(state.toUpperCase()); |
| 114 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 115 | 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 Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 122 | 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 Pantelopoulos | 250ad71 | 2017-10-02 17:44:59 -0400 | [diff] [blame] | 136 | if (type != null && !"any".equals(type)) { |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 137 | 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 Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 143 | return sortedGroups; |
| 144 | } |
| 145 | |
| 146 | private void printGroups(DeviceId deviceId, List<Group> groups) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 147 | print("deviceId=%s, groupCount=%s", deviceId, groups.size()); |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 148 | |
| 149 | if (countOnly) { |
| 150 | return; |
| 151 | } |
| 152 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 153 | for (Group group : groups) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 154 | print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(), |
Sivachidambaram Subramanian | 9882abb | 2017-06-19 14:35:25 +0530 | [diff] [blame] | 155 | group.bytes(), group.packets(), group.appId().name(), group.referenceCount()); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 156 | int i = 0; |
| 157 | for (GroupBucket bucket:group.buckets().buckets()) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 158 | print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i, |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 159 | bucket.bytes(), bucket.packets(), |
| 160 | bucket.treatment().allInstructions()); |
| 161 | } |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |