Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
| 19 | import java.util.List; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 20 | import java.util.Map; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 21 | import java.util.SortedMap; |
| 22 | import java.util.TreeMap; |
| 23 | |
| 24 | import org.apache.karaf.shell.commands.Argument; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Command; |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 26 | import org.apache.karaf.shell.commands.Option; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 27 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 28 | import org.onosproject.utils.Comparators; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Device; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.group.Group; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 33 | import org.onosproject.net.group.Group.GroupState; |
| 34 | import org.onosproject.net.group.GroupBucket; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 35 | import org.onosproject.net.group.GroupService; |
| 36 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 37 | import com.fasterxml.jackson.databind.JsonNode; |
| 38 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 39 | |
| 40 | import static com.google.common.collect.Lists.newArrayList; |
| 41 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Lists all groups in the system. |
| 44 | */ |
| 45 | @Command(scope = "onos", name = "groups", |
| 46 | description = "Lists all groups in the system") |
| 47 | public class GroupsListCommand extends AbstractShellCommand { |
| 48 | |
Charles Chan | f38aca7 | 2016-02-18 13:40:18 -0800 | [diff] [blame] | 49 | public static final String ANY = "any"; |
| 50 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 51 | private static final String FORMAT = |
Charles Chan | ccd59a4 | 2015-11-10 17:52:59 -0800 | [diff] [blame] | 52 | " id=0x%s, state=%s, type=%s, bytes=%s, packets=%s, appId=%s"; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 53 | private static final String BUCKET_FORMAT = |
Charles Chan | ccd59a4 | 2015-11-10 17:52:59 -0800 | [diff] [blame] | 54 | " id=0x%s, bucket=%s, bytes=%s, packets=%s, actions=%s"; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 55 | |
| 56 | @Argument(index = 1, name = "uri", description = "Device ID", |
| 57 | required = false, multiValued = false) |
| 58 | String uri = null; |
| 59 | |
| 60 | @Argument(index = 0, name = "state", description = "Group state", |
| 61 | required = false, multiValued = false) |
| 62 | String state; |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 63 | |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 64 | @Option(name = "-c", aliases = "--count", |
| 65 | description = "Print group count only", |
| 66 | required = false, multiValued = false) |
| 67 | private boolean countOnly = false; |
| 68 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 69 | private JsonNode json(Map<Device, List<Group>> sortedGroups) { |
| 70 | ArrayNode result = mapper().createArrayNode(); |
| 71 | |
| 72 | sortedGroups.forEach((device, groups) -> |
| 73 | groups.forEach(group -> |
| 74 | result.add(jsonForEntity(group, Group.class)))); |
| 75 | |
| 76 | return result; |
| 77 | } |
| 78 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 79 | @Override |
| 80 | protected void execute() { |
| 81 | DeviceService deviceService = get(DeviceService.class); |
| 82 | GroupService groupService = get(GroupService.class); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 83 | SortedMap<Device, List<Group>> sortedGroups = |
| 84 | getSortedGroups(deviceService, groupService); |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 85 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 86 | if (outputJson()) { |
| 87 | print("%s", json(sortedGroups)); |
| 88 | } else { |
| 89 | sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups)); |
| 90 | } |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Returns the list of devices sorted using the device ID URIs. |
| 95 | * |
| 96 | * @param deviceService device service |
| 97 | * @param groupService group service |
| 98 | * @return sorted device list |
| 99 | */ |
| 100 | protected SortedMap<Device, List<Group>> |
| 101 | getSortedGroups(DeviceService deviceService, |
| 102 | GroupService groupService) { |
| 103 | SortedMap<Device, List<Group>> sortedGroups = |
| 104 | new TreeMap<>(Comparators.ELEMENT_COMPARATOR); |
| 105 | List<Group> groups; |
| 106 | GroupState s = null; |
| 107 | if (state != null && !state.equals("any")) { |
| 108 | s = GroupState.valueOf(state.toUpperCase()); |
| 109 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 110 | Iterable<Device> devices = deviceService.getDevices(); |
| 111 | if (uri != null) { |
| 112 | Device dev = deviceService.getDevice(DeviceId.deviceId(uri)); |
| 113 | if (dev != null) { |
| 114 | devices = Collections.singletonList(dev); |
| 115 | } |
| 116 | } |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 117 | for (Device d : devices) { |
| 118 | if (s == null) { |
| 119 | groups = newArrayList(groupService.getGroups(d.id())); |
| 120 | } else { |
| 121 | groups = newArrayList(); |
| 122 | for (Group g : groupService.getGroups(d.id())) { |
| 123 | if (g.state().equals(s)) { |
| 124 | groups.add(g); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | groups.sort(Comparators.GROUP_COMPARATOR); |
| 129 | sortedGroups.put(d, groups); |
| 130 | } |
| 131 | return sortedGroups; |
| 132 | } |
| 133 | |
| 134 | private void printGroups(DeviceId deviceId, List<Group> groups) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 135 | print("deviceId=%s, groupCount=%s", deviceId, groups.size()); |
Charles Chan | f9b94ab | 2016-02-23 19:31:41 -0800 | [diff] [blame] | 136 | |
| 137 | if (countOnly) { |
| 138 | return; |
| 139 | } |
| 140 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 141 | for (Group group : groups) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 142 | print(FORMAT, Integer.toHexString(group.id().id()), group.state(), group.type(), |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 143 | group.bytes(), group.packets(), group.appId().name()); |
| 144 | int i = 0; |
| 145 | for (GroupBucket bucket:group.buckets().buckets()) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 146 | print(BUCKET_FORMAT, Integer.toHexString(group.id().id()), ++i, |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 147 | bucket.bytes(), bucket.packets(), |
| 148 | bucket.treatment().allInstructions()); |
| 149 | } |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |